openvswitch: Extend packet attribute for egress tunnel info
[deliverable/linux.git] / net / openvswitch / actions.c
CommitLineData
ccb1352e 1/*
971427f3 2 * Copyright (c) 2007-2014 Nicira, Inc.
ccb1352e
JG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/skbuff.h>
22#include <linux/in.h>
23#include <linux/ip.h>
24#include <linux/openvswitch.h>
a175a723 25#include <linux/sctp.h>
ccb1352e
JG
26#include <linux/tcp.h>
27#include <linux/udp.h>
28#include <linux/in6.h>
29#include <linux/if_arp.h>
30#include <linux/if_vlan.h>
25cd9ba0 31
ccb1352e 32#include <net/ip.h>
3fdbd1ce 33#include <net/ipv6.h>
ccb1352e
JG
34#include <net/checksum.h>
35#include <net/dsfield.h>
25cd9ba0 36#include <net/mpls.h>
a175a723 37#include <net/sctp/checksum.h>
ccb1352e
JG
38
39#include "datapath.h"
971427f3 40#include "flow.h"
ccb1352e
JG
41#include "vport.h"
42
43static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 44 struct sw_flow_key *key,
651887b0 45 const struct nlattr *attr, int len);
ccb1352e 46
971427f3
AZ
47struct deferred_action {
48 struct sk_buff *skb;
49 const struct nlattr *actions;
50
51 /* Store pkt_key clone when creating deferred action. */
52 struct sw_flow_key pkt_key;
53};
54
55#define DEFERRED_ACTION_FIFO_SIZE 10
56struct action_fifo {
57 int head;
58 int tail;
59 /* Deferred action fifo queue storage. */
60 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
61};
62
63static struct action_fifo __percpu *action_fifos;
64static DEFINE_PER_CPU(int, exec_actions_level);
65
66static void action_fifo_init(struct action_fifo *fifo)
67{
68 fifo->head = 0;
69 fifo->tail = 0;
70}
71
72static bool action_fifo_is_empty(struct action_fifo *fifo)
73{
74 return (fifo->head == fifo->tail);
75}
76
77static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
78{
79 if (action_fifo_is_empty(fifo))
80 return NULL;
81
82 return &fifo->fifo[fifo->tail++];
83}
84
85static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
86{
87 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
88 return NULL;
89
90 return &fifo->fifo[fifo->head++];
91}
92
93/* Return true if fifo is not full */
94static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
95 struct sw_flow_key *key,
96 const struct nlattr *attr)
97{
98 struct action_fifo *fifo;
99 struct deferred_action *da;
100
101 fifo = this_cpu_ptr(action_fifos);
102 da = action_fifo_put(fifo);
103 if (da) {
104 da->skb = skb;
105 da->actions = attr;
106 da->pkt_key = *key;
107 }
108
109 return da;
110}
111
ccb1352e
JG
112static int make_writable(struct sk_buff *skb, int write_len)
113{
2ba5af42
JB
114 if (!pskb_may_pull(skb, write_len))
115 return -ENOMEM;
116
ccb1352e
JG
117 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
118 return 0;
119
120 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
121}
122
25cd9ba0
SH
123static int push_mpls(struct sk_buff *skb,
124 const struct ovs_action_push_mpls *mpls)
125{
126 __be32 *new_mpls_lse;
127 struct ethhdr *hdr;
128
129 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
130 if (skb->encapsulation)
131 return -ENOTSUPP;
132
133 if (skb_cow_head(skb, MPLS_HLEN) < 0)
134 return -ENOMEM;
135
136 skb_push(skb, MPLS_HLEN);
137 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
138 skb->mac_len);
139 skb_reset_mac_header(skb);
140
141 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
142 *new_mpls_lse = mpls->mpls_lse;
143
144 if (skb->ip_summed == CHECKSUM_COMPLETE)
145 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
146 MPLS_HLEN, 0));
147
148 hdr = eth_hdr(skb);
149 hdr->h_proto = mpls->mpls_ethertype;
150
151 skb_set_inner_protocol(skb, skb->protocol);
152 skb->protocol = mpls->mpls_ethertype;
153
154 return 0;
155}
156
157static int pop_mpls(struct sk_buff *skb, const __be16 ethertype)
158{
159 struct ethhdr *hdr;
160 int err;
161
162 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
163 if (unlikely(err))
164 return err;
165
166 if (skb->ip_summed == CHECKSUM_COMPLETE)
167 skb->csum = csum_sub(skb->csum,
168 csum_partial(skb_mpls_header(skb),
169 MPLS_HLEN, 0));
170
171 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
172 skb->mac_len);
173
174 __skb_pull(skb, MPLS_HLEN);
175 skb_reset_mac_header(skb);
176
177 /* skb_mpls_header() is used to locate the ethertype
178 * field correctly in the presence of VLAN tags.
179 */
180 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
181 hdr->h_proto = ethertype;
182 if (eth_p_mpls(skb->protocol))
183 skb->protocol = ethertype;
184 return 0;
185}
186
187static int set_mpls(struct sk_buff *skb, const __be32 *mpls_lse)
188{
189 __be32 *stack;
190 int err;
191
192 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
193 if (unlikely(err))
194 return err;
195
196 stack = (__be32 *)skb_mpls_header(skb);
197 if (skb->ip_summed == CHECKSUM_COMPLETE) {
198 __be32 diff[] = { ~(*stack), *mpls_lse };
199
200 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
201 ~skb->csum);
202 }
203
204 *stack = *mpls_lse;
205
206 return 0;
207}
208
39855b5b 209/* remove VLAN header from packet and update csum accordingly. */
ccb1352e
JG
210static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
211{
212 struct vlan_hdr *vhdr;
213 int err;
214
215 err = make_writable(skb, VLAN_ETH_HLEN);
216 if (unlikely(err))
217 return err;
218
219 if (skb->ip_summed == CHECKSUM_COMPLETE)
220 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
7b024082 221 + (2 * ETH_ALEN), VLAN_HLEN, 0));
ccb1352e
JG
222
223 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
224 *current_tci = vhdr->h_vlan_TCI;
225
226 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
227 __skb_pull(skb, VLAN_HLEN);
228
229 vlan_set_encap_proto(skb, vhdr);
230 skb->mac_header += VLAN_HLEN;
25cd9ba0 231
2ba5af42
JB
232 if (skb_network_offset(skb) < ETH_HLEN)
233 skb_set_network_header(skb, ETH_HLEN);
ccb1352e 234
25cd9ba0
SH
235 /* Update mac_len for subsequent MPLS actions */
236 skb_reset_mac_len(skb);
ccb1352e
JG
237 return 0;
238}
239
240static int pop_vlan(struct sk_buff *skb)
241{
242 __be16 tci;
243 int err;
244
245 if (likely(vlan_tx_tag_present(skb))) {
246 skb->vlan_tci = 0;
247 } else {
248 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
249 skb->len < VLAN_ETH_HLEN))
250 return 0;
251
252 err = __pop_vlan_tci(skb, &tci);
253 if (err)
254 return err;
255 }
256 /* move next vlan tag to hw accel tag */
257 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
258 skb->len < VLAN_ETH_HLEN))
259 return 0;
260
261 err = __pop_vlan_tci(skb, &tci);
262 if (unlikely(err))
263 return err;
264
86a9bad3 265 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
ccb1352e
JG
266 return 0;
267}
268
269static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
270{
271 if (unlikely(vlan_tx_tag_present(skb))) {
272 u16 current_tag;
273
274 /* push down current VLAN tag */
275 current_tag = vlan_tx_tag_get(skb);
276
86a9bad3 277 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
ccb1352e 278 return -ENOMEM;
25cd9ba0
SH
279 /* Update mac_len for subsequent MPLS actions */
280 skb->mac_len += VLAN_HLEN;
ccb1352e
JG
281
282 if (skb->ip_summed == CHECKSUM_COMPLETE)
283 skb->csum = csum_add(skb->csum, csum_partial(skb->data
7b024082 284 + (2 * ETH_ALEN), VLAN_HLEN, 0));
ccb1352e
JG
285
286 }
86a9bad3 287 __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
ccb1352e
JG
288 return 0;
289}
290
291static int set_eth_addr(struct sk_buff *skb,
292 const struct ovs_key_ethernet *eth_key)
293{
294 int err;
295 err = make_writable(skb, ETH_HLEN);
296 if (unlikely(err))
297 return err;
298
b34df5e8
PS
299 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
300
8c63ff09
JP
301 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
302 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
ccb1352e 303
b34df5e8
PS
304 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
305
ccb1352e
JG
306 return 0;
307}
308
309static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
310 __be32 *addr, __be32 new_addr)
311{
312 int transport_len = skb->len - skb_transport_offset(skb);
313
314 if (nh->protocol == IPPROTO_TCP) {
315 if (likely(transport_len >= sizeof(struct tcphdr)))
316 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
317 *addr, new_addr, 1);
318 } else if (nh->protocol == IPPROTO_UDP) {
81e5d41d
JG
319 if (likely(transport_len >= sizeof(struct udphdr))) {
320 struct udphdr *uh = udp_hdr(skb);
321
322 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
323 inet_proto_csum_replace4(&uh->check, skb,
324 *addr, new_addr, 1);
325 if (!uh->check)
326 uh->check = CSUM_MANGLED_0;
327 }
328 }
ccb1352e
JG
329 }
330
331 csum_replace4(&nh->check, *addr, new_addr);
7539fadc 332 skb_clear_hash(skb);
ccb1352e
JG
333 *addr = new_addr;
334}
335
3fdbd1ce
AA
336static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
337 __be32 addr[4], const __be32 new_addr[4])
338{
339 int transport_len = skb->len - skb_transport_offset(skb);
340
341 if (l4_proto == IPPROTO_TCP) {
342 if (likely(transport_len >= sizeof(struct tcphdr)))
343 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
344 addr, new_addr, 1);
345 } else if (l4_proto == IPPROTO_UDP) {
346 if (likely(transport_len >= sizeof(struct udphdr))) {
347 struct udphdr *uh = udp_hdr(skb);
348
349 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
350 inet_proto_csum_replace16(&uh->check, skb,
351 addr, new_addr, 1);
352 if (!uh->check)
353 uh->check = CSUM_MANGLED_0;
354 }
355 }
356 }
357}
358
359static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
360 __be32 addr[4], const __be32 new_addr[4],
361 bool recalculate_csum)
362{
363 if (recalculate_csum)
364 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
365
7539fadc 366 skb_clear_hash(skb);
3fdbd1ce
AA
367 memcpy(addr, new_addr, sizeof(__be32[4]));
368}
369
370static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
371{
372 nh->priority = tc >> 4;
373 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
374}
375
376static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
377{
378 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
379 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
380 nh->flow_lbl[2] = fl & 0x000000FF;
381}
382
ccb1352e
JG
383static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
384{
385 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
386 nh->ttl = new_ttl;
387}
388
389static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
390{
391 struct iphdr *nh;
392 int err;
393
394 err = make_writable(skb, skb_network_offset(skb) +
395 sizeof(struct iphdr));
396 if (unlikely(err))
397 return err;
398
399 nh = ip_hdr(skb);
400
401 if (ipv4_key->ipv4_src != nh->saddr)
402 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
403
404 if (ipv4_key->ipv4_dst != nh->daddr)
405 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
406
407 if (ipv4_key->ipv4_tos != nh->tos)
408 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
409
410 if (ipv4_key->ipv4_ttl != nh->ttl)
411 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
412
413 return 0;
414}
415
3fdbd1ce
AA
416static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
417{
418 struct ipv6hdr *nh;
419 int err;
420 __be32 *saddr;
421 __be32 *daddr;
422
423 err = make_writable(skb, skb_network_offset(skb) +
424 sizeof(struct ipv6hdr));
425 if (unlikely(err))
426 return err;
427
428 nh = ipv6_hdr(skb);
429 saddr = (__be32 *)&nh->saddr;
430 daddr = (__be32 *)&nh->daddr;
431
432 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
433 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
434 ipv6_key->ipv6_src, true);
435
436 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
437 unsigned int offset = 0;
438 int flags = IP6_FH_F_SKIP_RH;
439 bool recalc_csum = true;
440
441 if (ipv6_ext_hdr(nh->nexthdr))
442 recalc_csum = ipv6_find_hdr(skb, &offset,
443 NEXTHDR_ROUTING, NULL,
444 &flags) != NEXTHDR_ROUTING;
445
446 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
447 ipv6_key->ipv6_dst, recalc_csum);
448 }
449
450 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
451 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
452 nh->hop_limit = ipv6_key->ipv6_hlimit;
453
454 return 0;
455}
456
ccb1352e
JG
457/* Must follow make_writable() since that can move the skb data. */
458static void set_tp_port(struct sk_buff *skb, __be16 *port,
459 __be16 new_port, __sum16 *check)
460{
461 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
462 *port = new_port;
7539fadc 463 skb_clear_hash(skb);
ccb1352e
JG
464}
465
81e5d41d
JG
466static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
467{
468 struct udphdr *uh = udp_hdr(skb);
469
470 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
471 set_tp_port(skb, port, new_port, &uh->check);
472
473 if (!uh->check)
474 uh->check = CSUM_MANGLED_0;
475 } else {
476 *port = new_port;
7539fadc 477 skb_clear_hash(skb);
81e5d41d
JG
478 }
479}
480
481static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
ccb1352e
JG
482{
483 struct udphdr *uh;
484 int err;
485
486 err = make_writable(skb, skb_transport_offset(skb) +
487 sizeof(struct udphdr));
488 if (unlikely(err))
489 return err;
490
491 uh = udp_hdr(skb);
492 if (udp_port_key->udp_src != uh->source)
81e5d41d 493 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
ccb1352e
JG
494
495 if (udp_port_key->udp_dst != uh->dest)
81e5d41d 496 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
ccb1352e
JG
497
498 return 0;
499}
500
81e5d41d 501static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
ccb1352e
JG
502{
503 struct tcphdr *th;
504 int err;
505
506 err = make_writable(skb, skb_transport_offset(skb) +
507 sizeof(struct tcphdr));
508 if (unlikely(err))
509 return err;
510
511 th = tcp_hdr(skb);
512 if (tcp_port_key->tcp_src != th->source)
513 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
514
515 if (tcp_port_key->tcp_dst != th->dest)
516 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
517
518 return 0;
519}
520
a175a723
JS
521static int set_sctp(struct sk_buff *skb,
522 const struct ovs_key_sctp *sctp_port_key)
523{
524 struct sctphdr *sh;
525 int err;
526 unsigned int sctphoff = skb_transport_offset(skb);
527
528 err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
529 if (unlikely(err))
530 return err;
531
532 sh = sctp_hdr(skb);
533 if (sctp_port_key->sctp_src != sh->source ||
534 sctp_port_key->sctp_dst != sh->dest) {
535 __le32 old_correct_csum, new_csum, old_csum;
536
537 old_csum = sh->checksum;
538 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
539
540 sh->source = sctp_port_key->sctp_src;
541 sh->dest = sctp_port_key->sctp_dst;
542
543 new_csum = sctp_compute_cksum(skb, sctphoff);
544
545 /* Carry any checksum errors through. */
546 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
547
7539fadc 548 skb_clear_hash(skb);
a175a723
JS
549 }
550
551 return 0;
552}
553
738967b8 554static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
ccb1352e 555{
738967b8 556 struct vport *vport = ovs_vport_rcu(dp, out_port);
ccb1352e 557
738967b8
AZ
558 if (likely(vport))
559 ovs_vport_send(vport, skb);
560 else
ccb1352e 561 kfree_skb(skb);
ccb1352e
JG
562}
563
564static int output_userspace(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 565 struct sw_flow_key *key, const struct nlattr *attr)
ccb1352e 566{
8f0aad6f 567 struct ovs_tunnel_info info;
ccb1352e
JG
568 struct dp_upcall_info upcall;
569 const struct nlattr *a;
570 int rem;
571
572 upcall.cmd = OVS_PACKET_CMD_ACTION;
2ff3e4e4 573 upcall.key = key;
ccb1352e 574 upcall.userdata = NULL;
15e47304 575 upcall.portid = 0;
8f0aad6f 576 upcall.egress_tun_info = NULL;
ccb1352e
JG
577
578 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
579 a = nla_next(a, &rem)) {
580 switch (nla_type(a)) {
581 case OVS_USERSPACE_ATTR_USERDATA:
582 upcall.userdata = a;
583 break;
584
585 case OVS_USERSPACE_ATTR_PID:
15e47304 586 upcall.portid = nla_get_u32(a);
ccb1352e 587 break;
8f0aad6f
WZ
588
589 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
590 /* Get out tunnel info. */
591 struct vport *vport;
592
593 vport = ovs_vport_rcu(dp, nla_get_u32(a));
594 if (vport) {
595 int err;
596
597 err = ovs_vport_get_egress_tun_info(vport, skb,
598 &info);
599 if (!err)
600 upcall.egress_tun_info = &info;
601 }
602 break;
ccb1352e 603 }
8f0aad6f
WZ
604
605 } /* End of switch. */
ccb1352e
JG
606 }
607
608 return ovs_dp_upcall(dp, skb, &upcall);
609}
610
611static int sample(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 612 struct sw_flow_key *key, const struct nlattr *attr)
ccb1352e
JG
613{
614 const struct nlattr *acts_list = NULL;
615 const struct nlattr *a;
616 int rem;
617
618 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
619 a = nla_next(a, &rem)) {
620 switch (nla_type(a)) {
621 case OVS_SAMPLE_ATTR_PROBABILITY:
63862b5b 622 if (prandom_u32() >= nla_get_u32(a))
ccb1352e
JG
623 return 0;
624 break;
625
626 case OVS_SAMPLE_ATTR_ACTIONS:
627 acts_list = a;
628 break;
629 }
630 }
631
651887b0
SH
632 rem = nla_len(acts_list);
633 a = nla_data(acts_list);
634
32ae87ff
AZ
635 /* Actions list is empty, do nothing */
636 if (unlikely(!rem))
637 return 0;
651887b0 638
32ae87ff
AZ
639 /* The only known usage of sample action is having a single user-space
640 * action. Treat this usage as a special case.
641 * The output_userspace() should clone the skb to be sent to the
642 * user space. This skb will be consumed by its caller.
651887b0 643 */
32ae87ff 644 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
941d8ebc 645 nla_is_last(a, rem)))
32ae87ff
AZ
646 return output_userspace(dp, skb, key, a);
647
648 skb = skb_clone(skb, GFP_ATOMIC);
649 if (!skb)
650 /* Skip the sample action when out of memory. */
651 return 0;
652
971427f3
AZ
653 if (!add_deferred_actions(skb, key, a)) {
654 if (net_ratelimit())
655 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
656 ovs_dp_name(dp));
657
658 kfree_skb(skb);
659 }
660 return 0;
661}
662
663static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
664 const struct nlattr *attr)
665{
666 struct ovs_action_hash *hash_act = nla_data(attr);
667 u32 hash = 0;
668
669 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
670 hash = skb_get_hash(skb);
671 hash = jhash_1word(hash, hash_act->hash_basis);
672 if (!hash)
673 hash = 0x1;
674
675 key->ovs_flow_hash = hash;
ccb1352e
JG
676}
677
678static int execute_set_action(struct sk_buff *skb,
679 const struct nlattr *nested_attr)
680{
681 int err = 0;
682
683 switch (nla_type(nested_attr)) {
684 case OVS_KEY_ATTR_PRIORITY:
685 skb->priority = nla_get_u32(nested_attr);
686 break;
687
39c7caeb
AA
688 case OVS_KEY_ATTR_SKB_MARK:
689 skb->mark = nla_get_u32(nested_attr);
690 break;
691
f0b128c1
JG
692 case OVS_KEY_ATTR_TUNNEL_INFO:
693 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
7d5437c7
PS
694 break;
695
ccb1352e
JG
696 case OVS_KEY_ATTR_ETHERNET:
697 err = set_eth_addr(skb, nla_data(nested_attr));
698 break;
699
700 case OVS_KEY_ATTR_IPV4:
701 err = set_ipv4(skb, nla_data(nested_attr));
702 break;
703
3fdbd1ce
AA
704 case OVS_KEY_ATTR_IPV6:
705 err = set_ipv6(skb, nla_data(nested_attr));
706 break;
707
ccb1352e 708 case OVS_KEY_ATTR_TCP:
81e5d41d 709 err = set_tcp(skb, nla_data(nested_attr));
ccb1352e
JG
710 break;
711
712 case OVS_KEY_ATTR_UDP:
81e5d41d 713 err = set_udp(skb, nla_data(nested_attr));
ccb1352e 714 break;
a175a723
JS
715
716 case OVS_KEY_ATTR_SCTP:
717 err = set_sctp(skb, nla_data(nested_attr));
718 break;
25cd9ba0
SH
719
720 case OVS_KEY_ATTR_MPLS:
721 err = set_mpls(skb, nla_data(nested_attr));
722 break;
ccb1352e
JG
723 }
724
725 return err;
726}
727
971427f3
AZ
728static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
729 struct sw_flow_key *key,
730 const struct nlattr *a, int rem)
731{
732 struct deferred_action *da;
733 int err;
734
735 err = ovs_flow_key_update(skb, key);
736 if (err)
737 return err;
738
941d8ebc 739 if (!nla_is_last(a, rem)) {
971427f3
AZ
740 /* Recirc action is the not the last action
741 * of the action list, need to clone the skb.
742 */
743 skb = skb_clone(skb, GFP_ATOMIC);
744
745 /* Skip the recirc action when out of memory, but
746 * continue on with the rest of the action list.
747 */
748 if (!skb)
749 return 0;
750 }
751
752 da = add_deferred_actions(skb, key, NULL);
753 if (da) {
754 da->pkt_key.recirc_id = nla_get_u32(a);
755 } else {
756 kfree_skb(skb);
757
758 if (net_ratelimit())
759 pr_warn("%s: deferred action limit reached, drop recirc action\n",
760 ovs_dp_name(dp));
761 }
762
763 return 0;
764}
765
ccb1352e
JG
766/* Execute a list of actions against 'skb'. */
767static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 768 struct sw_flow_key *key,
651887b0 769 const struct nlattr *attr, int len)
ccb1352e
JG
770{
771 /* Every output action needs a separate clone of 'skb', but the common
772 * case is just a single output action, so that doing a clone and
773 * then freeing the original skbuff is wasteful. So the following code
774 * is slightly obscure just to avoid that. */
775 int prev_port = -1;
776 const struct nlattr *a;
777 int rem;
778
779 for (a = attr, rem = len; rem > 0;
780 a = nla_next(a, &rem)) {
781 int err = 0;
782
738967b8
AZ
783 if (unlikely(prev_port != -1)) {
784 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
785
786 if (out_skb)
787 do_output(dp, out_skb, prev_port);
788
ccb1352e
JG
789 prev_port = -1;
790 }
791
792 switch (nla_type(a)) {
793 case OVS_ACTION_ATTR_OUTPUT:
794 prev_port = nla_get_u32(a);
795 break;
796
797 case OVS_ACTION_ATTR_USERSPACE:
2ff3e4e4 798 output_userspace(dp, skb, key, a);
ccb1352e
JG
799 break;
800
971427f3
AZ
801 case OVS_ACTION_ATTR_HASH:
802 execute_hash(skb, key, a);
803 break;
804
25cd9ba0
SH
805 case OVS_ACTION_ATTR_PUSH_MPLS:
806 err = push_mpls(skb, nla_data(a));
807 break;
808
809 case OVS_ACTION_ATTR_POP_MPLS:
810 err = pop_mpls(skb, nla_get_be16(a));
811 break;
812
ccb1352e
JG
813 case OVS_ACTION_ATTR_PUSH_VLAN:
814 err = push_vlan(skb, nla_data(a));
815 if (unlikely(err)) /* skb already freed. */
816 return err;
817 break;
818
819 case OVS_ACTION_ATTR_POP_VLAN:
820 err = pop_vlan(skb);
821 break;
822
971427f3
AZ
823 case OVS_ACTION_ATTR_RECIRC:
824 err = execute_recirc(dp, skb, key, a, rem);
941d8ebc 825 if (nla_is_last(a, rem)) {
971427f3
AZ
826 /* If this is the last action, the skb has
827 * been consumed or freed.
828 * Return immediately.
829 */
830 return err;
831 }
832 break;
833
ccb1352e
JG
834 case OVS_ACTION_ATTR_SET:
835 err = execute_set_action(skb, nla_data(a));
836 break;
837
838 case OVS_ACTION_ATTR_SAMPLE:
2ff3e4e4 839 err = sample(dp, skb, key, a);
fe984c08
AZ
840 if (unlikely(err)) /* skb already freed. */
841 return err;
ccb1352e
JG
842 break;
843 }
844
845 if (unlikely(err)) {
846 kfree_skb(skb);
847 return err;
848 }
849 }
850
651887b0 851 if (prev_port != -1)
ccb1352e 852 do_output(dp, skb, prev_port);
651887b0 853 else
ccb1352e
JG
854 consume_skb(skb);
855
856 return 0;
857}
858
971427f3
AZ
859static void process_deferred_actions(struct datapath *dp)
860{
861 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
862
863 /* Do not touch the FIFO in case there is no deferred actions. */
864 if (action_fifo_is_empty(fifo))
865 return;
866
867 /* Finishing executing all deferred actions. */
868 do {
869 struct deferred_action *da = action_fifo_get(fifo);
870 struct sk_buff *skb = da->skb;
871 struct sw_flow_key *key = &da->pkt_key;
872 const struct nlattr *actions = da->actions;
873
874 if (actions)
875 do_execute_actions(dp, skb, key, actions,
876 nla_len(actions));
877 else
878 ovs_dp_process_packet(skb, key);
879 } while (!action_fifo_is_empty(fifo));
880
881 /* Reset FIFO for the next packet. */
882 action_fifo_init(fifo);
883}
884
ccb1352e 885/* Execute a list of actions against 'skb'. */
2ff3e4e4 886int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
d98612b8 887 struct sw_flow_actions *acts, struct sw_flow_key *key)
ccb1352e 888{
971427f3 889 int level = this_cpu_read(exec_actions_level);
971427f3
AZ
890 int err;
891
971427f3 892 this_cpu_inc(exec_actions_level);
f0b128c1 893 OVS_CB(skb)->egress_tun_info = NULL;
971427f3
AZ
894 err = do_execute_actions(dp, skb, key,
895 acts->actions, acts->actions_len);
896
897 if (!level)
898 process_deferred_actions(dp);
899
900 this_cpu_dec(exec_actions_level);
901 return err;
902}
903
904int action_fifos_init(void)
905{
906 action_fifos = alloc_percpu(struct action_fifo);
907 if (!action_fifos)
908 return -ENOMEM;
ccb1352e 909
971427f3
AZ
910 return 0;
911}
912
913void action_fifos_exit(void)
914{
915 free_percpu(action_fifos);
ccb1352e 916}
This page took 0.182133 seconds and 5 git commands to generate.