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