Skip to contentSkip to conversation

Use sk_mark for routing lookup in more places

PATCH Applied Atis Elsts 5 participants 12 messages Oct 1, 2009 – Oct 7, 2009

Subsystems (6)
lore
Share & tools
lore

Atis Elsts

author
#0 lore raw

PATCH 1/1 Use sk_mark for routing lookup in more places

This patch against v2.6.31 adds support for route lookup using sk_mark in some more places. The benefits from this patch are the following. First, SO_MARK option now has effect on UDP sockets too.
Second, ip_queue_xmit() and inet_sk_rebuild_header() could fail to do routing lookup correctly if TCP sockets with SO_MARK were used.

---
 net/ipv4/af_inet.c   |    1 +
 net/ipv4/ip_output.c |    1 +
 net/ipv4/udp.c       |    1 +
 3 files changed, 3 insertions(+)
net/ipv4/af_inet.c +1 -0
index 566ea6c..7917963 100644
@@ -1103,6 +1103,7 @@ int inet_sk_rebuild_header(struct sock *sk)
 {
        struct flowi fl = {
                .oif = sk->sk_bound_dev_if,
+               .mark = sk->sk_mark,
                .nl_u = {
                        .ip4_u = {
                                .daddr  = daddr,
net/ipv4/ip_output.c +1 -0
index 7ffcd96..e088a97 100644
@@ -335,6 +335,7 @@ int ip_queue_xmit(struct sk_buff *skb, int ipfragok)
 
                {
                        struct flowi fl = { .oif = sk->sk_bound_dev_if,
+                                           .mark = sk->sk_mark,
                                            .nl_u = { .ip4_u =
                                                      { .daddr = daddr,
                                                        .saddr = inet->saddr,
net/ipv4/udp.c +0 -0
index 80e3812..f90cdcc 100644
@@ -688,6 +688,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, 

struct msghdr *msg,

if (rt == NULL) {
struct flowi fl = { .oif = ipc.oif,
+ .mark = sk->sk_mark,
.nl_u = { .ip4_u =
{ .daddr = faddr,
.saddr = saddr,

Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Atis Elsts

This patch against v2.6.31 adds support for route lookup using sk_mark in some more places. The benefits from this patch are the following. First, SO_MARK option now has effect on UDP sockets too.
Second, ip_queue_xmit() and inet_sk_rebuild_header() could fail to do routing lookup correctly if TCP sockets with SO_MARK were used.

Good catch, thanks !

I used SO_MARK on connected UDP sockets so did not notice the lack of functionality. (ip_route_connect() does use sk->sk_mark)

Quoted 44 lines
---
 net/ipv4/af_inet.c   |    1 +
 net/ipv4/ip_output.c |    1 +
 net/ipv4/udp.c       |    1 +
 3 files changed, 3 insertions(+)
net/ipv4/af_inet.c +1 -0
index 566ea6c..7917963 100644
@@ -1103,6 +1103,7 @@ int inet_sk_rebuild_header(struct sock *sk)
 {
        struct flowi fl = {
                .oif = sk->sk_bound_dev_if,
+               .mark = sk->sk_mark,
                .nl_u = {
                        .ip4_u = {
                                .daddr  = daddr,
net/ipv4/ip_output.c +1 -0
index 7ffcd96..e088a97 100644
@@ -335,6 +335,7 @@ int ip_queue_xmit(struct sk_buff *skb, int ipfragok)
 
                {
                        struct flowi fl = { .oif = sk->sk_bound_dev_if,
+                                           .mark = sk->sk_mark,
                                            .nl_u = { .ip4_u =
                                                      { .daddr = daddr,
                                                        .saddr = inet->saddr,
net/ipv4/udp.c +1 -0
index 80e3812..f90cdcc 100644
@@ -688,6 +688,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, 
 struct msghdr *msg,
 
        if (rt == NULL) {
                struct flowi fl = { .oif = ipc.oif,
+                                   .mark = sk->sk_mark,
                                    .nl_u = { .ip4_u =
                                              { .daddr = faddr,

.saddr = saddr,
--

maintainer Applied
#2 lore raw

From: Eric Dumazet <[email protected]>
Date: Thu, 01 Oct 2009 18:30:06 +0200

Quoted 7 lines
Quoted 6 lines · Atis Elsts

This patch against v2.6.31 adds support for route lookup using sk_mark in some more places. The benefits from this patch are the following. First, SO_MARK option now has effect on UDP sockets too.
Second, ip_queue_xmit() and inet_sk_rebuild_header() could fail to do routing lookup correctly if TCP sockets with SO_MARK were used.

Quoted 8 lines
Quoted 1 line

Good catch, thanks !

I used SO_MARK on connected UDP sockets so did not notice the lack of functionality. (ip_route_connect() does use sk->sk_mark)

Applied, thanks.

But Atis, your patch was whitespace corrupted by your email client so I had to apply your patch by hand. Please correct this problem before submitting future patches, thank you.

#3 lore raw

Here is a followup on this area, thanks.

[RFC] af_packet: fill skb->mark at xmit

skb->mark may be used by classifiers, so fill it in case user set a SO_MARK option on socket.

---
 net/packet/af_packet.c |    3 +++
 1 file changed, 3 insertions(+)
net/packet/af_packet.c +3 -0
index d7ecca0..610f150 100644
@@ -490,6 +490,7 @@ static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
 	skb->protocol = proto;
 	skb->dev = dev;
 	skb->priority = sk->sk_priority;
+	skb->mark = sk->sk_mark;
 	if (err)
 		goto out_free;
 
@@ -856,6 +857,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 	skb->protocol = proto;
 	skb->dev = dev;
 	skb->priority = po->sk.sk_priority;
+	skb->mark = po->sk.sk_mark;
 	skb_shinfo(skb)->destructor_arg = ph.raw;
 
 	switch (po->tp_version) {
@@ -1125,6 +1127,7 @@ static int packet_snd(struct socket *sock,
 	skb->protocol = proto;
 	skb->dev = dev;
 	skb->priority = sk->sk_priority;
+	skb->mark = sk->sk_mark;
 
 	/*
 	 *	Now send it
#4 lore raw
Quoted 7 lines · Eric Dumazet

Here is a followup on this area, thanks.

[RFC] af_packet: fill skb->mark at xmit

skb->mark may be used by classifiers, so fill it in case user set a SO_MARK option on socket.

Maybe a more generic way to handle this for various protocols would be to fill skb->mark in sock_alloc_send_pskb()

#5 lore raw

Cool!

As I've already pointed out in a post 2 or so weeks ago, we need the exact same treatment in a ton of places throughout the code (tcp, ipv6, decnet, etc...).

Maybe it would make more sense to create some constructor-like functions for the flowi struct?

Quoted 18 lines · Eric Dumazet
Quoted 7 lines · Eric Dumazet

Here is a followup on this area, thanks.

[RFC] af_packet: fill skb->mark at xmit

skb->mark may be used by classifiers, so fill it in case user set a SO_MARK option on socket.

Maybe a more generic way to handle this for various protocols would be to fill skb->mark in sock_alloc_send_pskb()

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

maintainer
#6 lore raw

From: Maciej Żenczykowski <[email protected]>
Date: Fri, 2 Oct 2009 10:25:13 -0700

Quoted 2 lines

Maybe it would make more sense to create some constructor-like functions for the flowi struct?

Maybe just an initializer like "FLOWI_SOCK(sk)" or similar. So you can say:

	struct flowi fl = FLOWI_SOCK(sk);

But the thing is we usually want to initialize all of the details in one go, so we'd need a very messy macro for this that would take many arguments.

It's important to use an initializer rather than assignments in some inline function so that GCC can better coalesce many small members into since large stores to the stack. It doesn't do this as well with real assignment statements.

#7 lore raw

PATCH 1/1 Use sk_mark for IPv6 routing lookups

In reply to Maciej Żenczykowski
Quoted 5 lines · Maciej Żenczykowski

Cool!

As I've already pointed out in a post 2 or so weeks ago, we need the exact same treatment in a ton of places throughout the code (tcp, ipv6, decnet, etc...).

Here's a try at the IPv6 part...

Add support for IPv6 route lookups using sk_mark.

---

net/ipv6/datagram.c +1 -0
index e2bdc6d..a615b4d 100644
@@ -147,6 +147,7 @@ ipv4_connected:
 	ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
 	ipv6_addr_copy(&fl.fl6_src, &np->saddr);
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = inet->dport;
 	fl.fl_ip_sport = inet->sport;
 
net/ipv6/inet6_connection_sock.c +1 -0
index cc4797d..a9f4a21 100644
@@ -194,6 +194,7 @@ int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
 	fl.fl6_flowlabel = np->flow_label;
 	IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_sport = inet->sport;
 	fl.fl_ip_dport = inet->dport;
 	security_sk_classify_flow(sk, &fl);
net/ipv6/ip6mr.c +1 -0
index 7161539..d98df54 100644
@@ -1523,6 +1523,7 @@ static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
 
 	fl = (struct flowi) {
 		.oif = vif->link,
+		.mark = skb->mark,
 		.nl_u = { .ip6_u =
 				{ .daddr = ipv6h->daddr, }
 		}
net/ipv6/ipv6_sockglue.c +1 -0
index 14f54eb..dc0f736 100644
@@ -424,6 +424,7 @@ sticky_done:
 
 		fl.fl6_flowlabel = 0;
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 
 		if (optlen == 0)
 			goto update;
net/ipv6/sit.c +1 -0
index dbd19a7..03a64c1 100644
@@ -629,6 +629,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
 						.saddr = tiph->saddr,
 						.tos = RT_TOS(tos) } },
 				    .oif = tunnel->parms.link,
+				    .mark = skb->mark,
 				    .proto = IPPROTO_IPV6 };
 		if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
 			stats->tx_carrier_errors++;
net/ipv6/tcp_ipv6.c +4 -0
index 21d100b..321aafd 100644
@@ -243,6 +243,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
 	ipv6_addr_copy(&fl.fl6_src,
 		       (saddr ? saddr : &np->saddr));
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = usin->sin6_port;
 	fl.fl_ip_sport = inet->sport;
 
@@ -383,6 +384,7 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 			ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
 			ipv6_addr_copy(&fl.fl6_src, &np->saddr);
 			fl.oif = sk->sk_bound_dev_if;
+			fl.mark = sk->sk_mark;
 			fl.fl_ip_dport = inet->dport;
 			fl.fl_ip_sport = inet->sport;
 			security_skb_classify_flow(skb, &fl);
@@ -477,6 +479,7 @@ static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req)
 	ipv6_addr_copy(&fl.fl6_src, &treq->loc_addr);
 	fl.fl6_flowlabel = 0;
 	fl.oif = treq->iif;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = inet_rsk(req)->rmt_port;
 	fl.fl_ip_sport = inet_rsk(req)->loc_port;
 	security_req_classify_flow(req, &fl);
@@ -1345,6 +1348,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
 		}
 		ipv6_addr_copy(&fl.fl6_src, &treq->loc_addr);
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 		fl.fl_ip_dport = inet_rsk(req)->rmt_port;
 		fl.fl_ip_sport = inet_rsk(req)->loc_port;
 		security_req_classify_flow(req, &fl);
net/ipv6/udp.c +2 -0
index 3a60f12..3842c55 100644
@@ -879,6 +879,8 @@ do_udp_sendmsg:
 	if (!fl.oif)
 		fl.oif = np->sticky_pktinfo.ipi6_ifindex;
 
+	fl.mark = sk->sk_mark;
+
 	if (msg->msg_controllen) {
 		opt = &opt_space;
 		memset(opt, 0, sizeof(struct ipv6_txoptions));
author
#8 lore raw

Re: [PATCH] Use sk_mark for IPv6 routing lookups

Quoted 12 lines · On Friday 02 October 2009 21:19:05 Brian Haley wrote:
Quoted 5 lines · Maciej Żenczykowski

Cool!

As I've already pointed out in a post 2 or so weeks ago, we need the exact same treatment in a ton of places throughout the code (tcp, ipv6, decnet, etc...).

Here's a try at the IPv6 part...

Add support for IPv6 route lookups using sk_mark.

Not sure if there is need to fill the mark from skb in tunnel xmit functions. In any case, it's not done for GRE or IPIP tunnels at the moment.

Also, in this patch you are doing that for SIT (v6-in-v4) tunnels only, and not doing it for v4-in-v6 or v6-in-v6 tunnels. Any reason for that?

Atis

#9 lore raw

Re: [PATCH] Use sk_mark for IPv6 routing lookups

Quoted 1 line · Atis Elsts

Not sure if there is need to fill the mark from skb in tunnel xmit functions. In any case, it's not done for GRE or IPIP tunnels at the moment.

Ok, I'll just drop that part, I'm not sure what should be done in this case.

Quoted 1 line

Also, in this patch you are doing that for SIT (v6-in-v4) tunnels only, and not doing it for v4-in-v6 or v6-in-v6 tunnels. Any reason for that?

I just sent that patch out too quickly, here's a better one with the updates.

Add support for IPv6 route lookups using sk_mark.

---

net/ipv6/af_inet6.c +1 -0
index e127a32..da36497 100644
@@ -654,6 +654,7 @@ int inet6_sk_rebuild_header(struct sock *sk)
 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
 		fl.fl6_flowlabel = np->flow_label;
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 		fl.fl_ip_dport = inet->dport;
 		fl.fl_ip_sport = inet->sport;
 		security_sk_classify_flow(sk, &fl);
net/ipv6/datagram.c +1 -0
index e2bdc6d..a615b4d 100644
@@ -147,6 +147,7 @@ ipv4_connected:
 	ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
 	ipv6_addr_copy(&fl.fl6_src, &np->saddr);
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = inet->dport;
 	fl.fl_ip_sport = inet->sport;
 
net/ipv6/inet6_connection_sock.c +1 -0
index cc4797d..a9f4a21 100644
@@ -194,6 +194,7 @@ int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
 	fl.fl6_flowlabel = np->flow_label;
 	IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_sport = inet->sport;
 	fl.fl_ip_dport = inet->dport;
 	security_sk_classify_flow(sk, &fl);
net/ipv6/ipv6_sockglue.c +1 -0
index 14f54eb..dc0f736 100644
@@ -424,6 +424,7 @@ sticky_done:
 
 		fl.fl6_flowlabel = 0;
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 
 		if (optlen == 0)
 			goto update;
net/ipv6/syncookies.c +1 -0
index 6b6ae91..cbe55e5 100644
@@ -252,6 +252,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 		}
 		ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 		fl.fl_ip_dport = inet_rsk(req)->rmt_port;
 		fl.fl_ip_sport = inet_sk(sk)->sport;
 		security_req_classify_flow(req, &fl);
net/ipv6/tcp_ipv6.c +4 -0
index 21d100b..321aafd 100644
@@ -243,6 +243,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
 	ipv6_addr_copy(&fl.fl6_src,
 		       (saddr ? saddr : &np->saddr));
 	fl.oif = sk->sk_bound_dev_if;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = usin->sin6_port;
 	fl.fl_ip_sport = inet->sport;
 
@@ -383,6 +384,7 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 			ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
 			ipv6_addr_copy(&fl.fl6_src, &np->saddr);
 			fl.oif = sk->sk_bound_dev_if;
+			fl.mark = sk->sk_mark;
 			fl.fl_ip_dport = inet->dport;
 			fl.fl_ip_sport = inet->sport;
 			security_skb_classify_flow(skb, &fl);
@@ -477,6 +479,7 @@ static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req)
 	ipv6_addr_copy(&fl.fl6_src, &treq->loc_addr);
 	fl.fl6_flowlabel = 0;
 	fl.oif = treq->iif;
+	fl.mark = sk->sk_mark;
 	fl.fl_ip_dport = inet_rsk(req)->rmt_port;
 	fl.fl_ip_sport = inet_rsk(req)->loc_port;
 	security_req_classify_flow(req, &fl);
@@ -1345,6 +1348,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
 		}
 		ipv6_addr_copy(&fl.fl6_src, &treq->loc_addr);
 		fl.oif = sk->sk_bound_dev_if;
+		fl.mark = sk->sk_mark;
 		fl.fl_ip_dport = inet_rsk(req)->rmt_port;
 		fl.fl_ip_sport = inet_rsk(req)->loc_port;
 		security_req_classify_flow(req, &fl);
net/ipv6/udp.c +2 -0
index 3a60f12..3842c55 100644
@@ -879,6 +879,8 @@ do_udp_sendmsg:
 	if (!fl.oif)
 		fl.oif = np->sticky_pktinfo.ipi6_ifindex;
 
+	fl.mark = sk->sk_mark;
+
 	if (msg->msg_controllen) {
 		opt = &opt_space;
 		memset(opt, 0, sizeof(struct ipv6_txoptions));