Skip to contentSkip to conversation

ipvs: enable the scheduling of icmp packets

RFC PATCH In review Alex Gartrell 3 participants 4 messages Aug 10, 2015 – Aug 11, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ipvs: enable the scheduling of icmp packets

If the schedule_icmp sysctl is set, ICMP messages for unseen flows will be handed off to the appropriate schedulers. This is very necessary for us at Facebook, because we ECMP load balance down to the IPVS instances and rely on a deterministic scheduler rather than sync'ing, so a TCP SYN and an ICMPv6 PACKET_TOO_BIG packet are unlikely to end up on the same ipvs host.

This patch introduces the following behavior changes:

1) We no longer check for !th->rst in the sloppy_tcp schedule case

Why? First, it's useful to schedule these packets anyway (the assumption being that we want to push the state all the way down to the actual TCP terminator in this case). Second, this bit is not present in IPv4 ICMP packets, and the alternative is to also plumb an "is_icmp" field all the way through.

2) Packets that do not contain full transport headers are not rejected for udp, sloppy tcp, or sloppy sctp. It was simply cleaner to implement this way.

---
 include/net/ip_vs.h                     |  15 ++++-
 net/netfilter/ipvs/ip_vs_core.c         | 107 +++++++++++++++++++++++---------
 net/netfilter/ipvs/ip_vs_ctl.c          |   7 +++
 net/netfilter/ipvs/ip_vs_proto_ah_esp.c |   2 +-
 net/netfilter/ipvs/ip_vs_proto_sctp.c   |  47 +++++++++-----
 net/netfilter/ipvs/ip_vs_proto_tcp.c    |  47 +++++++++++---
 net/netfilter/ipvs/ip_vs_proto_udp.c    |  23 ++++---
 7 files changed, 181 insertions(+), 67 deletions(-)
include/net/ip_vs.h +13 -2
index 4e3731e..8d7420d 100644
@@ -444,7 +444,7 @@ struct ip_vs_protocol {
 	int (*conn_schedule)(int af, struct sk_buff *skb,
 			     struct ip_vs_proto_data *pd,
 			     int *verdict, struct ip_vs_conn **cpp,
-			     struct ip_vs_iphdr *iph);
+			     struct ip_vs_iphdr *iph, int inverse);
 
 	struct ip_vs_conn *
 	(*conn_in_get)(int af,
@@ -942,6 +942,7 @@ struct netns_ipvs {
 	int			sysctl_pmtu_disc;
 	int			sysctl_backup_only;
 	int			sysctl_conn_reuse_mode;
+	int			sysctl_schedule_icmp;
 
 	/* ip_vs_lblc */
 	int			sysctl_lblc_expiration;
@@ -1065,6 +1066,11 @@ static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
 	return ipvs->sysctl_conn_reuse_mode;
 }
 
+static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
+{
+	return ipvs->sysctl_schedule_icmp;
+}
+
 #else
 
 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
@@ -1137,6 +1143,11 @@ static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
 	return 1;
 }
 
+static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
+{
+	return 0;
+}
+
 #endif
 
 /* IPVS core functions
@@ -1358,7 +1369,7 @@ void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
 struct ip_vs_conn *
 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
 	       struct ip_vs_proto_data *pd, int *ignored,
-	       struct ip_vs_iphdr *iph);
+	       struct ip_vs_iphdr *iph, int inverse);
 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
 		struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
 
net/netfilter/ipvs/ip_vs_core.c +76 -31

196 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_ctl.c +7 -0
index 24c5542..3e0657d 100644
@@ -1844,6 +1844,12 @@ static struct ctl_table vs_vars[] = {
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec,
 	},
+	{
+		.procname	= "schedule_icmp",
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
 #ifdef CONFIG_IP_VS_DEBUG
 	{
 		.procname	= "debug_level",
@@ -3818,6 +3824,7 @@ static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
 	tbl[idx++].data = &ipvs->sysctl_backup_only;
 	ipvs->sysctl_conn_reuse_mode = 1;
 	tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
+	tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
 
 
 	ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
net/netfilter/ipvs/ip_vs_proto_ah_esp.c +1 -1
index 5de3dd3..31c0b20 100644
@@ -109,7 +109,7 @@ ah_esp_conn_out_get(int af, const struct sk_buff *skb,
 static int
 ah_esp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		     int *verdict, struct ip_vs_conn **cpp,
-		     struct ip_vs_iphdr *iph)
+		     struct ip_vs_iphdr *iph, int is_icmp)
 {
 	/*
 	 * AH/ESP is only related traffic. Pass the packet to IP stack.
net/netfilter/ipvs/ip_vs_proto_sctp.c +31 -16

71 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_proto_tcp.c +37 -10

69 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_proto_udp.c +16 -7
index b62a3c0..7417fc2 100644
@@ -31,22 +31,31 @@
 static int
 udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		  int *verdict, struct ip_vs_conn **cpp,
-		  struct ip_vs_iphdr *iph)
+		  struct ip_vs_iphdr *iph, int inverse)
 {
 	struct net *net;
 	struct ip_vs_service *svc;
-	struct udphdr _udph, *uh;
+	__be16 _ports[2];
+	__be16 *ports = NULL;
+
+	ports = skb_header_pointer(
+		skb, iph->len, sizeof(_ports), &_ports);
 
 	/* IPv6 fragments, only first fragment will hit this */
-	uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph);
-	if (uh == NULL) {
+	if (!ports) {
 		*verdict = NF_DROP;
 		return 0;
 	}
 	net = skb_net(skb);
 	rcu_read_lock();
-	svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
-				 &iph->daddr, uh->dest);
+
+	if (inverse)
+		svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
+					 &iph->saddr, ports[0]);
+	else
+		svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
+					 &iph->daddr, ports[1]);
+
 	if (svc) {
 		int ignored;
 
@@ -64,7 +73,7 @@ udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		 * Let the virtual server select a real server for the
 		 * incoming connection, and create a connection entry.
 		 */
-		*cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph);
+		*cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph, inverse);
 		if (!*cpp && ignored <= 0) {
 			if (!ignored)
 				*verdict = ip_vs_leave(svc, skb, pd, iph);
Signature · 1 line
Alex Gartrell <[email protected]>
maintainer
#1 lore raw

Hello,

Quoted 5 lines · Alex Gartrell

If the schedule_icmp sysctl is set, ICMP messages for unseen flows will be handed off to the appropriate schedulers. This is very necessary for us at Facebook, because we ECMP load balance down to the IPVS instances and rely on a deterministic scheduler rather than sync'ing, so a TCP SYN and an ICMPv6 PACKET_TOO_BIG packet are unlikely to end up on the same ipvs host.

You mean related ICMP comes to one of the directors
because it was scheduled randomly by routers and the IPVS scheduler will schedule it to the right real server even if the original TCP connection is on another director?

If I understand it correctly, this patch tries to
schedule related ICMP only, right? And it creates new connection, for example, from ICMP-TCP_data packet?

commit bba54de5bdd1 ("ipvs: provide iph to schedulers")
added iph to the schedule() method, now we need to provide also "bool inverse" ? Needed only for the SH scheduler... Another option is to add the inverse flag to struct ip_vs_iphdr. It will benefit functions like ip_vs_leave(), ->packet_xmit, anything that uses iph addresses.

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 423

Quoted 5 lines
net/netfilter/ipvs/ip_vs_core.c +1 -0
@@ -420,6 +420,7 @@ ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
 	struct ip_vs_dest *dest;
 	__be16 _ports[2], *pptr;
 	unsigned int flags;
+	__be16 client_port, svc_port;

sport and dport looks better to me

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 469

Quoted 5 lines
net/netfilter/ipvs/ip_vs_core.c +2 -2
 	if (svc->flags & IP_VS_SVC_F_PERSISTENT)
-		return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
-					   iph);
+		return ip_vs_sched_persist(svc, skb, client_port, svc_port,
+					   ignored, iph);

We will need inverse flag for ip_vs_sched_persist
because the addresses are reversed too :(

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 509

Quoted 5 lines
net/netfilter/ipvs/ip_vs_core.c +2 -2
 		ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
-				      &iph->saddr, pptr[0], &iph->daddr,
-				      pptr[1], &p);
+				      &iph->saddr, client_port, &iph->daddr,
+				      svc_port, &p);

Above addresses may be reversed too.

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,

Quoted 11 lines
net/netfilter/ipvs/ip_vs_core.c +1 -4
 		cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
-				    dest->port ? dest->port : pptr[1],
+				    dest->port ? dest->port : svc_port,
 				    flags, dest, skb->mark);
 		if (!cp) {
 			*ignored = -1;
 
 	rcu_read_lock();
-	if ((sch->type == SCTP_CID_INIT || sysctl_sloppy_sctp(ipvs)) &&
-	    (svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
-				      &iph->daddr, sh->dest))) {

So, the inverse flag should simply imply sloppy mode?
Because if sloppy mode is disabled below code will not schedule the ICMP for non-INIT packet... Even, we should skip the 'sch = skb_header_pointer' code if inverse=1. What we need is just the port to be present in skb when inverse=1.

Quoted 34 lines
net/netfilter/ipvs/ip_vs_proto_sctp.c +25 -1
+
+	if (!sysctl_sloppy_sctp(ipvs)) {
+		sctp_chunkhdr_t _schunkh, *sch;
+
+		sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
+					 sizeof(_schunkh), &_schunkh);
+		if (!sch) {
+			*verdict = NF_DROP;
+			return 0;
+		}
+		if (sch->type != SCTP_CID_INIT) {
+			*verdict = NF_ACCEPT;
+			return 0;
+		}
+	}
+
+	if (inverse)
+		svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
+					 &iph->saddr, ports[0]);
+	else
+		svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
+					 &iph->daddr, ports[1]);
+
+	if (svc) {
 		int ignored;
 
 tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		  int *verdict, struct ip_vs_conn **cpp,
-		  struct ip_vs_iphdr *iph)
+		  struct ip_vs_iphdr *iph, int inverse)
 {
 	struct net *net;
 	struct ip_vs_service *svc;
 	struct tcphdr _tcph, *th;

net/netfilter/ipvs/ip_vs_proto_sctp.c

Quoted 10 lines
net/netfilter/ipvs/ip_vs_proto_sctp.c +6 -2
 	struct netns_ipvs *ipvs;
+	__be16 _ports[2];
+	__be16 *ports = NULL;
 
-	th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
-	if (th == NULL) {
+	net = skb_net(skb);
+	ipvs = net_ipvs(net);
+
+	if (sysctl_sloppy_tcp(ipvs)) {
	if (sysctl_sloppy_tcp(ipvs) || inverse) {

because inverse=1 means embedded TCP packet can be with any flags.

Quoted 9 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +9 -0
+		/* We're going to schedule any tcp that looks even vaguely
+		 * like a tcp header (important for ICMP where many fields
+		 * may be lost)
+		 */
+		ports = skb_header_pointer(
+			skb, iph->len, sizeof(_ports), &_ports);
+	} else {
+		th = skb_header_pointer(
+			skb, iph->len, sizeof(_tcph), &_tcph);

net/netfilter/ipvs/ip_vs_proto_tcp.c line 68

Quoted 10 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +10 -0
+		if (th && th->syn && !th->rst) {
+			/* We are not scheduling sloppily and we seem to
+			 * have a valid SYN or SYN-ACK (for active FTP)
+			 * here.
+			 */
+			ports = &th->source;
+		}
+	}
+
+	if (!ports) {

Regards

--
Julian Anastasov <[email protected]>

#2 lore raw

Hey Julian,

Thank you for your comments!

Quoted 6 lines · Julian Anastasov
Quoted 5 lines

If the schedule_icmp sysctl is set, ICMP messages for unseen flows will be handed off to the appropriate schedulers. This is very necessary for us at Facebook, because we ECMP load balance down to the IPVS instances and rely on a deterministic scheduler rather than sync'ing, so a TCP SYN and an ICMPv6 PACKET_TOO_BIG packet are unlikely to end up on the same ipvs host.

Quoted 8 lines

You mean related ICMP comes to one of the directors
because it was scheduled randomly by routers and the IPVS scheduler will schedule it to the right real server even if the original TCP connection is on another director?

If I understand it correctly, this patch tries to
schedule related ICMP only, right? And it creates new connection, for example, from ICMP-TCP_data packet?

Yeah, I'll break it down into more patches than one (because this is a lot to review), but

Quoted 7 lines

commit bba54de5bdd1 ("ipvs: provide iph to schedulers")
added iph to the schedule() method, now we need to provide also "bool inverse" ? Needed only for the SH scheduler... Another option is to add the inverse flag to struct ip_vs_iphdr. It will benefit functions like ip_vs_leave(), ->packet_xmit, anything that uses iph addresses.

yeah, this was the original reason for my other patch which consolidates the iph filling. I was actually thinking that, rather than adding bool inverse, we should specify the type so that we don't end up in a situation where we also need sloppy to be true. I was waiting to see what you thought.

Quoted 7 lines

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 423

Quoted 5 lines
net/netfilter/ipvs/ip_vs_core.c +1 -0
@@ -420,6 +420,7 @@ ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
      struct ip_vs_dest *dest;
      __be16 _ports[2], *pptr;
      unsigned int flags;
+     __be16 client_port, svc_port;

sport and dport looks better to me

I'll incorporate this.

Quoted 9 lines

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 469

Quoted 6 lines
net/netfilter/ipvs/ip_vs_core.c +2 -2
      if (svc->flags & IP_VS_SVC_F_PERSISTENT)
-             return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
-                                        iph);
+             return ip_vs_sched_persist(svc, skb, client_port, svc_port,
+                                        ignored, iph);

We will need inverse flag for ip_vs_sched_persist
because the addresses are reversed too :(

Ah, of course.

Quoted 8 lines

net/netfilter/ipvs/ip_vs_core.c ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, · line 509

Quoted 6 lines
              ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
net/netfilter/ipvs/ip_vs_core.c +2 -2
-                                   &iph->saddr, pptr[0], &iph->daddr,
-                                   pptr[1], &p);
+                                   &iph->saddr, client_port, &iph->daddr,
+                                   svc_port, &p);

Above addresses may be reversed too.

Duh, I've got some python tests and I'll update them so that the real deal will be fully tested, and then we should find a way to include the tests with upstream to save future modifiers the effort.

Quoted 10 lines

net/netfilter/ipvs/ip_vs_proto_sctp.c line 40

Quoted 3 lines
net/netfilter/ipvs/ip_vs_proto_sctp.c +0 -3
-     if ((sch->type == SCTP_CID_INIT || sysctl_sloppy_sctp(ipvs)) &&
-         (svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
-                                   &iph->daddr, sh->dest))) {

So, the inverse flag should simply imply sloppy mode?
Because if sloppy mode is disabled below code will not schedule the ICMP for non-INIT packet... Even, we should skip the 'sch = skb_header_pointer' code if inverse=1. What we need is just the port to be present in skb when inverse=1.

Yeah I was thinking about this. I think that, rather than adding a bool field for inverse to iph, we should add some kind of type field. This field will be one of IP_VS_IPHDR_NORMAL, IP_VS_IPHDR_INVERSE, IP_VS_IPHDR_ICMP, IP_VS_IPHDR_ICMP_INVERSE. That way was can take just the ports whenever we're looking for icmp. This is probably good anyway, because even in ICMPv6 where we have all of the fields, if we intend to schedule all ICMP messages, we don't care if the SYN flag is set or not.

Quoted 6 lines

net/netfilter/ipvs/ip_vs_proto_tcp.c line 49

Quoted 2 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +2 -0
+
+     if (sysctl_sloppy_tcp(ipvs)) {
        if (sysctl_sloppy_tcp(ipvs) || inverse) {

because inverse=1 means embedded TCP packet can be with any flags.

Yeah I thought about this and my concern is when we have ICMP with ipip=true. It's probably the case that that never happens unless there's something scheduled, but should we call it out explicitly in a comment or something?

Quoted 3 lines

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

Signature · 1 line
Alex Gartrell <[email protected]>
maintainer
#3 lore raw
In reply to Alex Gartrell

Hello,

More notes for today:

- ip_vs_in_icmp: lets add a bool flag (local var) if cp was created from the ip_vs_try_to_schedule call, I prefer if cp was present to call __ip_vs_conn_put:

	bool old = true;

	...
		if (!ip_vs_try_to_schedule(AF_INET, skb, pd, &v, &cp,
					   &ciph, !ipip))
			return v;
		old = false;
	}

	if (old)
		__ip_vs_conn_put(cp);
	else
		ip_vs_conn_put(cp);

- same for ip_vs_in_icmp_v6, we can use single place where to call [__]ip_vs_conn_put, now they are 2 places

- ensure_mtu_is_adequate:
- ipvsh->fragoffs can be for embedded header

- ip_vs_bypass_xmit_v6: uses ipvsh->daddr.in6 but it should use

	struct ipv6hdr *iph = ipv6_hdr(skb);

- we need a new flag in struct ip_vs_iphdr:
- embedded:
- we are working with embedded header
- this is needed by:
- ensure_mtu_is_adequate to avoid sending ICMP error as response to ICMP error
- schedule path: to avoid many checks for inverse flag

- ip_vs_in_icmp* can call ip_vs_fill_* with new arg 'inverse', so that the scheduling path see in ipvsh->saddr always the client address. The SH scheduler can know that addresses and ports are reversed by checking the embedded flag which implies 'inverse' mode. Then only ip_vs_sh_get_port should check this flag. Such flag allows schedulers to refuse to work for embedded packets, for example, if they schedule by payload.

Regards

--
Julian Anastasov <[email protected]>