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