baa58b7db9872e06f38420542e7cd10e02d571a2
[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                 /* 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                                 /* GSO can change the checksum type so update.*/
148                                 compute_ip_summed(segs, true);
149
150                                 segs = __vlan_put_tag(segs, ntohs(tci));
151                                 err = -ENOMEM;
152                                 if (segs) {
153                                         struct xflow_key segkey = *key;
154                                         err = execute_actions(dp, segs,
155                                                               &segkey, a + 1,
156                                                               n_actions - 1,
157                                                               gfp);
158                                 }
159
160                                 if (unlikely(err)) {
161                                         while ((segs = nskb)) {
162                                                 nskb = segs->next;
163                                                 segs->next = NULL;
164                                                 kfree_skb(segs);
165                                         }
166                                         return ERR_PTR(err);
167                                 }
168
169                                 segs = nskb;
170                         } while (segs->next);
171
172                         skb = segs;
173                         compute_ip_summed(skb, true);
174                 }
175
176                 /* The hardware-accelerated version of vlan_put_tag() works
177                  * only for a device that has a VLAN group configured (with
178                  * e.g. vconfig(8)), so call the software-only version
179                  * __vlan_put_tag() directly instead.
180                  */
181                 skb = __vlan_put_tag(skb, ntohs(tci));
182                 if (!skb)
183                         return ERR_PTR(-ENOMEM);
184
185                 /* GSO doesn't fix up the hardware computed checksum so this
186                  * will only be hit in the non-GSO case. */
187                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
188                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
189                                                 + ETH_HLEN, VLAN_HLEN, 0));
190         }
191
192         return skb;
193 }
194
195 static struct sk_buff *strip_vlan(struct sk_buff *skb,
196                                   struct xflow_key *key, gfp_t gfp)
197 {
198         skb = make_writable(skb, 0, gfp);
199         if (skb) {
200                 vlan_pull_tag(skb);
201                 key->dl_tci = htons(0);
202         }
203         return skb;
204 }
205
206 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
207                                    struct xflow_key *key,
208                                    const struct xflow_action_dl_addr *a,
209                                    gfp_t gfp)
210 {
211         skb = make_writable(skb, 0, gfp);
212         if (skb) {
213                 struct ethhdr *eh = eth_hdr(skb);
214                 if (a->type == XFLOWAT_SET_DL_SRC) {
215                         memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
216                         memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
217                 } else {
218                         memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
219                         memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
220                 }
221         }
222         return skb;
223 }
224
225 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
226  * covered by the sum has been changed from 'from' to 'to'.  If set,
227  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
228  * Based on nf_proto_csum_replace4. */
229 static void update_csum(__sum16 *sum, struct sk_buff *skb,
230                         __be32 from, __be32 to, int pseudohdr)
231 {
232         __be32 diff[] = { ~from, to };
233
234         if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
235                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
236                                 ~csum_unfold(*sum)));
237                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
238                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
239                                                 ~skb->csum);
240         } else if (pseudohdr)
241                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
242                                 csum_unfold(*sum)));
243 }
244
245 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
246                                    struct xflow_key *key,
247                                    const struct xflow_action_nw_addr *a,
248                                    gfp_t gfp)
249 {
250         if (key->dl_type != htons(ETH_P_IP))
251                 return skb;
252
253         skb = make_writable(skb, 0, gfp);
254         if (skb) {
255                 struct iphdr *nh = ip_hdr(skb);
256                 u32 *f = a->type == XFLOWAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
257                 u32 old = *f;
258                 u32 new = a->nw_addr;
259
260                 if (key->nw_proto == IPPROTO_TCP) {
261                         struct tcphdr *th = tcp_hdr(skb);
262                         update_csum(&th->check, skb, old, new, 1);
263                 } else if (key->nw_proto == IPPROTO_UDP) {
264                         struct udphdr *th = udp_hdr(skb);
265                         update_csum(&th->check, skb, old, new, 1);
266                 }
267                 update_csum(&nh->check, skb, old, new, 0);
268                 *f = new;
269
270                 if (a->type == XFLOWAT_SET_NW_SRC)
271                         key->nw_src = a->nw_addr;
272                 else
273                         key->nw_dst = a->nw_addr;
274         }
275         return skb;
276 }
277
278 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
279                                    struct xflow_key *key,
280                                    const struct xflow_action_nw_tos *a,
281                                    gfp_t gfp)
282 {
283         if (key->dl_type != htons(ETH_P_IP))
284                 return skb;
285
286         skb = make_writable(skb, 0, gfp);
287         if (skb) {
288                 struct iphdr *nh = ip_hdr(skb);
289                 u8 *f = &nh->tos;
290                 u8 old = *f;
291                 u8 new;
292
293                 /* Set the DSCP bits and preserve the ECN bits. */
294                 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
295                 update_csum(&nh->check, skb, htons((uint16_t)old),
296                                 htons((uint16_t)new), 0);
297                 *f = new;
298                 key->nw_tos = a->nw_tos;
299         }
300         return skb;
301 }
302
303 static struct sk_buff *
304 set_tp_port(struct sk_buff *skb, struct xflow_key *key,
305             const struct xflow_action_tp_port *a,
306             gfp_t gfp)
307 {
308         int check_ofs;
309
310         if (key->dl_type != htons(ETH_P_IP))
311                 return skb;
312
313         if (key->nw_proto == IPPROTO_TCP)
314                 check_ofs = offsetof(struct tcphdr, check);
315         else if (key->nw_proto == IPPROTO_UDP)
316                 check_ofs = offsetof(struct udphdr, check);
317         else
318                 return skb;
319
320         skb = make_writable(skb, 0, gfp);
321         if (skb) {
322                 struct udphdr *th = udp_hdr(skb);
323                 u16 *f = a->type == XFLOWAT_SET_TP_SRC ? &th->source : &th->dest;
324                 u16 old = *f;
325                 u16 new = a->tp_port;
326                 update_csum((u16*)(skb_transport_header(skb) + check_ofs), 
327                                 skb, old, new, 0);
328                 *f = new;
329                 if (a->type == XFLOWAT_SET_TP_SRC)
330                         key->tp_src = a->tp_port;
331                 else
332                         key->tp_dst = a->tp_port;
333         }
334         return skb;
335 }
336
337 static inline unsigned packet_length(const struct sk_buff *skb)
338 {
339         unsigned length = skb->len - ETH_HLEN;
340         if (skb->protocol == htons(ETH_P_8021Q))
341                 length -= VLAN_HLEN;
342         return length;
343 }
344
345 static void
346 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
347 {
348         struct dp_port *p;
349         int mtu;
350
351         if (!skb)
352                 goto error;
353
354         p = rcu_dereference(dp->ports[out_port]);
355         if (!p)
356                 goto error;
357
358         mtu = vport_get_mtu(p->vport);
359         if (packet_length(skb) > mtu && !skb_is_gso(skb)) {
360                 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
361                        dp_name(dp), packet_length(skb), mtu);
362                 goto error;
363         }
364
365         vport_send(p->vport, skb);
366         return;
367
368 error:
369         kfree_skb(skb);
370 }
371
372 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
373  * none.  */
374 static int output_group(struct datapath *dp, __u16 group,
375                         struct sk_buff *skb, gfp_t gfp)
376 {
377         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
378         int prev_port = -1;
379         int i;
380
381         if (!g)
382                 return -1;
383         for (i = 0; i < g->n_ports; i++) {
384                 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
385                 if (!p || OVS_CB(skb)->dp_port == p)
386                         continue;
387                 if (prev_port != -1) {
388                         struct sk_buff *clone = skb_clone(skb, gfp);
389                         if (!clone)
390                                 return -1;
391                         do_output(dp, clone, prev_port);
392                 }
393                 prev_port = p->port_no;
394         }
395         return prev_port;
396 }
397
398 static int
399 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
400 {
401         skb = skb_clone(skb, gfp);
402         if (!skb)
403                 return -ENOMEM;
404         return dp_output_control(dp, skb, _XFLOWL_ACTION_NR, arg);
405 }
406
407 /* Send a copy of this packet up to the sFlow agent, along with extra
408  * information about what happened to it. */
409 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
410                          const union xflow_action *a, int n_actions,
411                          gfp_t gfp, struct dp_port *dp_port)
412 {
413         struct xflow_sflow_sample_header *hdr;
414         unsigned int actlen = n_actions * sizeof(union xflow_action);
415         unsigned int hdrlen = sizeof(struct xflow_sflow_sample_header);
416         struct sk_buff *nskb;
417
418         nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
419         if (!nskb)
420                 return;
421
422         memcpy(__skb_push(nskb, actlen), a, actlen);
423         hdr = (struct xflow_sflow_sample_header*)__skb_push(nskb, hdrlen);
424         hdr->n_actions = n_actions;
425         hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
426         dp_output_control(dp, nskb, _XFLOWL_SFLOW_NR, 0);
427 }
428
429 /* Execute a list of actions against 'skb'. */
430 int execute_actions(struct datapath *dp, struct sk_buff *skb,
431                     struct xflow_key *key,
432                     const union xflow_action *a, int n_actions,
433                     gfp_t gfp)
434 {
435         /* Every output action needs a separate clone of 'skb', but the common
436          * case is just a single output action, so that doing a clone and
437          * then freeing the original skbuff is wasteful.  So the following code
438          * is slightly obscure just to avoid that. */
439         int prev_port = -1;
440         int err;
441
442         if (dp->sflow_probability) {
443                 struct dp_port *p = OVS_CB(skb)->dp_port;
444                 if (p) {
445                         atomic_inc(&p->sflow_pool);
446                         if (dp->sflow_probability == UINT_MAX ||
447                             net_random() < dp->sflow_probability)
448                                 sflow_sample(dp, skb, a, n_actions, gfp, p);
449                 }
450         }
451
452         OVS_CB(skb)->tun_id = 0;
453
454         for (; n_actions > 0; a++, n_actions--) {
455                 WARN_ON_ONCE(skb_shared(skb));
456                 if (prev_port != -1) {
457                         do_output(dp, skb_clone(skb, gfp), prev_port);
458                         prev_port = -1;
459                 }
460
461                 switch (a->type) {
462                 case XFLOWAT_OUTPUT:
463                         prev_port = a->output.port;
464                         break;
465
466                 case XFLOWAT_OUTPUT_GROUP:
467                         prev_port = output_group(dp, a->output_group.group,
468                                                  skb, gfp);
469                         break;
470
471                 case XFLOWAT_CONTROLLER:
472                         err = output_control(dp, skb, a->controller.arg, gfp);
473                         if (err) {
474                                 kfree_skb(skb);
475                                 return err;
476                         }
477                         break;
478
479                 case XFLOWAT_SET_TUNNEL:
480                         set_tunnel(skb, key, a->tunnel.tun_id);
481                         break;
482
483                 case XFLOWAT_SET_DL_TCI:
484                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
485                         if (IS_ERR(skb))
486                                 return PTR_ERR(skb);
487                         break;
488
489                 case XFLOWAT_STRIP_VLAN:
490                         skb = strip_vlan(skb, key, gfp);
491                         break;
492
493                 case XFLOWAT_SET_DL_SRC:
494                 case XFLOWAT_SET_DL_DST:
495                         skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
496                         break;
497
498                 case XFLOWAT_SET_NW_SRC:
499                 case XFLOWAT_SET_NW_DST:
500                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
501                         break;
502
503                 case XFLOWAT_SET_NW_TOS:
504                         skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
505                         break;
506
507                 case XFLOWAT_SET_TP_SRC:
508                 case XFLOWAT_SET_TP_DST:
509                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
510                         break;
511                 }
512                 if (!skb)
513                         return -ENOMEM;
514         }
515         if (prev_port != -1)
516                 do_output(dp, skb, prev_port);
517         else
518                 kfree_skb(skb);
519         return 0;
520 }