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