blob: b491c1c296fe8f954872320b7aef4db00dfb6f4a [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou971427f32014-09-15 19:37:25 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
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>
Joe Stringera175a722013-08-22 12:30:48 -070025#include <linux/sctp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070026#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>
Simon Horman25cd9ba2014-10-06 05:05:13 -070031
Jesse Grossccb13522011-10-25 19:26:31 -070032#include <net/ip.h>
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -080033#include <net/ipv6.h>
Jesse Grossccb13522011-10-25 19:26:31 -070034#include <net/checksum.h>
35#include <net/dsfield.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070036#include <net/mpls.h>
Joe Stringera175a722013-08-22 12:30:48 -070037#include <net/sctp/checksum.h>
Jesse Grossccb13522011-10-25 19:26:31 -070038
39#include "datapath.h"
Andy Zhou971427f32014-09-15 19:37:25 -070040#include "flow.h"
Jesse Grossccb13522011-10-25 19:26:31 -070041#include "vport.h"
42
43static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -070044 struct sw_flow_key *key,
Simon Horman651887b2014-07-21 15:12:34 -070045 const struct nlattr *attr, int len);
Jesse Grossccb13522011-10-25 19:26:31 -070046
Andy Zhou971427f32014-09-15 19:37:25 -070047struct 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
Thomas Graf12eb18f2014-11-06 06:58:52 -080072static bool action_fifo_is_empty(const struct action_fifo *fifo)
Andy Zhou971427f32014-09-15 19:37:25 -070073{
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,
Thomas Graf12eb18f2014-11-06 06:58:52 -080095 const struct sw_flow_key *key,
Andy Zhou971427f32014-09-15 19:37:25 -070096 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
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800112static 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
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800122static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700123 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
Pravin B Shelarcbe7e762014-12-23 16:20:28 -0800150 if (!skb->inner_protocol)
151 skb_set_inner_protocol(skb, skb->protocol);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700152 skb->protocol = mpls->mpls_ethertype;
153
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800154 invalidate_flow_key(key);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700155 return 0;
156}
157
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800158static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
159 const __be16 ethertype)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700160{
161 struct ethhdr *hdr;
162 int err;
163
Jiri Pirkoe2195122014-11-19 14:05:01 +0100164 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700165 if (unlikely(err))
166 return err;
167
Jiri Pirko1abcd822014-11-19 14:04:55 +0100168 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700169
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;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800183
184 invalidate_flow_key(key);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700185 return 0;
186}
187
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800188/* 'KEY' must not have any bits set outside of the 'MASK' */
189#define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
190#define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
191
192static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
193 const __be32 *mpls_lse, const __be32 *mask)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700194{
195 __be32 *stack;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800196 __be32 lse;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700197 int err;
198
Jiri Pirkoe2195122014-11-19 14:05:01 +0100199 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700200 if (unlikely(err))
201 return err;
202
203 stack = (__be32 *)skb_mpls_header(skb);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800204 lse = MASKED(*stack, *mpls_lse, *mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700205 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800206 __be32 diff[] = { ~(*stack), lse };
207
Simon Horman25cd9ba2014-10-06 05:05:13 -0700208 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
209 ~skb->csum);
210 }
211
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800212 *stack = lse;
213 flow_key->mpls.top_lse = lse;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700214 return 0;
215}
216
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800217static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700218{
Jesse Grossccb13522011-10-25 19:26:31 -0700219 int err;
220
Jiri Pirko93515d52014-11-19 14:05:02 +0100221 err = skb_vlan_pop(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100222 if (skb_vlan_tag_present(skb))
Jiri Pirko93515d52014-11-19 14:05:02 +0100223 invalidate_flow_key(key);
224 else
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800225 key->eth.tci = 0;
Jiri Pirko93515d52014-11-19 14:05:02 +0100226 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700227}
228
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800229static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
230 const struct ovs_action_push_vlan *vlan)
Jesse Grossccb13522011-10-25 19:26:31 -0700231{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100232 if (skb_vlan_tag_present(skb))
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800233 invalidate_flow_key(key);
Jiri Pirko93515d52014-11-19 14:05:02 +0100234 else
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800235 key->eth.tci = vlan->vlan_tci;
Jiri Pirko93515d52014-11-19 14:05:02 +0100236 return skb_vlan_push(skb, vlan->vlan_tpid,
237 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
Jesse Grossccb13522011-10-25 19:26:31 -0700238}
239
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800240/* 'src' is already properly masked. */
241static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
242{
243 u16 *dst = (u16 *)dst_;
244 const u16 *src = (const u16 *)src_;
245 const u16 *mask = (const u16 *)mask_;
246
247 SET_MASKED(dst[0], src[0], mask[0]);
248 SET_MASKED(dst[1], src[1], mask[1]);
249 SET_MASKED(dst[2], src[2], mask[2]);
250}
251
252static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
253 const struct ovs_key_ethernet *key,
254 const struct ovs_key_ethernet *mask)
Jesse Grossccb13522011-10-25 19:26:31 -0700255{
256 int err;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800257
Jiri Pirkoe2195122014-11-19 14:05:01 +0100258 err = skb_ensure_writable(skb, ETH_HLEN);
Jesse Grossccb13522011-10-25 19:26:31 -0700259 if (unlikely(err))
260 return err;
261
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700262 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
263
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800264 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
265 mask->eth_src);
266 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
267 mask->eth_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700268
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700269 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
270
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800271 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
272 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
Jesse Grossccb13522011-10-25 19:26:31 -0700273 return 0;
274}
275
276static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800277 __be32 *addr, __be32 new_addr)
Jesse Grossccb13522011-10-25 19:26:31 -0700278{
279 int transport_len = skb->len - skb_transport_offset(skb);
280
281 if (nh->protocol == IPPROTO_TCP) {
282 if (likely(transport_len >= sizeof(struct tcphdr)))
283 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
284 *addr, new_addr, 1);
285 } else if (nh->protocol == IPPROTO_UDP) {
Jesse Gross81e5d412012-03-06 15:05:46 -0800286 if (likely(transport_len >= sizeof(struct udphdr))) {
287 struct udphdr *uh = udp_hdr(skb);
288
289 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
290 inet_proto_csum_replace4(&uh->check, skb,
291 *addr, new_addr, 1);
292 if (!uh->check)
293 uh->check = CSUM_MANGLED_0;
294 }
295 }
Jesse Grossccb13522011-10-25 19:26:31 -0700296 }
297
298 csum_replace4(&nh->check, *addr, new_addr);
Tom Herbert7539fad2013-12-15 22:12:18 -0800299 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 *addr = new_addr;
301}
302
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800303static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
304 __be32 addr[4], const __be32 new_addr[4])
305{
306 int transport_len = skb->len - skb_transport_offset(skb);
307
Jesse Gross856447d2014-11-11 14:32:20 -0800308 if (l4_proto == NEXTHDR_TCP) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800309 if (likely(transport_len >= sizeof(struct tcphdr)))
310 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
311 addr, new_addr, 1);
Jesse Gross856447d2014-11-11 14:32:20 -0800312 } else if (l4_proto == NEXTHDR_UDP) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800313 if (likely(transport_len >= sizeof(struct udphdr))) {
314 struct udphdr *uh = udp_hdr(skb);
315
316 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
317 inet_proto_csum_replace16(&uh->check, skb,
318 addr, new_addr, 1);
319 if (!uh->check)
320 uh->check = CSUM_MANGLED_0;
321 }
322 }
Jesse Gross856447d2014-11-11 14:32:20 -0800323 } else if (l4_proto == NEXTHDR_ICMP) {
324 if (likely(transport_len >= sizeof(struct icmp6hdr)))
325 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
326 skb, addr, new_addr, 1);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800327 }
328}
329
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800330static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
331 const __be32 mask[4], __be32 masked[4])
332{
333 masked[0] = MASKED(old[0], addr[0], mask[0]);
334 masked[1] = MASKED(old[1], addr[1], mask[1]);
335 masked[2] = MASKED(old[2], addr[2], mask[2]);
336 masked[3] = MASKED(old[3], addr[3], mask[3]);
337}
338
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800339static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
340 __be32 addr[4], const __be32 new_addr[4],
341 bool recalculate_csum)
342{
343 if (recalculate_csum)
344 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
345
Tom Herbert7539fad2013-12-15 22:12:18 -0800346 skb_clear_hash(skb);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800347 memcpy(addr, new_addr, sizeof(__be32[4]));
348}
349
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800350static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800351{
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800352 /* Bits 21-24 are always unmasked, so this retains their values. */
353 SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
354 SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
355 SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800356}
357
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800358static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
359 u8 mask)
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800360{
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800361 new_ttl = MASKED(nh->ttl, new_ttl, mask);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800362
Jesse Grossccb13522011-10-25 19:26:31 -0700363 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
364 nh->ttl = new_ttl;
365}
366
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800367static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
368 const struct ovs_key_ipv4 *key,
369 const struct ovs_key_ipv4 *mask)
Jesse Grossccb13522011-10-25 19:26:31 -0700370{
371 struct iphdr *nh;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800372 __be32 new_addr;
Jesse Grossccb13522011-10-25 19:26:31 -0700373 int err;
374
Jiri Pirkoe2195122014-11-19 14:05:01 +0100375 err = skb_ensure_writable(skb, skb_network_offset(skb) +
376 sizeof(struct iphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700377 if (unlikely(err))
378 return err;
379
380 nh = ip_hdr(skb);
381
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800382 /* Setting an IP addresses is typically only a side effect of
383 * matching on them in the current userspace implementation, so it
384 * makes sense to check if the value actually changed.
385 */
386 if (mask->ipv4_src) {
387 new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
Jesse Grossccb13522011-10-25 19:26:31 -0700388
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800389 if (unlikely(new_addr != nh->saddr)) {
390 set_ip_addr(skb, nh, &nh->saddr, new_addr);
391 flow_key->ipv4.addr.src = new_addr;
392 }
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800393 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800394 if (mask->ipv4_dst) {
395 new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700396
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800397 if (unlikely(new_addr != nh->daddr)) {
398 set_ip_addr(skb, nh, &nh->daddr, new_addr);
399 flow_key->ipv4.addr.dst = new_addr;
400 }
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800401 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800402 if (mask->ipv4_tos) {
403 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
404 flow_key->ip.tos = nh->tos;
405 }
406 if (mask->ipv4_ttl) {
407 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
408 flow_key->ip.ttl = nh->ttl;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800409 }
Jesse Grossccb13522011-10-25 19:26:31 -0700410
411 return 0;
412}
413
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800414static bool is_ipv6_mask_nonzero(const __be32 addr[4])
415{
416 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
417}
418
419static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
420 const struct ovs_key_ipv6 *key,
421 const struct ovs_key_ipv6 *mask)
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800422{
423 struct ipv6hdr *nh;
424 int err;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800425
Jiri Pirkoe2195122014-11-19 14:05:01 +0100426 err = skb_ensure_writable(skb, skb_network_offset(skb) +
427 sizeof(struct ipv6hdr));
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800428 if (unlikely(err))
429 return err;
430
431 nh = ipv6_hdr(skb);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800432
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800433 /* Setting an IP addresses is typically only a side effect of
434 * matching on them in the current userspace implementation, so it
435 * makes sense to check if the value actually changed.
436 */
437 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
438 __be32 *saddr = (__be32 *)&nh->saddr;
439 __be32 masked[4];
440
441 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
442
443 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
444 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
445 true);
446 memcpy(&flow_key->ipv6.addr.src, masked,
447 sizeof(flow_key->ipv6.addr.src));
448 }
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800449 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800450 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800451 unsigned int offset = 0;
452 int flags = IP6_FH_F_SKIP_RH;
453 bool recalc_csum = true;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800454 __be32 *daddr = (__be32 *)&nh->daddr;
455 __be32 masked[4];
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800456
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800457 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800458
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800459 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
460 if (ipv6_ext_hdr(nh->nexthdr))
461 recalc_csum = (ipv6_find_hdr(skb, &offset,
462 NEXTHDR_ROUTING,
463 NULL, &flags)
464 != NEXTHDR_ROUTING);
465
466 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
467 recalc_csum);
468 memcpy(&flow_key->ipv6.addr.dst, masked,
469 sizeof(flow_key->ipv6.addr.dst));
470 }
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800471 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800472 if (mask->ipv6_tclass) {
473 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
474 flow_key->ip.tos = ipv6_get_dsfield(nh);
475 }
476 if (mask->ipv6_label) {
477 set_ipv6_fl(nh, ntohl(key->ipv6_label),
478 ntohl(mask->ipv6_label));
479 flow_key->ipv6.label =
480 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
481 }
482 if (mask->ipv6_hlimit) {
483 SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
484 flow_key->ip.ttl = nh->hop_limit;
485 }
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800486 return 0;
487}
488
Jiri Pirkoe2195122014-11-19 14:05:01 +0100489/* Must follow skb_ensure_writable() since that can move the skb data. */
Jesse Grossccb13522011-10-25 19:26:31 -0700490static void set_tp_port(struct sk_buff *skb, __be16 *port,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800491 __be16 new_port, __sum16 *check)
Jesse Grossccb13522011-10-25 19:26:31 -0700492{
493 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
494 *port = new_port;
Jesse Grossccb13522011-10-25 19:26:31 -0700495}
496
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800497static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
498 const struct ovs_key_udp *key,
499 const struct ovs_key_udp *mask)
Jesse Grossccb13522011-10-25 19:26:31 -0700500{
501 struct udphdr *uh;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800502 __be16 src, dst;
Jesse Grossccb13522011-10-25 19:26:31 -0700503 int err;
504
Jiri Pirkoe2195122014-11-19 14:05:01 +0100505 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
506 sizeof(struct udphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700507 if (unlikely(err))
508 return err;
509
510 uh = udp_hdr(skb);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800511 /* Either of the masks is non-zero, so do not bother checking them. */
512 src = MASKED(uh->source, key->udp_src, mask->udp_src);
513 dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
514
515 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
516 if (likely(src != uh->source)) {
517 set_tp_port(skb, &uh->source, src, &uh->check);
518 flow_key->tp.src = src;
519 }
520 if (likely(dst != uh->dest)) {
521 set_tp_port(skb, &uh->dest, dst, &uh->check);
522 flow_key->tp.dst = dst;
523 }
524
525 if (unlikely(!uh->check))
526 uh->check = CSUM_MANGLED_0;
527 } else {
528 uh->source = src;
529 uh->dest = dst;
530 flow_key->tp.src = src;
531 flow_key->tp.dst = dst;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800532 }
Jesse Grossccb13522011-10-25 19:26:31 -0700533
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800534 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700535
536 return 0;
537}
538
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800539static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
540 const struct ovs_key_tcp *key,
541 const struct ovs_key_tcp *mask)
Jesse Grossccb13522011-10-25 19:26:31 -0700542{
543 struct tcphdr *th;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800544 __be16 src, dst;
Jesse Grossccb13522011-10-25 19:26:31 -0700545 int err;
546
Jiri Pirkoe2195122014-11-19 14:05:01 +0100547 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
548 sizeof(struct tcphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700549 if (unlikely(err))
550 return err;
551
552 th = tcp_hdr(skb);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800553 src = MASKED(th->source, key->tcp_src, mask->tcp_src);
554 if (likely(src != th->source)) {
555 set_tp_port(skb, &th->source, src, &th->check);
556 flow_key->tp.src = src;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800557 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800558 dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
559 if (likely(dst != th->dest)) {
560 set_tp_port(skb, &th->dest, dst, &th->check);
561 flow_key->tp.dst = dst;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800562 }
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800563 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700564
565 return 0;
566}
567
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800568static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
569 const struct ovs_key_sctp *key,
570 const struct ovs_key_sctp *mask)
Joe Stringera175a722013-08-22 12:30:48 -0700571{
Joe Stringera175a722013-08-22 12:30:48 -0700572 unsigned int sctphoff = skb_transport_offset(skb);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800573 struct sctphdr *sh;
574 __le32 old_correct_csum, new_csum, old_csum;
575 int err;
Joe Stringera175a722013-08-22 12:30:48 -0700576
Jiri Pirkoe2195122014-11-19 14:05:01 +0100577 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
Joe Stringera175a722013-08-22 12:30:48 -0700578 if (unlikely(err))
579 return err;
580
581 sh = sctp_hdr(skb);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800582 old_csum = sh->checksum;
583 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
Joe Stringera175a722013-08-22 12:30:48 -0700584
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800585 sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
586 sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
Joe Stringera175a722013-08-22 12:30:48 -0700587
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800588 new_csum = sctp_compute_cksum(skb, sctphoff);
Joe Stringera175a722013-08-22 12:30:48 -0700589
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800590 /* Carry any checksum errors through. */
591 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
Joe Stringera175a722013-08-22 12:30:48 -0700592
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800593 skb_clear_hash(skb);
594 flow_key->tp.src = sh->source;
595 flow_key->tp.dst = sh->dest;
Joe Stringera175a722013-08-22 12:30:48 -0700596
597 return 0;
598}
599
Andy Zhou738967b2014-09-08 00:35:02 -0700600static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
Jesse Grossccb13522011-10-25 19:26:31 -0700601{
Andy Zhou738967b2014-09-08 00:35:02 -0700602 struct vport *vport = ovs_vport_rcu(dp, out_port);
Jesse Grossccb13522011-10-25 19:26:31 -0700603
Andy Zhou738967b2014-09-08 00:35:02 -0700604 if (likely(vport))
605 ovs_vport_send(vport, skb);
606 else
Jesse Grossccb13522011-10-25 19:26:31 -0700607 kfree_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700608}
609
610static int output_userspace(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700611 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700612{
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800613 struct ovs_tunnel_info info;
Jesse Grossccb13522011-10-25 19:26:31 -0700614 struct dp_upcall_info upcall;
615 const struct nlattr *a;
616 int rem;
617
618 upcall.cmd = OVS_PACKET_CMD_ACTION;
Jesse Grossccb13522011-10-25 19:26:31 -0700619 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000620 upcall.portid = 0;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800621 upcall.egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700622
623 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
624 a = nla_next(a, &rem)) {
625 switch (nla_type(a)) {
626 case OVS_USERSPACE_ATTR_USERDATA:
627 upcall.userdata = a;
628 break;
629
630 case OVS_USERSPACE_ATTR_PID:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000631 upcall.portid = nla_get_u32(a);
Jesse Grossccb13522011-10-25 19:26:31 -0700632 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800633
634 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
635 /* Get out tunnel info. */
636 struct vport *vport;
637
638 vport = ovs_vport_rcu(dp, nla_get_u32(a));
639 if (vport) {
640 int err;
641
642 err = ovs_vport_get_egress_tun_info(vport, skb,
643 &info);
644 if (!err)
645 upcall.egress_tun_info = &info;
646 }
647 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700648 }
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800649
650 } /* End of switch. */
Jesse Grossccb13522011-10-25 19:26:31 -0700651 }
652
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800653 return ovs_dp_upcall(dp, skb, key, &upcall);
Jesse Grossccb13522011-10-25 19:26:31 -0700654}
655
656static int sample(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700657 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700658{
659 const struct nlattr *acts_list = NULL;
660 const struct nlattr *a;
661 int rem;
662
663 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
664 a = nla_next(a, &rem)) {
665 switch (nla_type(a)) {
666 case OVS_SAMPLE_ATTR_PROBABILITY:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500667 if (prandom_u32() >= nla_get_u32(a))
Jesse Grossccb13522011-10-25 19:26:31 -0700668 return 0;
669 break;
670
671 case OVS_SAMPLE_ATTR_ACTIONS:
672 acts_list = a;
673 break;
674 }
675 }
676
Simon Horman651887b2014-07-21 15:12:34 -0700677 rem = nla_len(acts_list);
678 a = nla_data(acts_list);
679
Andy Zhou32ae87f2014-09-15 19:33:50 -0700680 /* Actions list is empty, do nothing */
681 if (unlikely(!rem))
682 return 0;
Simon Horman651887b2014-07-21 15:12:34 -0700683
Andy Zhou32ae87f2014-09-15 19:33:50 -0700684 /* The only known usage of sample action is having a single user-space
685 * action. Treat this usage as a special case.
686 * The output_userspace() should clone the skb to be sent to the
687 * user space. This skb will be consumed by its caller.
Simon Horman651887b2014-07-21 15:12:34 -0700688 */
Andy Zhou32ae87f2014-09-15 19:33:50 -0700689 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
Simon Horman941d8eb2014-10-27 16:12:16 +0900690 nla_is_last(a, rem)))
Andy Zhou32ae87f2014-09-15 19:33:50 -0700691 return output_userspace(dp, skb, key, a);
692
693 skb = skb_clone(skb, GFP_ATOMIC);
694 if (!skb)
695 /* Skip the sample action when out of memory. */
696 return 0;
697
Andy Zhou971427f32014-09-15 19:37:25 -0700698 if (!add_deferred_actions(skb, key, a)) {
699 if (net_ratelimit())
700 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
701 ovs_dp_name(dp));
702
703 kfree_skb(skb);
704 }
705 return 0;
706}
707
708static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
709 const struct nlattr *attr)
710{
711 struct ovs_action_hash *hash_act = nla_data(attr);
712 u32 hash = 0;
713
714 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
715 hash = skb_get_hash(skb);
716 hash = jhash_1word(hash, hash_act->hash_basis);
717 if (!hash)
718 hash = 0x1;
719
720 key->ovs_flow_hash = hash;
Jesse Grossccb13522011-10-25 19:26:31 -0700721}
722
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800723static int execute_set_action(struct sk_buff *skb,
724 struct sw_flow_key *flow_key,
725 const struct nlattr *a)
726{
727 /* Only tunnel set execution is supported without a mask. */
728 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
729 OVS_CB(skb)->egress_tun_info = nla_data(a);
730 return 0;
731 }
732
733 return -EINVAL;
734}
735
736/* Mask is at the midpoint of the data. */
737#define get_mask(a, type) ((const type)nla_data(a) + 1)
738
739static int execute_masked_set_action(struct sk_buff *skb,
740 struct sw_flow_key *flow_key,
741 const struct nlattr *a)
Jesse Grossccb13522011-10-25 19:26:31 -0700742{
743 int err = 0;
744
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800745 switch (nla_type(a)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700746 case OVS_KEY_ATTR_PRIORITY:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800747 SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
748 flow_key->phy.priority = skb->priority;
Jesse Grossccb13522011-10-25 19:26:31 -0700749 break;
750
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800751 case OVS_KEY_ATTR_SKB_MARK:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800752 SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
753 flow_key->phy.skb_mark = skb->mark;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800754 break;
755
Jesse Grossf0b128c2014-10-03 15:35:31 -0700756 case OVS_KEY_ATTR_TUNNEL_INFO:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800757 /* Masked data not supported for tunnel. */
758 err = -EINVAL;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700759 break;
760
Jesse Grossccb13522011-10-25 19:26:31 -0700761 case OVS_KEY_ATTR_ETHERNET:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800762 err = set_eth_addr(skb, flow_key, nla_data(a),
763 get_mask(a, struct ovs_key_ethernet *));
Jesse Grossccb13522011-10-25 19:26:31 -0700764 break;
765
766 case OVS_KEY_ATTR_IPV4:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800767 err = set_ipv4(skb, flow_key, nla_data(a),
768 get_mask(a, struct ovs_key_ipv4 *));
Jesse Grossccb13522011-10-25 19:26:31 -0700769 break;
770
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800771 case OVS_KEY_ATTR_IPV6:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800772 err = set_ipv6(skb, flow_key, nla_data(a),
773 get_mask(a, struct ovs_key_ipv6 *));
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800774 break;
775
Jesse Grossccb13522011-10-25 19:26:31 -0700776 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800777 err = set_tcp(skb, flow_key, nla_data(a),
778 get_mask(a, struct ovs_key_tcp *));
Jesse Grossccb13522011-10-25 19:26:31 -0700779 break;
780
781 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800782 err = set_udp(skb, flow_key, nla_data(a),
783 get_mask(a, struct ovs_key_udp *));
Jesse Grossccb13522011-10-25 19:26:31 -0700784 break;
Joe Stringera175a722013-08-22 12:30:48 -0700785
786 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800787 err = set_sctp(skb, flow_key, nla_data(a),
788 get_mask(a, struct ovs_key_sctp *));
Joe Stringera175a722013-08-22 12:30:48 -0700789 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700790
791 case OVS_KEY_ATTR_MPLS:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800792 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
793 __be32 *));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700794 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700795 }
796
797 return err;
798}
799
Andy Zhou971427f32014-09-15 19:37:25 -0700800static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
801 struct sw_flow_key *key,
802 const struct nlattr *a, int rem)
803{
804 struct deferred_action *da;
Andy Zhou971427f32014-09-15 19:37:25 -0700805
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800806 if (!is_flow_key_valid(key)) {
807 int err;
808
809 err = ovs_flow_key_update(skb, key);
810 if (err)
811 return err;
812 }
813 BUG_ON(!is_flow_key_valid(key));
Andy Zhou971427f32014-09-15 19:37:25 -0700814
Simon Horman941d8eb2014-10-27 16:12:16 +0900815 if (!nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700816 /* Recirc action is the not the last action
817 * of the action list, need to clone the skb.
818 */
819 skb = skb_clone(skb, GFP_ATOMIC);
820
821 /* Skip the recirc action when out of memory, but
822 * continue on with the rest of the action list.
823 */
824 if (!skb)
825 return 0;
826 }
827
828 da = add_deferred_actions(skb, key, NULL);
829 if (da) {
830 da->pkt_key.recirc_id = nla_get_u32(a);
831 } else {
832 kfree_skb(skb);
833
834 if (net_ratelimit())
835 pr_warn("%s: deferred action limit reached, drop recirc action\n",
836 ovs_dp_name(dp));
837 }
838
839 return 0;
840}
841
Jesse Grossccb13522011-10-25 19:26:31 -0700842/* Execute a list of actions against 'skb'. */
843static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700844 struct sw_flow_key *key,
Simon Horman651887b2014-07-21 15:12:34 -0700845 const struct nlattr *attr, int len)
Jesse Grossccb13522011-10-25 19:26:31 -0700846{
847 /* Every output action needs a separate clone of 'skb', but the common
848 * case is just a single output action, so that doing a clone and
849 * then freeing the original skbuff is wasteful. So the following code
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800850 * is slightly obscure just to avoid that.
851 */
Jesse Grossccb13522011-10-25 19:26:31 -0700852 int prev_port = -1;
853 const struct nlattr *a;
854 int rem;
855
856 for (a = attr, rem = len; rem > 0;
857 a = nla_next(a, &rem)) {
858 int err = 0;
859
Andy Zhou738967b2014-09-08 00:35:02 -0700860 if (unlikely(prev_port != -1)) {
861 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
862
863 if (out_skb)
864 do_output(dp, out_skb, prev_port);
865
Jesse Grossccb13522011-10-25 19:26:31 -0700866 prev_port = -1;
867 }
868
869 switch (nla_type(a)) {
870 case OVS_ACTION_ATTR_OUTPUT:
871 prev_port = nla_get_u32(a);
872 break;
873
874 case OVS_ACTION_ATTR_USERSPACE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700875 output_userspace(dp, skb, key, a);
Jesse Grossccb13522011-10-25 19:26:31 -0700876 break;
877
Andy Zhou971427f32014-09-15 19:37:25 -0700878 case OVS_ACTION_ATTR_HASH:
879 execute_hash(skb, key, a);
880 break;
881
Simon Horman25cd9ba2014-10-06 05:05:13 -0700882 case OVS_ACTION_ATTR_PUSH_MPLS:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800883 err = push_mpls(skb, key, nla_data(a));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700884 break;
885
886 case OVS_ACTION_ATTR_POP_MPLS:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800887 err = pop_mpls(skb, key, nla_get_be16(a));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700888 break;
889
Jesse Grossccb13522011-10-25 19:26:31 -0700890 case OVS_ACTION_ATTR_PUSH_VLAN:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800891 err = push_vlan(skb, key, nla_data(a));
Jesse Grossccb13522011-10-25 19:26:31 -0700892 break;
893
894 case OVS_ACTION_ATTR_POP_VLAN:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800895 err = pop_vlan(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700896 break;
897
Andy Zhou971427f32014-09-15 19:37:25 -0700898 case OVS_ACTION_ATTR_RECIRC:
899 err = execute_recirc(dp, skb, key, a, rem);
Simon Horman941d8eb2014-10-27 16:12:16 +0900900 if (nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700901 /* If this is the last action, the skb has
902 * been consumed or freed.
903 * Return immediately.
904 */
905 return err;
906 }
907 break;
908
Jesse Grossccb13522011-10-25 19:26:31 -0700909 case OVS_ACTION_ATTR_SET:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800910 err = execute_set_action(skb, key, nla_data(a));
Jesse Grossccb13522011-10-25 19:26:31 -0700911 break;
912
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -0800913 case OVS_ACTION_ATTR_SET_MASKED:
914 case OVS_ACTION_ATTR_SET_TO_MASKED:
915 err = execute_masked_set_action(skb, key, nla_data(a));
916 break;
917
Jesse Grossccb13522011-10-25 19:26:31 -0700918 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700919 err = sample(dp, skb, key, a);
Jesse Grossccb13522011-10-25 19:26:31 -0700920 break;
921 }
922
923 if (unlikely(err)) {
924 kfree_skb(skb);
925 return err;
926 }
927 }
928
Simon Horman651887b2014-07-21 15:12:34 -0700929 if (prev_port != -1)
Jesse Grossccb13522011-10-25 19:26:31 -0700930 do_output(dp, skb, prev_port);
Simon Horman651887b2014-07-21 15:12:34 -0700931 else
Jesse Grossccb13522011-10-25 19:26:31 -0700932 consume_skb(skb);
933
934 return 0;
935}
936
Andy Zhou971427f32014-09-15 19:37:25 -0700937static void process_deferred_actions(struct datapath *dp)
938{
939 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
940
941 /* Do not touch the FIFO in case there is no deferred actions. */
942 if (action_fifo_is_empty(fifo))
943 return;
944
945 /* Finishing executing all deferred actions. */
946 do {
947 struct deferred_action *da = action_fifo_get(fifo);
948 struct sk_buff *skb = da->skb;
949 struct sw_flow_key *key = &da->pkt_key;
950 const struct nlattr *actions = da->actions;
951
952 if (actions)
953 do_execute_actions(dp, skb, key, actions,
954 nla_len(actions));
955 else
956 ovs_dp_process_packet(skb, key);
957 } while (!action_fifo_is_empty(fifo));
958
959 /* Reset FIFO for the next packet. */
960 action_fifo_init(fifo);
961}
962
Jesse Grossccb13522011-10-25 19:26:31 -0700963/* Execute a list of actions against 'skb'. */
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700964int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800965 const struct sw_flow_actions *acts,
966 struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700967{
Andy Zhou971427f32014-09-15 19:37:25 -0700968 int level = this_cpu_read(exec_actions_level);
Andy Zhou971427f32014-09-15 19:37:25 -0700969 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700970
Andy Zhou971427f32014-09-15 19:37:25 -0700971 this_cpu_inc(exec_actions_level);
Jesse Grossf0b128c2014-10-03 15:35:31 -0700972 OVS_CB(skb)->egress_tun_info = NULL;
Andy Zhou971427f32014-09-15 19:37:25 -0700973 err = do_execute_actions(dp, skb, key,
974 acts->actions, acts->actions_len);
975
976 if (!level)
977 process_deferred_actions(dp);
978
979 this_cpu_dec(exec_actions_level);
980 return err;
981}
982
983int action_fifos_init(void)
984{
985 action_fifos = alloc_percpu(struct action_fifo);
986 if (!action_fifos)
987 return -ENOMEM;
988
989 return 0;
990}
991
992void action_fifos_exit(void)
993{
994 free_percpu(action_fifos);
Jesse Grossccb13522011-10-25 19:26:31 -0700995}