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