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