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