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