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