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