blob: 930b1b6e4cef26a1569ba284e3d6818a8ff63069 [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
Jesse Grossccb13522011-10-25 19:26:31 -0700554static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
555{
556 struct vport *vport;
557
558 if (unlikely(!skb))
559 return -ENOMEM;
560
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700561 vport = ovs_vport_rcu(dp, out_port);
Jesse Grossccb13522011-10-25 19:26:31 -0700562 if (unlikely(!vport)) {
563 kfree_skb(skb);
564 return -ENODEV;
565 }
566
567 ovs_vport_send(vport, skb);
568 return 0;
569}
570
571static int output_userspace(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700572 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700573{
574 struct dp_upcall_info upcall;
575 const struct nlattr *a;
576 int rem;
577
578 upcall.cmd = OVS_PACKET_CMD_ACTION;
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700579 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700580 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000581 upcall.portid = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700582
583 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
584 a = nla_next(a, &rem)) {
585 switch (nla_type(a)) {
586 case OVS_USERSPACE_ATTR_USERDATA:
587 upcall.userdata = a;
588 break;
589
590 case OVS_USERSPACE_ATTR_PID:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000591 upcall.portid = nla_get_u32(a);
Jesse Grossccb13522011-10-25 19:26:31 -0700592 break;
593 }
594 }
595
596 return ovs_dp_upcall(dp, skb, &upcall);
597}
598
599static int sample(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700600 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700601{
602 const struct nlattr *acts_list = NULL;
603 const struct nlattr *a;
604 int rem;
605
606 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
607 a = nla_next(a, &rem)) {
608 switch (nla_type(a)) {
609 case OVS_SAMPLE_ATTR_PROBABILITY:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500610 if (prandom_u32() >= nla_get_u32(a))
Jesse Grossccb13522011-10-25 19:26:31 -0700611 return 0;
612 break;
613
614 case OVS_SAMPLE_ATTR_ACTIONS:
615 acts_list = a;
616 break;
617 }
618 }
619
Simon Horman651887b2014-07-21 15:12:34 -0700620 rem = nla_len(acts_list);
621 a = nla_data(acts_list);
622
Andy Zhou32ae87f2014-09-15 19:33:50 -0700623 /* Actions list is empty, do nothing */
624 if (unlikely(!rem))
625 return 0;
Simon Horman651887b2014-07-21 15:12:34 -0700626
Andy Zhou32ae87f2014-09-15 19:33:50 -0700627 /* The only known usage of sample action is having a single user-space
628 * action. Treat this usage as a special case.
629 * The output_userspace() should clone the skb to be sent to the
630 * user space. This skb will be consumed by its caller.
Simon Horman651887b2014-07-21 15:12:34 -0700631 */
Andy Zhou32ae87f2014-09-15 19:33:50 -0700632 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
Simon Horman941d8eb2014-10-27 16:12:16 +0900633 nla_is_last(a, rem)))
Andy Zhou32ae87f2014-09-15 19:33:50 -0700634 return output_userspace(dp, skb, key, a);
635
636 skb = skb_clone(skb, GFP_ATOMIC);
637 if (!skb)
638 /* Skip the sample action when out of memory. */
639 return 0;
640
Andy Zhou971427f32014-09-15 19:37:25 -0700641 if (!add_deferred_actions(skb, key, a)) {
642 if (net_ratelimit())
643 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
644 ovs_dp_name(dp));
645
646 kfree_skb(skb);
647 }
648 return 0;
649}
650
651static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
652 const struct nlattr *attr)
653{
654 struct ovs_action_hash *hash_act = nla_data(attr);
655 u32 hash = 0;
656
657 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
658 hash = skb_get_hash(skb);
659 hash = jhash_1word(hash, hash_act->hash_basis);
660 if (!hash)
661 hash = 0x1;
662
663 key->ovs_flow_hash = hash;
Jesse Grossccb13522011-10-25 19:26:31 -0700664}
665
666static int execute_set_action(struct sk_buff *skb,
667 const struct nlattr *nested_attr)
668{
669 int err = 0;
670
671 switch (nla_type(nested_attr)) {
672 case OVS_KEY_ATTR_PRIORITY:
673 skb->priority = nla_get_u32(nested_attr);
674 break;
675
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800676 case OVS_KEY_ATTR_SKB_MARK:
677 skb->mark = nla_get_u32(nested_attr);
678 break;
679
Jesse Grossf0b128c2014-10-03 15:35:31 -0700680 case OVS_KEY_ATTR_TUNNEL_INFO:
681 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700682 break;
683
Jesse Grossccb13522011-10-25 19:26:31 -0700684 case OVS_KEY_ATTR_ETHERNET:
685 err = set_eth_addr(skb, nla_data(nested_attr));
686 break;
687
688 case OVS_KEY_ATTR_IPV4:
689 err = set_ipv4(skb, nla_data(nested_attr));
690 break;
691
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800692 case OVS_KEY_ATTR_IPV6:
693 err = set_ipv6(skb, nla_data(nested_attr));
694 break;
695
Jesse Grossccb13522011-10-25 19:26:31 -0700696 case OVS_KEY_ATTR_TCP:
Jesse Gross81e5d412012-03-06 15:05:46 -0800697 err = set_tcp(skb, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700698 break;
699
700 case OVS_KEY_ATTR_UDP:
Jesse Gross81e5d412012-03-06 15:05:46 -0800701 err = set_udp(skb, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700702 break;
Joe Stringera175a722013-08-22 12:30:48 -0700703
704 case OVS_KEY_ATTR_SCTP:
705 err = set_sctp(skb, nla_data(nested_attr));
706 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700707
708 case OVS_KEY_ATTR_MPLS:
709 err = set_mpls(skb, nla_data(nested_attr));
710 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700711 }
712
713 return err;
714}
715
Andy Zhou971427f32014-09-15 19:37:25 -0700716static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
717 struct sw_flow_key *key,
718 const struct nlattr *a, int rem)
719{
720 struct deferred_action *da;
721 int err;
722
723 err = ovs_flow_key_update(skb, key);
724 if (err)
725 return err;
726
Simon Horman941d8eb2014-10-27 16:12:16 +0900727 if (!nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700728 /* Recirc action is the not the last action
729 * of the action list, need to clone the skb.
730 */
731 skb = skb_clone(skb, GFP_ATOMIC);
732
733 /* Skip the recirc action when out of memory, but
734 * continue on with the rest of the action list.
735 */
736 if (!skb)
737 return 0;
738 }
739
740 da = add_deferred_actions(skb, key, NULL);
741 if (da) {
742 da->pkt_key.recirc_id = nla_get_u32(a);
743 } else {
744 kfree_skb(skb);
745
746 if (net_ratelimit())
747 pr_warn("%s: deferred action limit reached, drop recirc action\n",
748 ovs_dp_name(dp));
749 }
750
751 return 0;
752}
753
Jesse Grossccb13522011-10-25 19:26:31 -0700754/* Execute a list of actions against 'skb'. */
755static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700756 struct sw_flow_key *key,
Simon Horman651887b2014-07-21 15:12:34 -0700757 const struct nlattr *attr, int len)
Jesse Grossccb13522011-10-25 19:26:31 -0700758{
759 /* Every output action needs a separate clone of 'skb', but the common
760 * case is just a single output action, so that doing a clone and
761 * then freeing the original skbuff is wasteful. So the following code
762 * is slightly obscure just to avoid that. */
763 int prev_port = -1;
764 const struct nlattr *a;
765 int rem;
766
767 for (a = attr, rem = len; rem > 0;
768 a = nla_next(a, &rem)) {
769 int err = 0;
770
771 if (prev_port != -1) {
772 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
773 prev_port = -1;
774 }
775
776 switch (nla_type(a)) {
777 case OVS_ACTION_ATTR_OUTPUT:
778 prev_port = nla_get_u32(a);
779 break;
780
781 case OVS_ACTION_ATTR_USERSPACE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700782 output_userspace(dp, skb, key, a);
Jesse Grossccb13522011-10-25 19:26:31 -0700783 break;
784
Andy Zhou971427f32014-09-15 19:37:25 -0700785 case OVS_ACTION_ATTR_HASH:
786 execute_hash(skb, key, a);
787 break;
788
Simon Horman25cd9ba2014-10-06 05:05:13 -0700789 case OVS_ACTION_ATTR_PUSH_MPLS:
790 err = push_mpls(skb, nla_data(a));
791 break;
792
793 case OVS_ACTION_ATTR_POP_MPLS:
794 err = pop_mpls(skb, nla_get_be16(a));
795 break;
796
Jesse Grossccb13522011-10-25 19:26:31 -0700797 case OVS_ACTION_ATTR_PUSH_VLAN:
798 err = push_vlan(skb, nla_data(a));
799 if (unlikely(err)) /* skb already freed. */
800 return err;
801 break;
802
803 case OVS_ACTION_ATTR_POP_VLAN:
804 err = pop_vlan(skb);
805 break;
806
Andy Zhou971427f32014-09-15 19:37:25 -0700807 case OVS_ACTION_ATTR_RECIRC:
808 err = execute_recirc(dp, skb, key, a, rem);
Simon Horman941d8eb2014-10-27 16:12:16 +0900809 if (nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700810 /* If this is the last action, the skb has
811 * been consumed or freed.
812 * Return immediately.
813 */
814 return err;
815 }
816 break;
817
Jesse Grossccb13522011-10-25 19:26:31 -0700818 case OVS_ACTION_ATTR_SET:
819 err = execute_set_action(skb, nla_data(a));
820 break;
821
822 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700823 err = sample(dp, skb, key, a);
Andy Zhoufe984c02014-05-06 17:23:48 -0700824 if (unlikely(err)) /* skb already freed. */
825 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700826 break;
827 }
828
829 if (unlikely(err)) {
830 kfree_skb(skb);
831 return err;
832 }
833 }
834
Simon Horman651887b2014-07-21 15:12:34 -0700835 if (prev_port != -1)
Jesse Grossccb13522011-10-25 19:26:31 -0700836 do_output(dp, skb, prev_port);
Simon Horman651887b2014-07-21 15:12:34 -0700837 else
Jesse Grossccb13522011-10-25 19:26:31 -0700838 consume_skb(skb);
839
840 return 0;
841}
842
Andy Zhou971427f32014-09-15 19:37:25 -0700843static void process_deferred_actions(struct datapath *dp)
844{
845 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
846
847 /* Do not touch the FIFO in case there is no deferred actions. */
848 if (action_fifo_is_empty(fifo))
849 return;
850
851 /* Finishing executing all deferred actions. */
852 do {
853 struct deferred_action *da = action_fifo_get(fifo);
854 struct sk_buff *skb = da->skb;
855 struct sw_flow_key *key = &da->pkt_key;
856 const struct nlattr *actions = da->actions;
857
858 if (actions)
859 do_execute_actions(dp, skb, key, actions,
860 nla_len(actions));
861 else
862 ovs_dp_process_packet(skb, key);
863 } while (!action_fifo_is_empty(fifo));
864
865 /* Reset FIFO for the next packet. */
866 action_fifo_init(fifo);
867}
868
Jesse Grossccb13522011-10-25 19:26:31 -0700869/* Execute a list of actions against 'skb'. */
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700870int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
871 struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700872{
Andy Zhou971427f32014-09-15 19:37:25 -0700873 int level = this_cpu_read(exec_actions_level);
874 struct sw_flow_actions *acts;
875 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700876
Andy Zhou971427f32014-09-15 19:37:25 -0700877 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
878
879 this_cpu_inc(exec_actions_level);
Jesse Grossf0b128c2014-10-03 15:35:31 -0700880 OVS_CB(skb)->egress_tun_info = NULL;
Andy Zhou971427f32014-09-15 19:37:25 -0700881 err = do_execute_actions(dp, skb, key,
882 acts->actions, acts->actions_len);
883
884 if (!level)
885 process_deferred_actions(dp);
886
887 this_cpu_dec(exec_actions_level);
888 return err;
889}
890
891int action_fifos_init(void)
892{
893 action_fifos = alloc_percpu(struct action_fifo);
894 if (!action_fifos)
895 return -ENOMEM;
896
897 return 0;
898}
899
900void action_fifos_exit(void)
901{
902 free_percpu(action_fifos);
Jesse Grossccb13522011-10-25 19:26:31 -0700903}