blob: 9fd33c0bf173ef18626c9c15d5d99834a70fb6fe [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
72static bool action_fifo_is_empty(struct action_fifo *fifo)
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,
95 struct sw_flow_key *key,
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
Jesse Grossccb13522011-10-25 19:26:31 -0700112static int make_writable(struct sk_buff *skb, int write_len)
113{
Jiri Benc2ba5af42014-08-21 21:33:44 +0200114 if (!pskb_may_pull(skb, write_len))
115 return -ENOMEM;
116
Jesse Grossccb13522011-10-25 19:26:31 -0700117 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
118 return 0;
119
120 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
121}
122
Simon Horman25cd9ba2014-10-06 05:05:13 -0700123static int push_mpls(struct sk_buff *skb,
124 const struct ovs_action_push_mpls *mpls)
125{
126 __be32 *new_mpls_lse;
127 struct ethhdr *hdr;
128
129 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
130 if (skb->encapsulation)
131 return -ENOTSUPP;
132
133 if (skb_cow_head(skb, MPLS_HLEN) < 0)
134 return -ENOMEM;
135
136 skb_push(skb, MPLS_HLEN);
137 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
138 skb->mac_len);
139 skb_reset_mac_header(skb);
140
141 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
142 *new_mpls_lse = mpls->mpls_lse;
143
144 if (skb->ip_summed == CHECKSUM_COMPLETE)
145 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
146 MPLS_HLEN, 0));
147
148 hdr = eth_hdr(skb);
149 hdr->h_proto = mpls->mpls_ethertype;
150
151 skb_set_inner_protocol(skb, skb->protocol);
152 skb->protocol = mpls->mpls_ethertype;
153
154 return 0;
155}
156
157static int pop_mpls(struct sk_buff *skb, const __be16 ethertype)
158{
159 struct ethhdr *hdr;
160 int err;
161
162 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
163 if (unlikely(err))
164 return err;
165
166 if (skb->ip_summed == CHECKSUM_COMPLETE)
167 skb->csum = csum_sub(skb->csum,
168 csum_partial(skb_mpls_header(skb),
169 MPLS_HLEN, 0));
170
171 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
172 skb->mac_len);
173
174 __skb_pull(skb, MPLS_HLEN);
175 skb_reset_mac_header(skb);
176
177 /* skb_mpls_header() is used to locate the ethertype
178 * field correctly in the presence of VLAN tags.
179 */
180 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
181 hdr->h_proto = ethertype;
182 if (eth_p_mpls(skb->protocol))
183 skb->protocol = ethertype;
184 return 0;
185}
186
187static int set_mpls(struct sk_buff *skb, const __be32 *mpls_lse)
188{
189 __be32 *stack;
190 int err;
191
192 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
193 if (unlikely(err))
194 return err;
195
196 stack = (__be32 *)skb_mpls_header(skb);
197 if (skb->ip_summed == CHECKSUM_COMPLETE) {
198 __be32 diff[] = { ~(*stack), *mpls_lse };
199
200 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
201 ~skb->csum);
202 }
203
204 *stack = *mpls_lse;
205
206 return 0;
207}
208
Joe Stringer39855b52012-08-31 15:28:28 -0700209/* remove VLAN header from packet and update csum accordingly. */
Jesse Grossccb13522011-10-25 19:26:31 -0700210static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
211{
212 struct vlan_hdr *vhdr;
213 int err;
214
215 err = make_writable(skb, VLAN_ETH_HLEN);
216 if (unlikely(err))
217 return err;
218
219 if (skb->ip_summed == CHECKSUM_COMPLETE)
220 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
Cong Wang7b024082013-02-22 17:32:26 +0800221 + (2 * ETH_ALEN), VLAN_HLEN, 0));
Jesse Grossccb13522011-10-25 19:26:31 -0700222
223 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
224 *current_tci = vhdr->h_vlan_TCI;
225
226 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
227 __skb_pull(skb, VLAN_HLEN);
228
229 vlan_set_encap_proto(skb, vhdr);
230 skb->mac_header += VLAN_HLEN;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700231
Jiri Benc2ba5af42014-08-21 21:33:44 +0200232 if (skb_network_offset(skb) < ETH_HLEN)
233 skb_set_network_header(skb, ETH_HLEN);
Jesse Grossccb13522011-10-25 19:26:31 -0700234
Simon Horman25cd9ba2014-10-06 05:05:13 -0700235 /* Update mac_len for subsequent MPLS actions */
236 skb_reset_mac_len(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700237 return 0;
238}
239
240static int pop_vlan(struct sk_buff *skb)
241{
242 __be16 tci;
243 int err;
244
245 if (likely(vlan_tx_tag_present(skb))) {
246 skb->vlan_tci = 0;
247 } else {
248 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
249 skb->len < VLAN_ETH_HLEN))
250 return 0;
251
252 err = __pop_vlan_tci(skb, &tci);
253 if (err)
254 return err;
255 }
256 /* move next vlan tag to hw accel tag */
257 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
258 skb->len < VLAN_ETH_HLEN))
259 return 0;
260
261 err = __pop_vlan_tci(skb, &tci);
262 if (unlikely(err))
263 return err;
264
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000265 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
Jesse Grossccb13522011-10-25 19:26:31 -0700266 return 0;
267}
268
269static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
270{
271 if (unlikely(vlan_tx_tag_present(skb))) {
272 u16 current_tag;
273
274 /* push down current VLAN tag */
275 current_tag = vlan_tx_tag_get(skb);
276
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000277 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
Jesse Grossccb13522011-10-25 19:26:31 -0700278 return -ENOMEM;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700279 /* Update mac_len for subsequent MPLS actions */
280 skb->mac_len += VLAN_HLEN;
Jesse Grossccb13522011-10-25 19:26:31 -0700281
282 if (skb->ip_summed == CHECKSUM_COMPLETE)
283 skb->csum = csum_add(skb->csum, csum_partial(skb->data
Cong Wang7b024082013-02-22 17:32:26 +0800284 + (2 * ETH_ALEN), VLAN_HLEN, 0));
Jesse Grossccb13522011-10-25 19:26:31 -0700285
286 }
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000287 __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
Jesse Grossccb13522011-10-25 19:26:31 -0700288 return 0;
289}
290
291static int set_eth_addr(struct sk_buff *skb,
292 const struct ovs_key_ethernet *eth_key)
293{
294 int err;
295 err = make_writable(skb, ETH_HLEN);
296 if (unlikely(err))
297 return err;
298
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700299 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
300
Joe Perches8c63ff02014-02-18 11:15:45 -0800301 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
302 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700303
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700304 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
305
Jesse Grossccb13522011-10-25 19:26:31 -0700306 return 0;
307}
308
309static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
310 __be32 *addr, __be32 new_addr)
311{
312 int transport_len = skb->len - skb_transport_offset(skb);
313
314 if (nh->protocol == IPPROTO_TCP) {
315 if (likely(transport_len >= sizeof(struct tcphdr)))
316 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
317 *addr, new_addr, 1);
318 } else if (nh->protocol == IPPROTO_UDP) {
Jesse Gross81e5d412012-03-06 15:05:46 -0800319 if (likely(transport_len >= sizeof(struct udphdr))) {
320 struct udphdr *uh = udp_hdr(skb);
321
322 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
323 inet_proto_csum_replace4(&uh->check, skb,
324 *addr, new_addr, 1);
325 if (!uh->check)
326 uh->check = CSUM_MANGLED_0;
327 }
328 }
Jesse Grossccb13522011-10-25 19:26:31 -0700329 }
330
331 csum_replace4(&nh->check, *addr, new_addr);
Tom Herbert7539fad2013-12-15 22:12:18 -0800332 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700333 *addr = new_addr;
334}
335
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800336static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
337 __be32 addr[4], const __be32 new_addr[4])
338{
339 int transport_len = skb->len - skb_transport_offset(skb);
340
341 if (l4_proto == IPPROTO_TCP) {
342 if (likely(transport_len >= sizeof(struct tcphdr)))
343 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
344 addr, new_addr, 1);
345 } else if (l4_proto == IPPROTO_UDP) {
346 if (likely(transport_len >= sizeof(struct udphdr))) {
347 struct udphdr *uh = udp_hdr(skb);
348
349 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
350 inet_proto_csum_replace16(&uh->check, skb,
351 addr, new_addr, 1);
352 if (!uh->check)
353 uh->check = CSUM_MANGLED_0;
354 }
355 }
356 }
357}
358
359static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
360 __be32 addr[4], const __be32 new_addr[4],
361 bool recalculate_csum)
362{
363 if (recalculate_csum)
364 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
365
Tom Herbert7539fad2013-12-15 22:12:18 -0800366 skb_clear_hash(skb);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800367 memcpy(addr, new_addr, sizeof(__be32[4]));
368}
369
370static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
371{
372 nh->priority = tc >> 4;
373 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
374}
375
376static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
377{
378 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
379 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
380 nh->flow_lbl[2] = fl & 0x000000FF;
381}
382
Jesse Grossccb13522011-10-25 19:26:31 -0700383static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
384{
385 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
386 nh->ttl = new_ttl;
387}
388
389static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
390{
391 struct iphdr *nh;
392 int err;
393
394 err = make_writable(skb, skb_network_offset(skb) +
395 sizeof(struct iphdr));
396 if (unlikely(err))
397 return err;
398
399 nh = ip_hdr(skb);
400
401 if (ipv4_key->ipv4_src != nh->saddr)
402 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
403
404 if (ipv4_key->ipv4_dst != nh->daddr)
405 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
406
407 if (ipv4_key->ipv4_tos != nh->tos)
408 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
409
410 if (ipv4_key->ipv4_ttl != nh->ttl)
411 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
412
413 return 0;
414}
415
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800416static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
417{
418 struct ipv6hdr *nh;
419 int err;
420 __be32 *saddr;
421 __be32 *daddr;
422
423 err = make_writable(skb, skb_network_offset(skb) +
424 sizeof(struct ipv6hdr));
425 if (unlikely(err))
426 return err;
427
428 nh = ipv6_hdr(skb);
429 saddr = (__be32 *)&nh->saddr;
430 daddr = (__be32 *)&nh->daddr;
431
432 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
433 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
434 ipv6_key->ipv6_src, true);
435
436 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
437 unsigned int offset = 0;
438 int flags = IP6_FH_F_SKIP_RH;
439 bool recalc_csum = true;
440
441 if (ipv6_ext_hdr(nh->nexthdr))
442 recalc_csum = ipv6_find_hdr(skb, &offset,
443 NEXTHDR_ROUTING, NULL,
444 &flags) != NEXTHDR_ROUTING;
445
446 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
447 ipv6_key->ipv6_dst, recalc_csum);
448 }
449
450 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
451 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
452 nh->hop_limit = ipv6_key->ipv6_hlimit;
453
454 return 0;
455}
456
Jesse Grossccb13522011-10-25 19:26:31 -0700457/* Must follow make_writable() since that can move the skb data. */
458static void set_tp_port(struct sk_buff *skb, __be16 *port,
459 __be16 new_port, __sum16 *check)
460{
461 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
462 *port = new_port;
Tom Herbert7539fad2013-12-15 22:12:18 -0800463 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700464}
465
Jesse Gross81e5d412012-03-06 15:05:46 -0800466static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
467{
468 struct udphdr *uh = udp_hdr(skb);
469
470 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
471 set_tp_port(skb, port, new_port, &uh->check);
472
473 if (!uh->check)
474 uh->check = CSUM_MANGLED_0;
475 } else {
476 *port = new_port;
Tom Herbert7539fad2013-12-15 22:12:18 -0800477 skb_clear_hash(skb);
Jesse Gross81e5d412012-03-06 15:05:46 -0800478 }
479}
480
481static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
Jesse Grossccb13522011-10-25 19:26:31 -0700482{
483 struct udphdr *uh;
484 int err;
485
486 err = make_writable(skb, skb_transport_offset(skb) +
487 sizeof(struct udphdr));
488 if (unlikely(err))
489 return err;
490
491 uh = udp_hdr(skb);
492 if (udp_port_key->udp_src != uh->source)
Jesse Gross81e5d412012-03-06 15:05:46 -0800493 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
Jesse Grossccb13522011-10-25 19:26:31 -0700494
495 if (udp_port_key->udp_dst != uh->dest)
Jesse Gross81e5d412012-03-06 15:05:46 -0800496 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700497
498 return 0;
499}
500
Jesse Gross81e5d412012-03-06 15:05:46 -0800501static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
Jesse Grossccb13522011-10-25 19:26:31 -0700502{
503 struct tcphdr *th;
504 int err;
505
506 err = make_writable(skb, skb_transport_offset(skb) +
507 sizeof(struct tcphdr));
508 if (unlikely(err))
509 return err;
510
511 th = tcp_hdr(skb);
512 if (tcp_port_key->tcp_src != th->source)
513 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
514
515 if (tcp_port_key->tcp_dst != th->dest)
516 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
517
518 return 0;
519}
520
Joe Stringera175a722013-08-22 12:30:48 -0700521static int set_sctp(struct sk_buff *skb,
522 const struct ovs_key_sctp *sctp_port_key)
523{
524 struct sctphdr *sh;
525 int err;
526 unsigned int sctphoff = skb_transport_offset(skb);
527
528 err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
529 if (unlikely(err))
530 return err;
531
532 sh = sctp_hdr(skb);
533 if (sctp_port_key->sctp_src != sh->source ||
534 sctp_port_key->sctp_dst != sh->dest) {
535 __le32 old_correct_csum, new_csum, old_csum;
536
537 old_csum = sh->checksum;
538 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
539
540 sh->source = sctp_port_key->sctp_src;
541 sh->dest = sctp_port_key->sctp_dst;
542
543 new_csum = sctp_compute_cksum(skb, sctphoff);
544
545 /* Carry any checksum errors through. */
546 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
547
Tom Herbert7539fad2013-12-15 22:12:18 -0800548 skb_clear_hash(skb);
Joe Stringera175a722013-08-22 12:30:48 -0700549 }
550
551 return 0;
552}
553
Andy Zhou738967b2014-09-08 00:35:02 -0700554static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
Jesse Grossccb13522011-10-25 19:26:31 -0700555{
Andy Zhou738967b2014-09-08 00:35:02 -0700556 struct vport *vport = ovs_vport_rcu(dp, out_port);
Jesse Grossccb13522011-10-25 19:26:31 -0700557
Andy Zhou738967b2014-09-08 00:35:02 -0700558 if (likely(vport))
559 ovs_vport_send(vport, skb);
560 else
Jesse Grossccb13522011-10-25 19:26:31 -0700561 kfree_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700562}
563
564static int output_userspace(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700565 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700566{
567 struct dp_upcall_info upcall;
568 const struct nlattr *a;
569 int rem;
570
571 upcall.cmd = OVS_PACKET_CMD_ACTION;
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700572 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700573 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000574 upcall.portid = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700575
576 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
577 a = nla_next(a, &rem)) {
578 switch (nla_type(a)) {
579 case OVS_USERSPACE_ATTR_USERDATA:
580 upcall.userdata = a;
581 break;
582
583 case OVS_USERSPACE_ATTR_PID:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000584 upcall.portid = nla_get_u32(a);
Jesse Grossccb13522011-10-25 19:26:31 -0700585 break;
586 }
587 }
588
589 return ovs_dp_upcall(dp, skb, &upcall);
590}
591
592static int sample(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700593 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700594{
595 const struct nlattr *acts_list = NULL;
596 const struct nlattr *a;
597 int rem;
598
599 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
600 a = nla_next(a, &rem)) {
601 switch (nla_type(a)) {
602 case OVS_SAMPLE_ATTR_PROBABILITY:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500603 if (prandom_u32() >= nla_get_u32(a))
Jesse Grossccb13522011-10-25 19:26:31 -0700604 return 0;
605 break;
606
607 case OVS_SAMPLE_ATTR_ACTIONS:
608 acts_list = a;
609 break;
610 }
611 }
612
Simon Horman651887b2014-07-21 15:12:34 -0700613 rem = nla_len(acts_list);
614 a = nla_data(acts_list);
615
Andy Zhou32ae87f2014-09-15 19:33:50 -0700616 /* Actions list is empty, do nothing */
617 if (unlikely(!rem))
618 return 0;
Simon Horman651887b2014-07-21 15:12:34 -0700619
Andy Zhou32ae87f2014-09-15 19:33:50 -0700620 /* The only known usage of sample action is having a single user-space
621 * action. Treat this usage as a special case.
622 * The output_userspace() should clone the skb to be sent to the
623 * user space. This skb will be consumed by its caller.
Simon Horman651887b2014-07-21 15:12:34 -0700624 */
Andy Zhou32ae87f2014-09-15 19:33:50 -0700625 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
Simon Horman941d8eb2014-10-27 16:12:16 +0900626 nla_is_last(a, rem)))
Andy Zhou32ae87f2014-09-15 19:33:50 -0700627 return output_userspace(dp, skb, key, a);
628
629 skb = skb_clone(skb, GFP_ATOMIC);
630 if (!skb)
631 /* Skip the sample action when out of memory. */
632 return 0;
633
Andy Zhou971427f32014-09-15 19:37:25 -0700634 if (!add_deferred_actions(skb, key, a)) {
635 if (net_ratelimit())
636 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
637 ovs_dp_name(dp));
638
639 kfree_skb(skb);
640 }
641 return 0;
642}
643
644static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
645 const struct nlattr *attr)
646{
647 struct ovs_action_hash *hash_act = nla_data(attr);
648 u32 hash = 0;
649
650 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
651 hash = skb_get_hash(skb);
652 hash = jhash_1word(hash, hash_act->hash_basis);
653 if (!hash)
654 hash = 0x1;
655
656 key->ovs_flow_hash = hash;
Jesse Grossccb13522011-10-25 19:26:31 -0700657}
658
659static int execute_set_action(struct sk_buff *skb,
660 const struct nlattr *nested_attr)
661{
662 int err = 0;
663
664 switch (nla_type(nested_attr)) {
665 case OVS_KEY_ATTR_PRIORITY:
666 skb->priority = nla_get_u32(nested_attr);
667 break;
668
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800669 case OVS_KEY_ATTR_SKB_MARK:
670 skb->mark = nla_get_u32(nested_attr);
671 break;
672
Jesse Grossf0b128c2014-10-03 15:35:31 -0700673 case OVS_KEY_ATTR_TUNNEL_INFO:
674 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700675 break;
676
Jesse Grossccb13522011-10-25 19:26:31 -0700677 case OVS_KEY_ATTR_ETHERNET:
678 err = set_eth_addr(skb, nla_data(nested_attr));
679 break;
680
681 case OVS_KEY_ATTR_IPV4:
682 err = set_ipv4(skb, nla_data(nested_attr));
683 break;
684
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800685 case OVS_KEY_ATTR_IPV6:
686 err = set_ipv6(skb, nla_data(nested_attr));
687 break;
688
Jesse Grossccb13522011-10-25 19:26:31 -0700689 case OVS_KEY_ATTR_TCP:
Jesse Gross81e5d412012-03-06 15:05:46 -0800690 err = set_tcp(skb, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700691 break;
692
693 case OVS_KEY_ATTR_UDP:
Jesse Gross81e5d412012-03-06 15:05:46 -0800694 err = set_udp(skb, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700695 break;
Joe Stringera175a722013-08-22 12:30:48 -0700696
697 case OVS_KEY_ATTR_SCTP:
698 err = set_sctp(skb, nla_data(nested_attr));
699 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700700
701 case OVS_KEY_ATTR_MPLS:
702 err = set_mpls(skb, nla_data(nested_attr));
703 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700704 }
705
706 return err;
707}
708
Andy Zhou971427f32014-09-15 19:37:25 -0700709static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
710 struct sw_flow_key *key,
711 const struct nlattr *a, int rem)
712{
713 struct deferred_action *da;
714 int err;
715
716 err = ovs_flow_key_update(skb, key);
717 if (err)
718 return err;
719
Simon Horman941d8eb2014-10-27 16:12:16 +0900720 if (!nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700721 /* Recirc action is the not the last action
722 * of the action list, need to clone the skb.
723 */
724 skb = skb_clone(skb, GFP_ATOMIC);
725
726 /* Skip the recirc action when out of memory, but
727 * continue on with the rest of the action list.
728 */
729 if (!skb)
730 return 0;
731 }
732
733 da = add_deferred_actions(skb, key, NULL);
734 if (da) {
735 da->pkt_key.recirc_id = nla_get_u32(a);
736 } else {
737 kfree_skb(skb);
738
739 if (net_ratelimit())
740 pr_warn("%s: deferred action limit reached, drop recirc action\n",
741 ovs_dp_name(dp));
742 }
743
744 return 0;
745}
746
Jesse Grossccb13522011-10-25 19:26:31 -0700747/* Execute a list of actions against 'skb'. */
748static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700749 struct sw_flow_key *key,
Simon Horman651887b2014-07-21 15:12:34 -0700750 const struct nlattr *attr, int len)
Jesse Grossccb13522011-10-25 19:26:31 -0700751{
752 /* Every output action needs a separate clone of 'skb', but the common
753 * case is just a single output action, so that doing a clone and
754 * then freeing the original skbuff is wasteful. So the following code
755 * is slightly obscure just to avoid that. */
756 int prev_port = -1;
757 const struct nlattr *a;
758 int rem;
759
760 for (a = attr, rem = len; rem > 0;
761 a = nla_next(a, &rem)) {
762 int err = 0;
763
Andy Zhou738967b2014-09-08 00:35:02 -0700764 if (unlikely(prev_port != -1)) {
765 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
766
767 if (out_skb)
768 do_output(dp, out_skb, prev_port);
769
Jesse Grossccb13522011-10-25 19:26:31 -0700770 prev_port = -1;
771 }
772
773 switch (nla_type(a)) {
774 case OVS_ACTION_ATTR_OUTPUT:
775 prev_port = nla_get_u32(a);
776 break;
777
778 case OVS_ACTION_ATTR_USERSPACE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700779 output_userspace(dp, skb, key, a);
Jesse Grossccb13522011-10-25 19:26:31 -0700780 break;
781
Andy Zhou971427f32014-09-15 19:37:25 -0700782 case OVS_ACTION_ATTR_HASH:
783 execute_hash(skb, key, a);
784 break;
785
Simon Horman25cd9ba2014-10-06 05:05:13 -0700786 case OVS_ACTION_ATTR_PUSH_MPLS:
787 err = push_mpls(skb, nla_data(a));
788 break;
789
790 case OVS_ACTION_ATTR_POP_MPLS:
791 err = pop_mpls(skb, nla_get_be16(a));
792 break;
793
Jesse Grossccb13522011-10-25 19:26:31 -0700794 case OVS_ACTION_ATTR_PUSH_VLAN:
795 err = push_vlan(skb, nla_data(a));
796 if (unlikely(err)) /* skb already freed. */
797 return err;
798 break;
799
800 case OVS_ACTION_ATTR_POP_VLAN:
801 err = pop_vlan(skb);
802 break;
803
Andy Zhou971427f32014-09-15 19:37:25 -0700804 case OVS_ACTION_ATTR_RECIRC:
805 err = execute_recirc(dp, skb, key, a, rem);
Simon Horman941d8eb2014-10-27 16:12:16 +0900806 if (nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700807 /* If this is the last action, the skb has
808 * been consumed or freed.
809 * Return immediately.
810 */
811 return err;
812 }
813 break;
814
Jesse Grossccb13522011-10-25 19:26:31 -0700815 case OVS_ACTION_ATTR_SET:
816 err = execute_set_action(skb, nla_data(a));
817 break;
818
819 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700820 err = sample(dp, skb, key, a);
Andy Zhoufe984c02014-05-06 17:23:48 -0700821 if (unlikely(err)) /* skb already freed. */
822 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700823 break;
824 }
825
826 if (unlikely(err)) {
827 kfree_skb(skb);
828 return err;
829 }
830 }
831
Simon Horman651887b2014-07-21 15:12:34 -0700832 if (prev_port != -1)
Jesse Grossccb13522011-10-25 19:26:31 -0700833 do_output(dp, skb, prev_port);
Simon Horman651887b2014-07-21 15:12:34 -0700834 else
Jesse Grossccb13522011-10-25 19:26:31 -0700835 consume_skb(skb);
836
837 return 0;
838}
839
Andy Zhou971427f32014-09-15 19:37:25 -0700840static void process_deferred_actions(struct datapath *dp)
841{
842 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
843
844 /* Do not touch the FIFO in case there is no deferred actions. */
845 if (action_fifo_is_empty(fifo))
846 return;
847
848 /* Finishing executing all deferred actions. */
849 do {
850 struct deferred_action *da = action_fifo_get(fifo);
851 struct sk_buff *skb = da->skb;
852 struct sw_flow_key *key = &da->pkt_key;
853 const struct nlattr *actions = da->actions;
854
855 if (actions)
856 do_execute_actions(dp, skb, key, actions,
857 nla_len(actions));
858 else
859 ovs_dp_process_packet(skb, key);
860 } while (!action_fifo_is_empty(fifo));
861
862 /* Reset FIFO for the next packet. */
863 action_fifo_init(fifo);
864}
865
Jesse Grossccb13522011-10-25 19:26:31 -0700866/* Execute a list of actions against 'skb'. */
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700867int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
868 struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700869{
Andy Zhou971427f32014-09-15 19:37:25 -0700870 int level = this_cpu_read(exec_actions_level);
871 struct sw_flow_actions *acts;
872 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700873
Andy Zhou971427f32014-09-15 19:37:25 -0700874 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
875
876 this_cpu_inc(exec_actions_level);
Jesse Grossf0b128c2014-10-03 15:35:31 -0700877 OVS_CB(skb)->egress_tun_info = NULL;
Andy Zhou971427f32014-09-15 19:37:25 -0700878 err = do_execute_actions(dp, skb, key,
879 acts->actions, acts->actions_len);
880
881 if (!level)
882 process_deferred_actions(dp);
883
884 this_cpu_dec(exec_actions_level);
885 return err;
886}
887
888int action_fifos_init(void)
889{
890 action_fifos = alloc_percpu(struct action_fifo);
891 if (!action_fifos)
892 return -ENOMEM;
893
894 return 0;
895}
896
897void action_fifos_exit(void)
898{
899 free_percpu(action_fifos);
Jesse Grossccb13522011-10-25 19:26:31 -0700900}