datapath: Consistently maintain checksum offloading state.
[sliver-openvswitch.git] / datapath / actions.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for executing flow actions. */
10
11 #include <linux/skbuff.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/if_vlan.h>
18 #include <net/inet_ecn.h>
19 #include <net/ip.h>
20 #include <net/checksum.h>
21 #include "datapath.h"
22 #include "dp_dev.h"
23 #include "actions.h"
24 #include "openvswitch/datapath-protocol.h"
25
26 static struct sk_buff *
27 make_writable(struct sk_buff *skb, unsigned min_headroom, gfp_t gfp)
28 {
29         if (skb_shared(skb) || skb_cloned(skb)) {
30                 struct sk_buff *nskb;
31                 unsigned headroom = max(min_headroom, skb_headroom(skb));
32
33                 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), gfp);
34                 if (nskb) {
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
36                         /* Before 2.6.24 these fields were not copied when
37                          * doing an skb_copy_expand. */
38                         nskb->ip_summed = skb->ip_summed;
39                         nskb->csum = skb->csum;
40 #endif
41 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
42                         /* These fields are copied in skb_clone but not in
43                          * skb_copy or related functions.  We need to manually
44                          * copy them over here. */
45                         nskb->proto_data_valid = skb->proto_data_valid;
46                         nskb->proto_csum_blank = skb->proto_csum_blank;
47 #endif
48                         kfree_skb(skb);
49                         return nskb;
50                 }
51         } else {
52                 unsigned int hdr_len = (skb_transport_offset(skb)
53                                         + sizeof(struct tcphdr));
54                 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
55                         return skb;
56         }
57         kfree_skb(skb);
58         return NULL;
59 }
60
61
62 static struct sk_buff *
63 vlan_pull_tag(struct sk_buff *skb)
64 {
65         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
66         struct ethhdr *eh;
67
68
69         /* Verify we were given a vlan packet */
70         if (vh->h_vlan_proto != htons(ETH_P_8021Q))
71                 return skb;
72
73         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
74
75         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
76
77         skb->protocol = eh->h_proto;
78         skb->mac_header += VLAN_HLEN;
79
80         return skb;
81 }
82
83
84 static struct sk_buff *
85 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
86                 struct odp_flow_key *key, const union odp_action *a,
87                 int n_actions, gfp_t gfp)
88 {
89         u16 tci, mask;
90
91         if (a->type == ODPAT_SET_VLAN_VID) {
92                 tci = ntohs(a->vlan_vid.vlan_vid);
93                 mask = VLAN_VID_MASK;
94                 key->dl_vlan = htons(tci & mask);
95         } else {
96                 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
97                 mask = VLAN_PCP_MASK;
98         }
99
100         skb = make_writable(skb, VLAN_HLEN, gfp);
101         if (!skb)
102                 return ERR_PTR(-ENOMEM);
103
104         if (skb->protocol == htons(ETH_P_8021Q)) {
105                 /* Modify vlan id, but maintain other TCI values */
106                 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
107                 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
108         } else {
109                 /* Add vlan header */
110
111                 /* Set up checksumming pointers for checksum-deferred packets
112                  * on Xen.  Otherwise, dev_queue_xmit() will try to do this
113                  * when we send the packet out on the wire, and it will fail at
114                  * that point because skb_checksum_setup() will not look inside
115                  * an 802.1Q header. */
116                 vswitch_skb_checksum_setup(skb);
117
118                 /* GSO is not implemented for packets with an 802.1Q header, so
119                  * we have to do segmentation before we add that header.
120                  *
121                  * GSO does work with hardware-accelerated VLAN tagging, but we
122                  * can't use hardware-accelerated VLAN tagging since it
123                  * requires the device to have a VLAN group configured (with
124                  * e.g. vconfig(8)) and we don't do that.
125                  *
126                  * Having to do this here may be a performance loss, since we
127                  * can't take advantage of TSO hardware support, although it
128                  * does not make a measurable network performance difference
129                  * for 1G Ethernet.  Fixing that would require patching the
130                  * kernel (either to add GSO support to the VLAN protocol or to
131                  * support hardware-accelerated VLAN tagging without VLAN
132                  * groups configured). */
133                 if (skb_is_gso(skb)) {
134                         struct sk_buff *segs;
135
136                         segs = skb_gso_segment(skb, 0);
137                         kfree_skb(skb);
138                         if (unlikely(IS_ERR(segs)))
139                                 return ERR_CAST(segs);
140
141                         do {
142                                 struct sk_buff *nskb = segs->next;
143                                 int err;
144
145                                 segs->next = NULL;
146
147                                 segs = __vlan_put_tag(segs, tci);
148                                 err = -ENOMEM;
149                                 if (segs) {
150                                         struct odp_flow_key segkey = *key;
151                                         err = execute_actions(dp, segs,
152                                                               &segkey, a + 1,
153                                                               n_actions - 1,
154                                                               gfp);
155                                 }
156
157                                 if (unlikely(err)) {
158                                         while ((segs = nskb)) {
159                                                 nskb = segs->next;
160                                                 segs->next = NULL;
161                                                 kfree_skb(segs);
162                                         }
163                                         return ERR_PTR(err);
164                                 }
165
166                                 segs = nskb;
167                         } while (segs->next);
168
169                         skb = segs;
170                 }
171
172                 /* The hardware-accelerated version of vlan_put_tag() works
173                  * only for a device that has a VLAN group configured (with
174                  * e.g. vconfig(8)), so call the software-only version
175                  * __vlan_put_tag() directly instead.
176                  */
177                 skb = __vlan_put_tag(skb, tci);
178                 if (!skb)
179                         return ERR_PTR(-ENOMEM);
180         }
181
182         return skb;
183 }
184
185 static struct sk_buff *strip_vlan(struct sk_buff *skb,
186                                   struct odp_flow_key *key, gfp_t gfp)
187 {
188         skb = make_writable(skb, 0, gfp);
189         if (skb) {
190                 vlan_pull_tag(skb);
191                 key->dl_vlan = htons(ODP_VLAN_NONE);
192         }
193         return skb;
194 }
195
196 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
197                                    const struct odp_action_dl_addr *a,
198                                    gfp_t gfp)
199 {
200         skb = make_writable(skb, 0, gfp);
201         if (skb) {
202                 struct ethhdr *eh = eth_hdr(skb);
203                 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
204                        a->dl_addr, ETH_ALEN);
205         }
206         return skb;
207 }
208
209 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
210  * covered by the sum has been changed from 'from' to 'to'.  If set,
211  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
212  * Based on nf_proto_csum_replace4. */
213 static void update_csum(__sum16 *sum, struct sk_buff *skb,
214                         __be32 from, __be32 to, int pseudohdr)
215 {
216         __be32 diff[] = { ~from, to };
217
218         if (OVS_CB(skb)->ip_summed != CSUM_PARTIAL) {
219                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
220                                 ~csum_unfold(*sum)));
221                 if (OVS_CB(skb)->ip_summed == CSUM_COMPLETE && pseudohdr)
222                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
223                                                 ~skb->csum);
224         } else if (pseudohdr)
225                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
226                                 csum_unfold(*sum)));
227 }
228
229 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
230                                    struct odp_flow_key *key,
231                                    const struct odp_action_nw_addr *a,
232                                    gfp_t gfp)
233 {
234         if (key->dl_type != htons(ETH_P_IP))
235                 return skb;
236
237         skb = make_writable(skb, 0, gfp);
238         if (skb) {
239                 struct iphdr *nh = ip_hdr(skb);
240                 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
241                 u32 old = *f;
242                 u32 new = a->nw_addr;
243
244                 if (key->nw_proto == IPPROTO_TCP) {
245                         struct tcphdr *th = tcp_hdr(skb);
246                         update_csum(&th->check, skb, old, new, 1);
247                 } else if (key->nw_proto == IPPROTO_UDP) {
248                         struct udphdr *th = udp_hdr(skb);
249                         update_csum(&th->check, skb, old, new, 1);
250                 }
251                 update_csum(&nh->check, skb, old, new, 0);
252                 *f = new;
253         }
254         return skb;
255 }
256
257 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
258                                    struct odp_flow_key *key,
259                                    const struct odp_action_nw_tos *a,
260                                    gfp_t gfp)
261 {
262         if (key->dl_type != htons(ETH_P_IP))
263                 return skb;
264
265         skb = make_writable(skb, 0, gfp);
266         if (skb) {
267                 struct iphdr *nh = ip_hdr(skb);
268                 u8 *f = &nh->tos;
269                 u8 old = *f;
270                 u8 new;
271
272                 /* Set the DSCP bits and preserve the ECN bits. */
273                 new = (a->nw_tos & ~INET_ECN_MASK) | (nh->tos & INET_ECN_MASK);
274                 update_csum(&nh->check, skb, htons((uint16_t)old),
275                                 htons((uint16_t)new), 0);
276                 *f = new;
277         }
278         return skb;
279 }
280
281 static struct sk_buff *
282 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
283             const struct odp_action_tp_port *a,
284             gfp_t gfp)
285 {
286         int check_ofs;
287
288         if (key->dl_type != htons(ETH_P_IP))
289                 return skb;
290
291         if (key->nw_proto == IPPROTO_TCP)
292                 check_ofs = offsetof(struct tcphdr, check);
293         else if (key->nw_proto == IPPROTO_UDP)
294                 check_ofs = offsetof(struct udphdr, check);
295         else
296                 return skb;
297
298         skb = make_writable(skb, 0, gfp);
299         if (skb) {
300                 struct udphdr *th = udp_hdr(skb);
301                 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
302                 u16 old = *f;
303                 u16 new = a->tp_port;
304                 update_csum((u16*)(skb_transport_header(skb) + check_ofs), 
305                                 skb, old, new, 0);
306                 *f = new;
307         }
308         return skb;
309 }
310
311 static inline unsigned packet_length(const struct sk_buff *skb)
312 {
313         unsigned length = skb->len - ETH_HLEN;
314         if (skb->protocol == htons(ETH_P_8021Q))
315                 length -= VLAN_HLEN;
316         return length;
317 }
318
319 int dp_xmit_skb(struct sk_buff *skb)
320 {
321         struct datapath *dp = skb->dev->br_port->dp;
322         int len = skb->len;
323
324         if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
325                 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
326                        dp_name(dp), packet_length(skb), skb->dev->mtu);
327                 kfree_skb(skb);
328                 return -E2BIG;
329         }
330
331         forward_ip_summed(skb);
332         dev_queue_xmit(skb);
333
334         return len;
335 }
336
337 static void
338 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
339 {
340         struct net_bridge_port *p;
341         struct net_device *dev;
342
343         if (!skb)
344                 goto error;
345
346         p = dp->ports[out_port];
347         if (!p)
348                 goto error;
349
350         dev = skb->dev = p->dev;
351         if (is_dp_dev(dev))
352                 dp_dev_recv(dev, skb);
353         else
354                 dp_xmit_skb(skb);
355         return;
356
357 error:
358         kfree_skb(skb);
359 }
360
361 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
362  * none.  */
363 static int output_group(struct datapath *dp, __u16 group,
364                         struct sk_buff *skb, gfp_t gfp)
365 {
366         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
367         int prev_port = -1;
368         int i;
369
370         if (!g)
371                 return -1;
372         for (i = 0; i < g->n_ports; i++) {
373                 struct net_bridge_port *p = dp->ports[g->ports[i]];
374                 if (!p || skb->dev == p->dev)
375                         continue;
376                 if (prev_port != -1) {
377                         struct sk_buff *clone = skb_clone(skb, gfp);
378                         if (!clone)
379                                 return -1;
380                         do_output(dp, clone, prev_port);
381                 }
382                 prev_port = p->port_no;
383         }
384         return prev_port;
385 }
386
387 static int
388 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
389 {
390         skb = skb_clone(skb, gfp);
391         if (!skb)
392                 return -ENOMEM;
393         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
394 }
395
396 /* Send a copy of this packet up to the sFlow agent, along with extra
397  * information about what happened to it. */
398 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
399                          const union odp_action *a, int n_actions,
400                          gfp_t gfp, struct net_bridge_port *nbp)
401 {
402         struct odp_sflow_sample_header *hdr;
403         unsigned int actlen = n_actions * sizeof(union odp_action);
404         unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
405         struct sk_buff *nskb;
406
407         nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
408         if (!nskb)
409                 return;
410
411         memcpy(__skb_push(nskb, actlen), a, actlen);
412         hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
413         hdr->n_actions = n_actions;
414         hdr->sample_pool = atomic_read(&nbp->sflow_pool);
415         dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
416 }
417
418 /* Execute a list of actions against 'skb'. */
419 int execute_actions(struct datapath *dp, struct sk_buff *skb,
420                     struct odp_flow_key *key,
421                     const union odp_action *a, int n_actions,
422                     gfp_t gfp)
423 {
424         /* Every output action needs a separate clone of 'skb', but the common
425          * case is just a single output action, so that doing a clone and
426          * then freeing the original skbuff is wasteful.  So the following code
427          * is slightly obscure just to avoid that. */
428         int prev_port = -1;
429         int err;
430
431         if (dp->sflow_probability) {
432                 struct net_bridge_port *p = skb->dev->br_port;
433                 if (p) {
434                         atomic_inc(&p->sflow_pool);
435                         if (dp->sflow_probability == UINT_MAX ||
436                             net_random() < dp->sflow_probability)
437                                 sflow_sample(dp, skb, a, n_actions, gfp, p);
438                 }
439         }
440
441         for (; n_actions > 0; a++, n_actions--) {
442                 WARN_ON_ONCE(skb_shared(skb));
443                 if (prev_port != -1) {
444                         do_output(dp, skb_clone(skb, gfp), prev_port);
445                         prev_port = -1;
446                 }
447
448                 switch (a->type) {
449                 case ODPAT_OUTPUT:
450                         prev_port = a->output.port;
451                         break;
452
453                 case ODPAT_OUTPUT_GROUP:
454                         prev_port = output_group(dp, a->output_group.group,
455                                                  skb, gfp);
456                         break;
457
458                 case ODPAT_CONTROLLER:
459                         err = output_control(dp, skb, a->controller.arg, gfp);
460                         if (err) {
461                                 kfree_skb(skb);
462                                 return err;
463                         }
464                         break;
465
466                 case ODPAT_SET_VLAN_VID:
467                 case ODPAT_SET_VLAN_PCP:
468                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
469                         if (IS_ERR(skb))
470                                 return PTR_ERR(skb);
471                         break;
472
473                 case ODPAT_STRIP_VLAN:
474                         skb = strip_vlan(skb, key, gfp);
475                         break;
476
477                 case ODPAT_SET_DL_SRC:
478                 case ODPAT_SET_DL_DST:
479                         skb = set_dl_addr(skb, &a->dl_addr, gfp);
480                         break;
481
482                 case ODPAT_SET_NW_SRC:
483                 case ODPAT_SET_NW_DST:
484                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
485                         break;
486
487                 case ODPAT_SET_NW_TOS:
488                         skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
489                         break;
490
491                 case ODPAT_SET_TP_SRC:
492                 case ODPAT_SET_TP_DST:
493                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
494                         break;
495                 }
496                 if (!skb)
497                         return -ENOMEM;
498         }
499         if (prev_port != -1)
500                 do_output(dp, skb, prev_port);
501         else
502                 kfree_skb(skb);
503         return 0;
504 }