net: ipv6: Unduplicate {raw,udp}v6_sendmsg code
Subsystems (4)
PATCH v5 1/3 net: ipv6: Unduplicate {raw,udp}v6_sendmsg code
rawv6_sendmsg and udpv6_sendmsg have ~100 lines of almost identical code. Move this into a new ipv6_datagram_send_common helper function.
Tested: black-box tested using user-mode Linux.
- Basic UDP sends using sendto work.
- Mark routing and oif routing using SO_BINDTODEVICE work.
--- include/net/ipv6.h | 7 +++ net/ipv6/datagram.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/raw.c | 105 +++------------------------------------- net/ipv6/udp.c | 125 +++++------------------------------------------- 4 files changed, 161 insertions(+), 210 deletions(-)
include/net/ipv6.h +7 -0
@@ -785,6 +785,13 @@ 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 sockaddr_in6 *sin6,int addr_len, +struct flowi6 *fl6,struct dst_entry **dstp, +struct ipv6_txoptions **optp, +struct ipv6_txoptions *opt_space, +int *hlimit,int *tclass,int *dontfrag, +int *connected);int ipv6_recv_error (struct sock *sk,struct msghdr *msg,int len,int *addr_len);
net/ipv6/datagram.c +134 -0
140 lines not on this page · raw message
net/ipv6/raw.c +7 -98
147 lines not on this page · raw message
net/ipv6/udp.c +13 -112
172 lines not on this page · raw message
Signature · 1 line
1.9.1.423.g4596e3a
PATCH v5 2/3 net: ipv6: Use ip6_datagram_send_common in L2TP IPv6.
This code was also virtually identical with the UDP and raw socket sendmsg code.
Tested: compiles with CONFIG_IPV6={m,y} and CONFIG_L2TP_IP={m,y}.
--- net/l2tp/l2tp_ip6.c | 112 ++++------------------------------------------------ 1 file changed, 8 insertions(+), 104 deletions(-)
net/l2tp/l2tp_ip6.c +8 -104
@@ -485,19 +485,15 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk, {struct ipv6_txoptions opt_space; DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name); -struct in6_addr *daddr, *final_p, final; -struct ipv6_pinfo *np =inet6_sk (sk); -struct ipv6_txoptions *opt =NULL ; -struct ip6_flowlabel *flowlabel =NULL ; +struct ipv6_txoptions *opt;struct dst_entry *dst =NULL ;struct flowi6 fl6;int addr_len = msg->msg_namelen; -int hlimit =-1 ; -int tclass =-1 ; -int dontfrag =-1 ; +int hlimit, tclass, dontfrag;int transhdrlen =4 ;/* zero session-id */ int ulen = len + transhdrlen;int err; +int connected;/* Rough check on arithmetic overflow, better check is made in ip6_append_data().
@@ -514,7 +510,7 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,*/ memset (&fl6,0 ,sizeof (fl6)); - fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_proto = sk->sk_protocol;if (lsa) {if (addr_len <SIN6_LEN_RFC2133 )
@@ -522,103 +518,13 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,if (lsa->l2tp_family && lsa->l2tp_family !=AF_INET6 )return -EAFNOSUPPORT ; - - daddr = &lsa->l2tp_addr; -if (np->sndflow) { - fl6.flowlabel = lsa->l2tp_flowinfo &IPV6_FLOWINFO_MASK ; -if (fl6.flowlabel&IPV6_FLOWLABEL_MASK ) { - flowlabel =fl6_sock_lookup (sk, fl6.flowlabel); -if (flowlabel ==NULL ) -return -EINVAL ; - } - } - -/* -* Otherwise it will be difficult to maintain -* sk->sk_dst_cache. -*/ -if (sk->sk_state ==TCP_ESTABLISHED && -ipv6_addr_equal (daddr, &sk->sk_v6_daddr)) - daddr = &sk->sk_v6_daddr; - -if (addr_len >=sizeof (struct sockaddr_in6 ) && - lsa->l2tp_scope_id && -ipv6_addr_type (daddr) &IPV6_ADDR_LINKLOCAL ) - fl6.flowi6_oif = lsa->l2tp_scope_id; - }else { -if (sk->sk_state !=TCP_ESTABLISHED ) -return -EDESTADDRREQ ; - - daddr = &sk->sk_v6_daddr; - fl6.flowlabel = np->flow_label; - } - -if (fl6.flowi6_oif ==0 ) - fl6.flowi6_oif = sk->sk_bound_dev_if; - -if (msg->msg_controllen) { - opt = &opt_space; -memset (opt,0 ,sizeof (struct ipv6_txoptions )); - opt->tot_len =sizeof (struct ipv6_txoptions ); - - err =ip6_datagram_send_ctl (sock_net (sk), sk, msg, &fl6, opt, - &hlimit, &tclass, &dontfrag); -if (err <0 ) { -fl6_sock_release (flowlabel); -return err; - } -if ((fl6.flowlabel &IPV6_FLOWLABEL_MASK ) && !flowlabel) { - flowlabel =fl6_sock_lookup (sk, fl6.flowlabel); -if (flowlabel ==NULL ) -return -EINVAL ; - } -if (!(opt->opt_nflen|opt->opt_flen)) - opt =NULL ; } -if (opt ==NULL ) - opt = np->opt; -if (flowlabel) - opt =fl6_merge_options (&opt_space, flowlabel, opt); - opt =ipv6_fixup_options (&opt_space, opt); - - fl6.flowi6_proto = sk->sk_protocol; -if (!ipv6_addr_any (daddr)) - fl6.daddr = *daddr; -else - fl6.daddr.s6_addr[15 ] =0x1 ;/* :: means loopback (BSD'ism) */ -if (ipv6_addr_any (&fl6.saddr) && !ipv6_addr_any (&np->saddr)) - fl6.saddr = np->saddr; - - final_p =fl6_update_dst (&fl6, opt, &final); - -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; - -security_sk_classify_flow (sk,flowi6_to_flowi (&fl6)); - - dst =ip6_dst_lookup_flow (sk, &fl6, final_p); -if (IS_ERR (dst)) { - err =PTR_ERR (dst); + err =ip6_datagram_send_common (sk, msg, (struct sockaddr_in6 *) lsa, + addr_len, &fl6, &dst, &opt, &opt_space, + &hlimit, &tclass, &dontfrag, &connected); +if (err) goto out; - } - -if (hlimit <0 ) { -if (ipv6_addr_is_multicast (&fl6.daddr)) - hlimit = np->mcast_hops; -else - hlimit = np->hop_limit; -if (hlimit <0 ) - hlimit =ip6_dst_hoplimit (dst); - } - -if (tclass <0 ) - tclass = np->tclass; - -if (dontfrag <0 ) - dontfrag = np->dontfrag;if (msg->msg_flags &MSG_CONFIRM ) goto do_confirm;
@@ -637,8 +543,6 @@ back_from_confirm: done:dst_release (dst); out: -fl6_sock_release (flowlabel); -return err <0 ? err : len; do_confirm:
Signature · 1 line
1.9.1.423.g4596e3a
PATCH v5 3/3 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 | 95 +++++++++++++++++++-------------------------------------- 1 file changed, 31 insertions(+), 64 deletions(-)
net/ipv6/ping.c +31 -64
@@ -81,16 +81,17 @@ 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 ipv6_txoptions *opt, opt_space;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; -int hlimit; -struct dst_entry *dst; +int hlimit, tclass, dontfrag; +int connected; +struct dst_entry *dst =NULL ;struct rt6_info *rt;struct pingfakehdr pfh;
@@ -101,63 +102,38 @@ 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 ) { +if (sin6) { +if (addr_len <sizeof (struct sockaddr_in6 ))return -EINVAL ; - } -if (sk->sk_bound_dev_if && - sk->sk_bound_dev_if != u->sin6_scope_id) { -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, sin6, addr_len, &fl6, &dst, + &opt, &opt_space, &hlimit, &tclass, + &dontfrag, &connected); +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,18 +144,10 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, pfh.wcheck =0 ; pfh.family =AF_INET6 ; -if (ipv6_addr_is_multicast (&fl6.daddr)) - hlimit = np->mcast_hops; -else - hlimit = np->hop_limit; -if (hlimit <0 ) - hlimit =ip6_dst_hoplimit (dst); - + rt = (struct rt6_info *) 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 , hlimit, tclass, + opt, &fl6, rt, msg->msg_flags, dontfrag);if (err) { ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
@@ -192,10 +160,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
From: Lorenzo Colitti
Quoted 8 lines
rawv6_sendmsgandudpv6_sendmsghave ~100 lines of almost identical code. Move this into a newipv6_datagram_send_commonhelper function.Tested: black-box tested using user-mode Linux.
- Basic UDP sends using sendto work.
- Mark routing and oif routing usingSO_BINDTODEVICEwork.
...
Quoted 5 lines
include/net/ipv6.h +0
-0
include/net/ipv6.h int compat_ipv6_getsockopt(struct sock *sk, int level, int optname, · line 794
Quoted 10 lines
include/net/ipv6.h +7
-0@@ -785,6 +785,13 @@ 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 sockaddr_in6 *sin6,int addr_len, +struct flowi6 *fl6,struct dst_entry **dstp, +struct ipv6_txoptions **optp, +struct ipv6_txoptions *opt_space, +int *hlimit,int *tclass,int *dontfrag, +int *connected);
That is a lot of parameters! and udp send is probably an important path.
You also repeatedly dereference some of them (eg hlimit).
I think that all of hlimit, tclass, dontfrag and connected could be shadowed by locals and only written out on success (maybe into a structure?).
It might also be possible to reduce the number of arguments by using a sequence of calls. (getting the args 'passed by register' helps performance.)
David
David Miller
maintainerFrom: David Laight <[email protected]>
Date: Fri, 25 Apr 2014 11:24:21 +0000
Quoted 19 lines
From: Lorenzo Colitti
Quoted 8 lines
rawv6_sendmsgandudpv6_sendmsghave ~100 lines of almost identical code. Move this into a newipv6_datagram_send_commonhelper function.Tested: black-box tested using user-mode Linux.
- Basic UDP sends using sendto work.
- Mark routing and oif routing usingSO_BINDTODEVICEwork....
Quoted 9 lines
include/net/ipv6.h +1
-0@@ -785,6 +785,13 @@ 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,
Quoted 8 lines
include/net/ipv6.h int compat_ipv6_getsockopt(struct sock *sk, int level, int optname, · line 794
Quoted 6 lines
include/net/ipv6.h +6
-0+ struct sockaddr_in6 *sin6, int addr_len, + struct flowi6 *fl6, struct dst_entry **dstp, + struct ipv6_txoptions **optp, + struct ipv6_txoptions *opt_space, + int *hlimit, int *tclass, int *dontfrag, + int *connected);That is a lot of parameters! and udp send is probably an important path.
The more I read this series the more I dislike it.
All of these pointers being passed in, were local variables in the original code.
This consolidation makes the code much worse, not better.
Quoted 31 lines · David Miller
From: David Laight <[email protected]>
Date: Fri, 25 Apr 2014 11:24:21 +0000Quoted 19 lines
From: Lorenzo Colitti
Quoted 8 lines
rawv6_sendmsgandudpv6_sendmsghave ~100 lines of almost identical code. Move this into a newipv6_datagram_send_commonhelper function.Tested: black-box tested using user-mode Linux.
- Basic UDP sends using sendto work.
- Mark routing and oif routing usingSO_BINDTODEVICEwork....
Quoted 9 lines
include/net/ipv6.h +1
-0@@ -785,6 +785,13 @@ 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,Quoted 8 lines
include/net/ipv6.h int compat_ipv6_getsockopt(struct sock *sk, int level, int optname, · line 794
Quoted 6 lines
include/net/ipv6.h +6
-0+ struct sockaddr_in6 *sin6, int addr_len, + struct flowi6 *fl6, struct dst_entry **dstp, + struct ipv6_txoptions **optp, + struct ipv6_txoptions *opt_space, + int *hlimit, int *tclass, int *dontfrag, + int *connected);That is a lot of parameters! and udp send is probably an important path.
Quoted 6 lines
The more I read this series the more I dislike it.
All of these pointers being passed in, were local variables in the original code.
This consolidation makes the code much worse, not better.
Maybe we can put all those hlimit, tclass, dontfrag vars into a struct and just pass one pointer.
Also it could be possible to just do that by value if return slot optimization in gcc optimizes that away.
Greetings,
Hannes
David Miller
maintainerFrom: Hannes Frederic Sowa <[email protected]>
Date: Fri, 25 Apr 2014 19:19:09 +0200
Quoted 2 lines
Maybe we can put all those hlimit, tclass, dontfrag vars into a struct and just pass one pointer.
It's still an unnecessary memory dereference.
The compiler can potentially optimize the accesses to those variables into a local register with the existing code, with this consolidation it no longer can.
Quoted 7 lines · David Miller
From: Hannes Frederic Sowa <[email protected]>
Date: Fri, 25 Apr 2014 19:19:09 +0200Quoted 2 lines
Maybe we can put all those hlimit, tclass, dontfrag vars into a struct and just pass one pointer.
It's still an unnecessary memory dereference.
True, true.
Quoted 3 lines
The compiler can potentially optimize the accesses to those variables into a local register with the existing code, with this consolidation it no longer can.
We could hack up the function in a local header file in net/ipv6 and __always_inline it. If the resulting assembly looks good would this be acceptable for you? Maybe we could switch to something like struct ipv6_output_opts where we gather all those state variables common to the code paths.
Thanks,
Hannes
David Miller
maintainerFrom: Hannes Frederic Sowa <[email protected]>
Date: Fri, 25 Apr 2014 23:26:42 +0200
Quoted 15 lines · David Miller
Quoted 7 lines
From: Hannes Frederic Sowa <[email protected]>
Date: Fri, 25 Apr 2014 19:19:09 +0200Quoted 2 lines
Maybe we can put all those hlimit, tclass, dontfrag vars into a struct and just pass one pointer.
It's still an unnecessary memory dereference.
True, true.
Quoted 3 lines
The compiler can potentially optimize the accesses to those variables into a local register with the existing code, with this consolidation it no longer can.
Quoted 5 lines
We could hack up the function in a local header file in net/ipv6 and
__always_inlineit. If the resulting assembly looks good would this be acceptable for you? Maybe we could switch to something like structipv6_output_optswhere we gather all those state variables common to the code paths.
I think the consolidation attempts seen so far generally look like crap because we haven't found the right abstraction yet.
Once a good fit is found I don't think there will be any reason for me to consider the result ugly.
Quoted 1 line · David Miller
This consolidation makes the code much worse, not better.
This is because the three codepaths differ at the beginning, have a common middle, and then later on UDP differs because it supports corking and udplite, and because it does a ip6_dst_store at the end (which I'd argue l2tp and ping should do as well but...).
I thought reducing duplication was useful, so I sent a patch to factor out the duplicated code in the middle. The problem is that since the codepaths also differ at the end, it has to pass in a lot of parameters in, and pointers to local variables that we need later on.
I see a few ways to proceed here.
1. Refactor all these codepaths into a ip6_datagram_sendmsg that handles all the code all the way to sending the packet. That would basically look like udpv6_sendmsg does today, but it would also support non-UDP sockets. A few more if statements, but no more duplicated code.
2. Rework raw and l2tp (which are almost identical), into one function, make ping use that function, and leave UDP alone.
3. Duplicate the cmsg / flowlabel parsing code into ping6.
4. Do nothing and have ping6 not support these APIs.
Thoughts?