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