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