Unify IPv6 sendmsg codepaths.
Subsystems (5)
v6 cover Unify IPv6 sendmsg codepaths.
Ok, here's another attempt at reducing code duplication between the IPv6 UDP/raw/L2TP/ping sendmsg codepaths. I took Hannes's suggestion and created an ip6_output_opts structure which groups together parameters that go to ip6_append data (and which can be written to directly by ip6_datagram_send_ctl).
Does this look better now?
Since there was a concern that the previous patch might impact UDP sendmsg performance, I ran a couple of benchmarks on this version and the results suggest that impact is in the noise. x86_64 improved by 1-2%, but ARM got 1% worse. I suspect that this what this means is that any optimization made to this code is irrelevant compared to the overhead of context switches, system call setup, copying from userspace, etc.
Specifically, I tested sending UDP on loobpack with:
netperf -6 -H ::1 -t UDP_STREAM -- -m <size>
and counted the number of packets sent in 10 seconds, 5 times, for UDP payload sizes of 1 byte (the minimum) and 1452. Results:
x86_64 workstation, 1-byte UDP payload: +1.1%
- Before:
- Run #1: 2717005 2707060 2652702 2695698 2703355
- Run #2: 2711797 2692290 2699832 2705740 2698174
- After:
- Run #1: 2726430 2725619 2704406 2672582 2730418
- Run #2: 2777486 2740683 2732652 2707956 2772541
x86_64 workstation, 1452-byte UDP payload: +1.8%
- Before: 2577335 2514953 2572881 2571107 2571355
- After: 2620455 2609399 2635285 2610107 2568943
Nexus 5 phone, 1-byte UDP payload (backport to kernel 3.4): -1.0%
- Before: 213955 216284 217529 217785 216854
- After: 221096 216049 216426 203414 213897
Lorenzo Colitti (2):
net: ipv6: Unduplicate UDP, raw, and L2TP sendmsg
net: ipv6: Use ip6_datagram_send_common in ping.
include/net/ipv6.h | 33 ++++++++++- include/net/transp_v6.h | 7 ++- net/ipv6/datagram.c | 145 +++++++++++++++++++++++++++++++++++++++++++++-- net/ipv6/icmp.c | 27 +++++---- net/ipv6/ip6_flowlabel.c | 6 +- net/ipv6/ip6_output.c | 11 ++-- net/ipv6/ipv6_sockglue.c | 7 ++- net/ipv6/ping.c | 90 ++++++++++------------------- net/ipv6/raw.c | 101 ++------------------------------- net/ipv6/udp.c | 122 ++++----------------------------------- net/l2tp/l2tp_ip6.c | 108 +++-------------------------------- 11 files changed, 255 insertions(+), 402 deletions(-)
Signature · 1 line
1.9.1.423.g4596e3a
PATCH v6 1/2 net: ipv6: Unduplicate UDP, raw, and L2TP sendmsg
udpv6_sendmsg, rawv6_sendmsg and l2tp_ip6_sendmsg share ~100 lines of mostly identical code. Move this into a new ipv6_datagram_send_common helper function.
In order to do so without having to pass around too many parameters, define a new ip6_output_opts structure that holds some of the information needed by ip6_append_data and retrieved from userspace by ip6_datagram_send_ctl.
Tested: black-box functional testing using user-mode Linux. The following were tested to work as expected:
- UDP sends using sendto and connect/send.
- Raw socket sends using sendto.
- Mark routing and oif routing using SO_BINDTODEVICE on UDP and raw sockets.
- IPv6 extension headers set on UDP packets via setsockopt()
- L2TP compiles with CONFIG_IPV6={m,y} and CONFIG_L2TP_IP={m,y}.
Untested:
- Setting hop limit, tclass and options via cmsg.
Performance: netperf UDP_STREAM results (1-byte and 1452-byte payloads) shows impact is in the noise (+-1%).
--- include/net/ipv6.h | 33 ++++++++++- include/net/transp_v6.h | 7 ++- net/ipv6/datagram.c | 145 +++++++++++++++++++++++++++++++++++++++++++++-- net/ipv6/icmp.c | 27 +++++---- net/ipv6/ip6_flowlabel.c | 6 +- net/ipv6/ip6_output.c | 11 ++-- net/ipv6/ipv6_sockglue.c | 7 ++- net/ipv6/ping.c | 11 ++-- net/ipv6/raw.c | 101 ++------------------------------- net/ipv6/udp.c | 122 ++++----------------------------------- net/l2tp/l2tp_ip6.c | 108 +++-------------------------------- 11 files changed, 230 insertions(+), 348 deletions(-)
include/net/ipv6.h +30 -3
@@ -706,6 +706,28 @@ static inline u8 ip6_tclass(__be32 flowinfo) {return ntohl (flowinfo &IPV6_TCLASS_MASK ) >>IPV6_TCLASS_SHIFT ; } + +struct ip6_output_opts { +int hlimit:9 ; +int tclass:9 ; +int dontfrag:2 ; +unsigned connected:1 ; +struct ipv6_txoptions *opt; +}; +#define IP6_OUTOPTS_INIT { -1, -1, -1, 0, NULL } + +static inline void ip6_output_opts_sk_dst_init (struct ip6_output_opts *outopts, +struct ipv6_pinfo *np, +struct flowi6 *fl6, +struct dst_entry *dst) +{ + outopts->hlimit =ip6_sk_dst_hoplimit (np, fl6, dst); + outopts->tclass = np->tclass; + outopts->dontfrag = np->dontfrag; + outopts->connected =0 ; + outopts->opt = np->opt; +} +/* * Prototypes exported by ipv6 */
@@ -730,9 +752,10 @@ int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);int ip6_append_data (struct sock *sk,int getfrag (void *from,char *to,int offset,int len,int odd,struct sk_buff *skb), -void *from,int length,int transhdrlen,int hlimit, -int tclass,struct ipv6_txoptions *opt,struct flowi6 *fl6, -struct rt6_info *rt,unsigned int flags,int dontfrag); +void *from,int length,int transhdrlen, +struct flowi6 *fl6,struct rt6_info *rt, +const struct ip6_output_opts *outopts, +unsigned int flags);int ip6_push_pending_frames (struct sock *sk);
@@ -804,6 +827,10 @@ int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,int ip6_datagram_connect (struct sock *sk,struct sockaddr *addr,int addr_len);int ip6_datagram_connect_v6_only (struct sock *sk,struct sockaddr *addr,int addr_len); +int ip6_datagram_send_common (struct sock *sk,struct msghdr *msg, +struct flowi6 *fl6,struct dst_entry **dstp, +struct ip6_output_opts *opt, +struct ipv6_txoptions *opt_space);int ipv6_recv_error (struct sock *sk,struct msghdr *msg,int len,int *addr_len);
include/net/transp_v6.h +4 -3
@@ -11,6 +11,7 @@ extern struct proto tcpv6_prot;extern struct proto pingv6_prot;struct flowi6 ; +struct ip6_output_opts ;/* extension headers */ int ipv6_exthdrs_init (void );
@@ -40,9 +41,9 @@ void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,void ip6_datagram_recv_specific_ctl (struct sock *sk,struct msghdr *msg,struct sk_buff *skb); -int ip6_datagram_send_ctl (struct net *net,struct sock *sk,struct msghdr *msg, -struct flowi6 *fl6,struct ipv6_txoptions *opt, -int *hlimit,int *tclass,int *dontfrag); +int ip6_datagram_send_ctl (struct net *net,struct sock *sk, +struct msghdr *msg,struct flowi6 *fl6, +struct ip6_output_opts *outopts);void ip6_dgram_sock_seq_show (struct seq_file *seq,struct sock *sp,__u16 srcp,__u16 destp,int bucket);
net/ipv6/datagram.c +139 -6
190 lines not on this page · raw message
net/ipv6/icmp.c +13 -14
@@ -398,8 +398,8 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)int iif =0 ;int addr_type =0 ;int len; -int hlimit;int err =0 ; +struct ip6_output_opts outopts;if ((u8 *)hdr < skb->head || (skb_network_header (skb) +sizeof (*hdr)) >skb_tail_pointer (skb))
@@ -493,7 +493,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)if (IS_ERR (dst)) goto out; - hlimit =ip6_sk_dst_hoplimit (np, &fl6, dst); +ip6_output_opts_sk_dst_init (&outopts, np, &fl6, dst); msg.skb = skb; msg.offset =skb_network_offset (skb);
@@ -511,9 +511,9 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info) err =ip6_append_data (sk, icmpv6_getfrag, &msg, len +sizeof (struct icmp6hdr ), -sizeof (struct icmp6hdr ), hlimit, - np->tclass,NULL , &fl6, (struct rt6_info *)dst, -MSG_DONTWAIT , np->dontfrag); +sizeof (struct icmp6hdr ), + &fl6, (struct rt6_info *)dst, &outopts, +MSG_DONTWAIT );if (err) {ICMP6_INC_STATS (net, idev,ICMP6_MIB_OUTERRORS );ip6_flush_pending_frames (sk);
@@ -546,11 +546,10 @@ static void icmpv6_echo_reply(struct sk_buff *skb)struct icmp6hdr *icmph =icmp6_hdr (skb);struct icmp6hdr tmp_hdr;struct flowi6 fl6; +struct ip6_output_opts outopts;struct icmpv6_msg msg;struct dst_entry *dst;int err =0 ; -int hlimit; -u8 tclass; saddr = &ipv6_hdr (skb)->daddr;
@@ -588,19 +587,19 @@ static void icmpv6_echo_reply(struct sk_buff *skb)if (IS_ERR (dst)) goto out; - hlimit =ip6_sk_dst_hoplimit (np, &fl6, dst); - idev =__in6_dev_get (skb->dev); msg.skb = skb; msg.offset =0 ; msg.type =ICMPV6_ECHO_REPLY ; - tclass =ipv6_get_dsfield (ipv6_hdr (skb)); - err =ip6_append_data (sk, icmpv6_getfrag, &msg, skb->len +sizeof (struct icmp6hdr ), -sizeof (struct icmp6hdr ), hlimit, tclass,NULL , &fl6, - (struct rt6_info *)dst,MSG_DONTWAIT , - np->dontfrag); +ip6_output_opts_sk_dst_init (&outopts, np, &fl6, dst); + outopts.tclass =ipv6_get_dsfield (ipv6_hdr (skb)); + + err =ip6_append_data (sk, icmpv6_getfrag, &msg, + skb->len +sizeof (struct icmp6hdr ), +sizeof (struct icmp6hdr ), &fl6, + (struct rt6_info *)dst, &outopts,MSG_DONTWAIT );if (err) {ICMP6_INC_STATS_BH (net, idev,ICMP6_MIB_OUTERRORS );
net/ipv6/ip6_flowlabel.c +3 -3
@@ -374,7 +374,7 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,if (olen >0 ) {struct msghdr msg;struct flowi6 flowi6; -int junk; +struct ip6_output_opts outopts; err = -ENOMEM ; fl->opt =kmalloc (sizeof (*fl->opt) + olen,GFP_KERNEL );
@@ -391,10 +391,10 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq, msg.msg_control = (void *)(fl->opt+1 );memset (&flowi6,0 ,sizeof (flowi6)); - err =ip6_datagram_send_ctl (net, sk, &msg, &flowi6, fl->opt, - &junk, &junk, &junk); + err =ip6_datagram_send_ctl (net, sk, &msg, &flowi6, &outopts);if (err) goto done; + fl->opt = outopts.opt; err = -EINVAL ;if (fl->opt->opt_flen) goto done;
net/ipv6/ip6_output.c +7 -4
@@ -1123,8 +1123,9 @@ static void ip6_append_data_mtu(unsigned int *mtu,int ip6_append_data (struct sock *sk,int getfrag (void *from,char *to,int offset,int len,int odd,struct sk_buff *skb),void *from,int length,int transhdrlen, -int hlimit,int tclass,struct ipv6_txoptions *opt,struct flowi6 *fl6, -struct rt6_info *rt,unsigned int flags,int dontfrag) +struct flowi6 *fl6,struct rt6_info *rt, +const struct ip6_output_opts *outopts, +unsigned int flags) {struct inet_sock *inet =inet_sk (sk);struct ipv6_pinfo *np =inet6_sk (sk);
@@ -1138,6 +1139,8 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,int err;int offset =0 ;__u8 tx_flags =0 ; +struct ipv6_txoptions *opt = outopts->opt; +int dontfrag = outopts->dontfrag;if (flags&MSG_PROBE )return 0 ;
@@ -1183,8 +1186,8 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,dst_hold (&rt->dst); cork->dst = &rt->dst; inet->cork.fl.u.ip6 = *fl6; - np->cork.hop_limit = hlimit; - np->cork.tclass = tclass; + np->cork.hop_limit = outopts->hlimit; + np->cork.tclass = outopts->tclass;if (rt->dst.flags &DST_XFRM_TUNNEL ) mtu = np->pmtudisc >=IPV6_PMTUDISC_PROBE ? rt->dst.dev->mtu :dst_mtu (&rt->dst);
net/ipv6/ipv6_sockglue.c +4 -3
@@ -443,10 +443,10 @@ sticky_done:case IPV6_2292PKTOPTIONS : { +struct ip6_output_opts outopts;struct ipv6_txoptions *opt =NULL ;struct msghdr msg;struct flowi6 fl6; -int junk;memset (&fl6,0 ,sizeof (fl6)); fl6.flowi6_oif = sk->sk_bound_dev_if;
@@ -476,13 +476,14 @@ sticky_done: msg.msg_controllen = optlen; msg.msg_control = (void *)(opt+1 ); - retv =ip6_datagram_send_ctl (net, sk, &msg, &fl6, opt, &junk, - &junk, &junk); + outopts.opt = opt; + retv =ip6_datagram_send_ctl (net, sk, &msg, &fl6, &outopts);if (retv) goto done; update: retv =0 ; opt =ipv6_update_options (sk, opt); + done:if (opt)sock_kfree_s (sk, opt, opt->tot_len);
net/ipv6/ping.c +5 -6
@@ -83,13 +83,13 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, {struct inet_sock *inet =inet_sk (sk);struct ipv6_pinfo *np =inet6_sk (sk); +struct ip6_output_opts outopts;struct icmp6hdr user_icmph;int addr_type;struct in6_addr *daddr;int iif =0 ;struct flowi6 fl6;int err; -int hlimit;struct dst_entry *dst;struct rt6_info *rt;struct pingfakehdr pfh;
@@ -168,13 +168,12 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, pfh.wcheck =0 ; pfh.family =AF_INET6 ; - hlimit =ip6_sk_dst_hoplimit (np, &fl6, dst); + +ip6_output_opts_sk_dst_init (&outopts, np, &fl6, dst);lock_sock (sk); - err =ip6_append_data (sk, ping_getfrag, &pfh, len, -0 , hlimit, - np->tclass,NULL , &fl6, rt, -MSG_DONTWAIT , np->dontfrag); + err =ip6_append_data (sk, ping_getfrag, &pfh, len,0 , + &outopts, &fl6, rt,MSG_DONTWAIT );if (err) { ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
net/ipv6/raw.c +6 -95
146 lines not on this page · raw message
net/ipv6/udp.c +12 -110
184 lines not on this page · raw message
net/l2tp/l2tp_ip6.c +7 -101
144 lines not on this page · raw message
Signature · 1 line
1.9.1.423.g4596e3a
Re: [PATCH v6 1/2] net: ipv6: Unduplicate UDP, raw, and L2TP sendmsg
Hi!
include/net/ipv6.h static inline u8 ip6_tclass(__be32 flowinfo) · line 716
Quoted 8 lines · Lorenzo Colitti
include/net/ipv6.h +8
-0+ +struct ip6_output_opts { +int hlimit:9 ; +int tclass:9 ; +int dontfrag:2 ; +unsigned connected:1 ; +struct ipv6_txoptions *opt; +};
I don't think the bitfields are useful, they can cause more RMW cycles and at least on 64-bit systems the structure does not shrink in size if you instead just use s16 and bool types because of the padding needed for the pointer. On 32-bit it does save 4 bytes on the stack, but I don't think it is worth it.
If you put the pointer above the other members, newly added fields at the bottom can eat into padding space instead of leaving padding holes in it.
Greetings,
Hannes
David Miller
maintainerRe: [PATCH v6 1/2] net: ipv6: Unduplicate UDP, raw, and L2TP sendmsg
From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 05 May 2014 04:15:57 -0700
Quoted 18 lines
Hi!
include/net/ipv6.h static inline u8 ip6_tclass(__be32 flowinfo) · line 716
Quoted 8 lines · Lorenzo Colitti
include/net/ipv6.h +8
-0+ +struct ip6_output_opts { +int hlimit:9 ; +int tclass:9 ; +int dontfrag:2 ; +unsigned connected:1 ; +struct ipv6_txoptions *opt; +};I don't think the bitfields are useful, they can cause more RMW cycles and at least on 64-bit systems the structure does not shrink in size if you instead just use s16 and bool types because of the padding needed for the pointer. On 32-bit it does save 4 bytes on the stack, but I don't think it is worth it.
Quoted 3 lines
If you put the pointer above the other members, newly added fields at the bottom can eat into padding space instead of leaving padding holes in it.
Furthermore, some architectures are just going to pass this in via two registers anyways, so it doesn't save argument count in practice.
I apologize, but I still don't like these changes :-/
PATCH v6 2/2 net: ipv6: Use ip6_datagram_send_common in ping.
In reply to Lorenzo ColittiThis replaces the ad-hoc code used by ping6_sendmsg with the implementation now used by UDP, raw and L2TP sockets. This also adds the ability to set options via ancillary data, proper flowlabel validation, etc. etc.
Tested: Black-box tested using user-mode Linux.
- IPv6 pings using both connect()/send() and sendto() still work.
- Fragmented IPv6 pings still work.
- Specifying a flowlabel still works.
- Attempting to send a flowlabel that is not first set via IPV6_FLOWLABEL_MGR now correctly returns EINVAL.
--- net/ipv6/ping.c | 89 +++++++++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 59 deletions(-)
net/ipv6/ping.c +30 -59
@@ -81,16 +81,16 @@ static int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,int ping_v6_sendmsg (struct kiocb *iocb,struct sock *sk,struct msghdr *msg,size_t len) { + DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);struct inet_sock *inet =inet_sk (sk); -struct ipv6_pinfo *np =inet6_sk (sk); -struct ip6_output_opts outopts; +struct ipv6_txoptions opt_space; +struct ip6_output_opts outopts =IP6_OUTOPTS_INIT ;struct icmp6hdr user_icmph; -int addr_type; +int addr_len = msg->msg_namelen;struct in6_addr *daddr; -int iif =0 ; -struct flowi6 fl6;int err; -struct dst_entry *dst; +struct flowi6 fl6; +struct dst_entry *dst =NULL ;struct rt6_info *rt;struct pingfakehdr pfh;
@@ -101,63 +101,37 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,if (err)return err; -if (msg->msg_name) { - DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name); -if (msg->msg_namelen <sizeof (struct sockaddr_in6 ) || - u->sin6_family !=AF_INET6 ) { -return -EINVAL ; - } -if (sk->sk_bound_dev_if && - sk->sk_bound_dev_if != u->sin6_scope_id) { +if (sin6) { +if (addr_len <sizeof (struct sockaddr_in6 ))return -EINVAL ; - } - daddr = &(u->sin6_addr); - iif = u->sin6_scope_id; + +if (sin6->sin6_family !=AF_INET6 ) +return -EAFNOSUPPORT ; + + daddr = &sin6->sin6_addr; }else { -if (sk->sk_state !=TCP_ESTABLISHED ) -return -EDESTADDRREQ ; daddr = &sk->sk_v6_daddr; } -if (!iif) - iif = sk->sk_bound_dev_if; - - addr_type =ipv6_addr_type (daddr); -if (__ipv6_addr_needs_scope_id (addr_type) && !iif) -return -EINVAL ; -if (addr_type &IPV6_ADDR_MAPPED ) +if (ipv6_addr_v4mapped (daddr))return -EINVAL ; -/* TODO: use ip6_datagram_send_ctl to get options from cmsg */ -memset (&fl6,0 ,sizeof (fl6)); - fl6.flowi6_proto =IPPROTO_ICMPV6 ; - fl6.saddr = np->saddr; - fl6.daddr = *daddr; - fl6.flowi6_mark = sk->sk_mark; fl6.fl6_icmp_type = user_icmph.icmp6_type; fl6.fl6_icmp_code = user_icmph.icmp6_code; -security_sk_classify_flow (sk,flowi6_to_flowi (&fl6)); - -if (!fl6.flowi6_oif &&ipv6_addr_is_multicast (&fl6.daddr)) - fl6.flowi6_oif = np->mcast_oif; -else if (!fl6.flowi6_oif) - fl6.flowi6_oif = np->ucast_oif; - dst =ip6_sk_dst_lookup_flow (sk, &fl6, daddr); -if (IS_ERR (dst)) -return PTR_ERR (dst); - rt = (struct rt6_info *) dst; - - np =inet6_sk (sk); -if (!np) -return -EBADF ; + err =ip6_datagram_send_common (sk, msg, &fl6, &dst, + &outopts, &opt_space); +if (err) + goto out; -if (!fl6.flowi6_oif &&ipv6_addr_is_multicast (&fl6.daddr)) - fl6.flowi6_oif = np->mcast_oif; -else if (!fl6.flowi6_oif) - fl6.flowi6_oif = np->ucast_oif; +/* TODO: Move this check into ip6_datagram_sendmsg. */ +if (__ipv6_addr_needs_scope_id (__ipv6_addr_type (daddr)) && + !fl6.flowi6_oif) { + err = -EINVAL ; + goto out; + } pfh.icmph.type = user_icmph.icmp6_type; pfh.icmph.code = user_icmph.icmp6_code;
@@ -168,12 +142,10 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, pfh.wcheck =0 ; pfh.family =AF_INET6 ; - -ip6_output_opts_sk_dst_init (&outopts, np, &fl6, dst); - + rt = (struct rt6_info *) dst;lock_sock (sk); - err =ip6_append_data (sk, ping_getfrag, &pfh, len,0 , - &outopts, &fl6, rt,MSG_DONTWAIT ); + err =ip6_append_data (sk, ping_getfrag, &pfh, len,0 , &fl6, rt, + &outopts,MSG_DONTWAIT );if (err) { ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
@@ -186,10 +158,9 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, }release_sock (sk); -if (err) -return err; - -return len; +out: +dst_release (dst); +return err ? err : len; }#ifdef CONFIG_PROC_FS
Signature · 1 line
1.9.1.423.g4596e3a