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