Merge branch 'master' into next
[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 *
289 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
290             const struct odp_action_tp_port *a,
291             gfp_t gfp)
292 {
293         int check_ofs;
294
295         if (key->dl_type != htons(ETH_P_IP))
296                 return skb;
297
298         if (key->nw_proto == IPPROTO_TCP)
299                 check_ofs = offsetof(struct tcphdr, check);
300         else if (key->nw_proto == IPPROTO_UDP)
301                 check_ofs = offsetof(struct udphdr, check);
302         else
303                 return skb;
304
305         skb = make_writable(skb, 0, gfp);
306         if (skb) {
307                 struct udphdr *th = udp_hdr(skb);
308                 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
309                 u16 old = *f;
310                 u16 new = a->tp_port;
311                 update_csum((u16*)(skb_transport_header(skb) + check_ofs), 
312                                 skb, old, new, 0);
313                 *f = new;
314         }
315         return skb;
316 }
317
318 static inline unsigned packet_length(const struct sk_buff *skb)
319 {
320         unsigned length = skb->len - ETH_HLEN;
321         if (skb->protocol == htons(ETH_P_8021Q))
322                 length -= VLAN_HLEN;
323         return length;
324 }
325
326 int dp_xmit_skb(struct sk_buff *skb)
327 {
328         struct datapath *dp = skb->dev->br_port->dp;
329         int len = skb->len;
330
331         if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
332                 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
333                        dp_name(dp), packet_length(skb), skb->dev->mtu);
334                 kfree_skb(skb);
335                 return -E2BIG;
336         }
337
338         forward_ip_summed(skb);
339         dev_queue_xmit(skb);
340
341         return len;
342 }
343
344 static void
345 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
346 {
347         struct net_bridge_port *p;
348         struct net_device *dev;
349
350         if (!skb)
351                 goto error;
352
353         p = dp->ports[out_port];
354         if (!p)
355                 goto error;
356
357         dev = skb->dev = p->dev;
358         if (is_dp_dev(dev))
359                 dp_dev_recv(dev, skb);
360         else
361                 dp_xmit_skb(skb);
362         return;
363
364 error:
365         kfree_skb(skb);
366 }
367
368 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
369  * none.  */
370 static int output_group(struct datapath *dp, __u16 group,
371                         struct sk_buff *skb, gfp_t gfp)
372 {
373         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
374         int prev_port = -1;
375         int i;
376
377         if (!g)
378                 return -1;
379         for (i = 0; i < g->n_ports; i++) {
380                 struct net_bridge_port *p = dp->ports[g->ports[i]];
381                 if (!p || skb->dev == p->dev)
382                         continue;
383                 if (prev_port != -1) {
384                         struct sk_buff *clone = skb_clone(skb, gfp);
385                         if (!clone)
386                                 return -1;
387                         do_output(dp, clone, prev_port);
388                 }
389                 prev_port = p->port_no;
390         }
391         return prev_port;
392 }
393
394 static int
395 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
396 {
397         skb = skb_clone(skb, gfp);
398         if (!skb)
399                 return -ENOMEM;
400         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
401 }
402
403 /* Send a copy of this packet up to the sFlow agent, along with extra
404  * information about what happened to it. */
405 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
406                          const union odp_action *a, int n_actions,
407                          gfp_t gfp, struct net_bridge_port *nbp)
408 {
409         struct odp_sflow_sample_header *hdr;
410         unsigned int actlen = n_actions * sizeof(union odp_action);
411         unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
412         struct sk_buff *nskb;
413
414         nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
415         if (!nskb)
416                 return;
417
418         memcpy(__skb_push(nskb, actlen), a, actlen);
419         hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
420         hdr->n_actions = n_actions;
421         hdr->sample_pool = atomic_read(&nbp->sflow_pool);
422         dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
423 }
424
425 /* Execute a list of actions against 'skb'. */
426 int execute_actions(struct datapath *dp, struct sk_buff *skb,
427                     struct odp_flow_key *key,
428                     const union odp_action *a, int n_actions,
429                     gfp_t gfp)
430 {
431         /* Every output action needs a separate clone of 'skb', but the common
432          * case is just a single output action, so that doing a clone and
433          * then freeing the original skbuff is wasteful.  So the following code
434          * is slightly obscure just to avoid that. */
435         int prev_port = -1;
436         int err;
437
438         if (dp->sflow_probability) {
439                 struct net_bridge_port *p = skb->dev->br_port;
440                 if (p) {
441                         atomic_inc(&p->sflow_pool);
442                         if (dp->sflow_probability == UINT_MAX ||
443                             net_random() < dp->sflow_probability)
444                                 sflow_sample(dp, skb, a, n_actions, gfp, p);
445                 }
446         }
447
448         for (; n_actions > 0; a++, n_actions--) {
449                 WARN_ON_ONCE(skb_shared(skb));
450                 if (prev_port != -1) {
451                         do_output(dp, skb_clone(skb, gfp), prev_port);
452                         prev_port = -1;
453                 }
454
455                 switch (a->type) {
456                 case ODPAT_OUTPUT:
457                         prev_port = a->output.port;
458                         break;
459
460                 case ODPAT_OUTPUT_GROUP:
461                         prev_port = output_group(dp, a->output_group.group,
462                                                  skb, gfp);
463                         break;
464
465                 case ODPAT_CONTROLLER:
466                         err = output_control(dp, skb, a->controller.arg, gfp);
467                         if (err) {
468                                 kfree_skb(skb);
469                                 return err;
470                         }
471                         break;
472
473                 case ODPAT_SET_VLAN_VID:
474                 case ODPAT_SET_VLAN_PCP:
475                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
476                         if (IS_ERR(skb))
477                                 return PTR_ERR(skb);
478                         break;
479
480                 case ODPAT_STRIP_VLAN:
481                         skb = strip_vlan(skb, key, gfp);
482                         break;
483
484                 case ODPAT_SET_DL_SRC:
485                 case ODPAT_SET_DL_DST:
486                         skb = set_dl_addr(skb, &a->dl_addr, gfp);
487                         break;
488
489                 case ODPAT_SET_NW_SRC:
490                 case ODPAT_SET_NW_DST:
491                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
492                         break;
493
494                 case ODPAT_SET_TP_SRC:
495                 case ODPAT_SET_TP_DST:
496                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
497                         break;
498                 }
499                 if (!skb)
500                         return -ENOMEM;
501         }
502         if (prev_port != -1)
503                 do_output(dp, skb, prev_port);
504         else
505                 kfree_skb(skb);
506         return 0;
507 }