blob: b4cffe686126e501505957f0fb2281f5c16d317b [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
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800188static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
189 const __be32 *mpls_lse)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700190{
191 __be32 *stack;
192 int err;
193
Jiri Pirkoe2195122014-11-19 14:05:01 +0100194 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700195 if (unlikely(err))
196 return err;
197
198 stack = (__be32 *)skb_mpls_header(skb);
199 if (skb->ip_summed == CHECKSUM_COMPLETE) {
200 __be32 diff[] = { ~(*stack), *mpls_lse };
Simon Horman25cd9ba2014-10-06 05:05:13 -0700201 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
202 ~skb->csum);
203 }
204
205 *stack = *mpls_lse;
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800206 key->mpls.top_lse = *mpls_lse;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700207 return 0;
208}
209
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800210static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700211{
Jesse Grossccb13522011-10-25 19:26:31 -0700212 int err;
213
Jiri Pirko93515d52014-11-19 14:05:02 +0100214 err = skb_vlan_pop(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100215 if (skb_vlan_tag_present(skb))
Jiri Pirko93515d52014-11-19 14:05:02 +0100216 invalidate_flow_key(key);
217 else
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800218 key->eth.tci = 0;
Jiri Pirko93515d52014-11-19 14:05:02 +0100219 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700220}
221
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800222static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
223 const struct ovs_action_push_vlan *vlan)
Jesse Grossccb13522011-10-25 19:26:31 -0700224{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100225 if (skb_vlan_tag_present(skb))
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800226 invalidate_flow_key(key);
Jiri Pirko93515d52014-11-19 14:05:02 +0100227 else
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800228 key->eth.tci = vlan->vlan_tci;
Jiri Pirko93515d52014-11-19 14:05:02 +0100229 return skb_vlan_push(skb, vlan->vlan_tpid,
230 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
Jesse Grossccb13522011-10-25 19:26:31 -0700231}
232
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800233static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700234 const struct ovs_key_ethernet *eth_key)
235{
236 int err;
Jiri Pirkoe2195122014-11-19 14:05:01 +0100237 err = skb_ensure_writable(skb, ETH_HLEN);
Jesse Grossccb13522011-10-25 19:26:31 -0700238 if (unlikely(err))
239 return err;
240
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700241 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
242
Joe Perches8c63ff02014-02-18 11:15:45 -0800243 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
244 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700245
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700246 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
247
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800248 ether_addr_copy(key->eth.src, eth_key->eth_src);
249 ether_addr_copy(key->eth.dst, eth_key->eth_dst);
Jesse Grossccb13522011-10-25 19:26:31 -0700250 return 0;
251}
252
253static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800254 __be32 *addr, __be32 new_addr)
Jesse Grossccb13522011-10-25 19:26:31 -0700255{
256 int transport_len = skb->len - skb_transport_offset(skb);
257
258 if (nh->protocol == IPPROTO_TCP) {
259 if (likely(transport_len >= sizeof(struct tcphdr)))
260 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
261 *addr, new_addr, 1);
262 } else if (nh->protocol == IPPROTO_UDP) {
Jesse Gross81e5d412012-03-06 15:05:46 -0800263 if (likely(transport_len >= sizeof(struct udphdr))) {
264 struct udphdr *uh = udp_hdr(skb);
265
266 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
267 inet_proto_csum_replace4(&uh->check, skb,
268 *addr, new_addr, 1);
269 if (!uh->check)
270 uh->check = CSUM_MANGLED_0;
271 }
272 }
Jesse Grossccb13522011-10-25 19:26:31 -0700273 }
274
275 csum_replace4(&nh->check, *addr, new_addr);
Tom Herbert7539fad2013-12-15 22:12:18 -0800276 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700277 *addr = new_addr;
278}
279
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800280static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
281 __be32 addr[4], const __be32 new_addr[4])
282{
283 int transport_len = skb->len - skb_transport_offset(skb);
284
Jesse Gross856447d2014-11-11 14:32:20 -0800285 if (l4_proto == NEXTHDR_TCP) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800286 if (likely(transport_len >= sizeof(struct tcphdr)))
287 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
288 addr, new_addr, 1);
Jesse Gross856447d2014-11-11 14:32:20 -0800289 } else if (l4_proto == NEXTHDR_UDP) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800290 if (likely(transport_len >= sizeof(struct udphdr))) {
291 struct udphdr *uh = udp_hdr(skb);
292
293 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
294 inet_proto_csum_replace16(&uh->check, skb,
295 addr, new_addr, 1);
296 if (!uh->check)
297 uh->check = CSUM_MANGLED_0;
298 }
299 }
Jesse Gross856447d2014-11-11 14:32:20 -0800300 } else if (l4_proto == NEXTHDR_ICMP) {
301 if (likely(transport_len >= sizeof(struct icmp6hdr)))
302 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
303 skb, addr, new_addr, 1);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800304 }
305}
306
307static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
308 __be32 addr[4], const __be32 new_addr[4],
309 bool recalculate_csum)
310{
311 if (recalculate_csum)
312 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
313
Tom Herbert7539fad2013-12-15 22:12:18 -0800314 skb_clear_hash(skb);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800315 memcpy(addr, new_addr, sizeof(__be32[4]));
316}
317
318static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
319{
320 nh->priority = tc >> 4;
321 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
322}
323
324static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
325{
326 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
327 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
328 nh->flow_lbl[2] = fl & 0x000000FF;
329}
330
Jesse Grossccb13522011-10-25 19:26:31 -0700331static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
332{
333 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
334 nh->ttl = new_ttl;
335}
336
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800337static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *key,
338 const struct ovs_key_ipv4 *ipv4_key)
Jesse Grossccb13522011-10-25 19:26:31 -0700339{
340 struct iphdr *nh;
341 int err;
342
Jiri Pirkoe2195122014-11-19 14:05:01 +0100343 err = skb_ensure_writable(skb, skb_network_offset(skb) +
344 sizeof(struct iphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700345 if (unlikely(err))
346 return err;
347
348 nh = ip_hdr(skb);
349
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800350 if (ipv4_key->ipv4_src != nh->saddr) {
Jesse Grossccb13522011-10-25 19:26:31 -0700351 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800352 key->ipv4.addr.src = ipv4_key->ipv4_src;
353 }
Jesse Grossccb13522011-10-25 19:26:31 -0700354
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800355 if (ipv4_key->ipv4_dst != nh->daddr) {
Jesse Grossccb13522011-10-25 19:26:31 -0700356 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800357 key->ipv4.addr.dst = ipv4_key->ipv4_dst;
358 }
Jesse Grossccb13522011-10-25 19:26:31 -0700359
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800360 if (ipv4_key->ipv4_tos != nh->tos) {
Jesse Grossccb13522011-10-25 19:26:31 -0700361 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800362 key->ip.tos = nh->tos;
363 }
Jesse Grossccb13522011-10-25 19:26:31 -0700364
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800365 if (ipv4_key->ipv4_ttl != nh->ttl) {
Jesse Grossccb13522011-10-25 19:26:31 -0700366 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800367 key->ip.ttl = ipv4_key->ipv4_ttl;
368 }
Jesse Grossccb13522011-10-25 19:26:31 -0700369
370 return 0;
371}
372
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800373static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
374 const struct ovs_key_ipv6 *ipv6_key)
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800375{
376 struct ipv6hdr *nh;
377 int err;
378 __be32 *saddr;
379 __be32 *daddr;
380
Jiri Pirkoe2195122014-11-19 14:05:01 +0100381 err = skb_ensure_writable(skb, skb_network_offset(skb) +
382 sizeof(struct ipv6hdr));
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800383 if (unlikely(err))
384 return err;
385
386 nh = ipv6_hdr(skb);
387 saddr = (__be32 *)&nh->saddr;
388 daddr = (__be32 *)&nh->daddr;
389
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800390 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800391 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
392 ipv6_key->ipv6_src, true);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800393 memcpy(&key->ipv6.addr.src, ipv6_key->ipv6_src,
394 sizeof(ipv6_key->ipv6_src));
395 }
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800396
397 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
398 unsigned int offset = 0;
399 int flags = IP6_FH_F_SKIP_RH;
400 bool recalc_csum = true;
401
402 if (ipv6_ext_hdr(nh->nexthdr))
403 recalc_csum = ipv6_find_hdr(skb, &offset,
404 NEXTHDR_ROUTING, NULL,
405 &flags) != NEXTHDR_ROUTING;
406
407 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
408 ipv6_key->ipv6_dst, recalc_csum);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800409 memcpy(&key->ipv6.addr.dst, ipv6_key->ipv6_dst,
410 sizeof(ipv6_key->ipv6_dst));
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800411 }
412
413 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800414 key->ip.tos = ipv6_get_dsfield(nh);
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800415
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800416 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
417 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
418
419 nh->hop_limit = ipv6_key->ipv6_hlimit;
420 key->ip.ttl = ipv6_key->ipv6_hlimit;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800421 return 0;
422}
423
Jiri Pirkoe2195122014-11-19 14:05:01 +0100424/* Must follow skb_ensure_writable() since that can move the skb data. */
Jesse Grossccb13522011-10-25 19:26:31 -0700425static void set_tp_port(struct sk_buff *skb, __be16 *port,
426 __be16 new_port, __sum16 *check)
427{
428 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
429 *port = new_port;
Tom Herbert7539fad2013-12-15 22:12:18 -0800430 skb_clear_hash(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700431}
432
Jesse Gross81e5d412012-03-06 15:05:46 -0800433static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
434{
435 struct udphdr *uh = udp_hdr(skb);
436
437 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
438 set_tp_port(skb, port, new_port, &uh->check);
439
440 if (!uh->check)
441 uh->check = CSUM_MANGLED_0;
442 } else {
443 *port = new_port;
Tom Herbert7539fad2013-12-15 22:12:18 -0800444 skb_clear_hash(skb);
Jesse Gross81e5d412012-03-06 15:05:46 -0800445 }
446}
447
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800448static int set_udp(struct sk_buff *skb, struct sw_flow_key *key,
449 const struct ovs_key_udp *udp_port_key)
Jesse Grossccb13522011-10-25 19:26:31 -0700450{
451 struct udphdr *uh;
452 int err;
453
Jiri Pirkoe2195122014-11-19 14:05:01 +0100454 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
455 sizeof(struct udphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700456 if (unlikely(err))
457 return err;
458
459 uh = udp_hdr(skb);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800460 if (udp_port_key->udp_src != uh->source) {
Jesse Gross81e5d412012-03-06 15:05:46 -0800461 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800462 key->tp.src = udp_port_key->udp_src;
463 }
Jesse Grossccb13522011-10-25 19:26:31 -0700464
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800465 if (udp_port_key->udp_dst != uh->dest) {
Jesse Gross81e5d412012-03-06 15:05:46 -0800466 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800467 key->tp.dst = udp_port_key->udp_dst;
468 }
Jesse Grossccb13522011-10-25 19:26:31 -0700469
470 return 0;
471}
472
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800473static int set_tcp(struct sk_buff *skb, struct sw_flow_key *key,
474 const struct ovs_key_tcp *tcp_port_key)
Jesse Grossccb13522011-10-25 19:26:31 -0700475{
476 struct tcphdr *th;
477 int err;
478
Jiri Pirkoe2195122014-11-19 14:05:01 +0100479 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
480 sizeof(struct tcphdr));
Jesse Grossccb13522011-10-25 19:26:31 -0700481 if (unlikely(err))
482 return err;
483
484 th = tcp_hdr(skb);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800485 if (tcp_port_key->tcp_src != th->source) {
Jesse Grossccb13522011-10-25 19:26:31 -0700486 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800487 key->tp.src = tcp_port_key->tcp_src;
488 }
Jesse Grossccb13522011-10-25 19:26:31 -0700489
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800490 if (tcp_port_key->tcp_dst != th->dest) {
Jesse Grossccb13522011-10-25 19:26:31 -0700491 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800492 key->tp.dst = tcp_port_key->tcp_dst;
493 }
Jesse Grossccb13522011-10-25 19:26:31 -0700494
495 return 0;
496}
497
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800498static int set_sctp(struct sk_buff *skb, struct sw_flow_key *key,
499 const struct ovs_key_sctp *sctp_port_key)
Joe Stringera175a722013-08-22 12:30:48 -0700500{
501 struct sctphdr *sh;
502 int err;
503 unsigned int sctphoff = skb_transport_offset(skb);
504
Jiri Pirkoe2195122014-11-19 14:05:01 +0100505 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
Joe Stringera175a722013-08-22 12:30:48 -0700506 if (unlikely(err))
507 return err;
508
509 sh = sctp_hdr(skb);
510 if (sctp_port_key->sctp_src != sh->source ||
511 sctp_port_key->sctp_dst != sh->dest) {
512 __le32 old_correct_csum, new_csum, old_csum;
513
514 old_csum = sh->checksum;
515 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
516
517 sh->source = sctp_port_key->sctp_src;
518 sh->dest = sctp_port_key->sctp_dst;
519
520 new_csum = sctp_compute_cksum(skb, sctphoff);
521
522 /* Carry any checksum errors through. */
523 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
524
Tom Herbert7539fad2013-12-15 22:12:18 -0800525 skb_clear_hash(skb);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800526 key->tp.src = sctp_port_key->sctp_src;
527 key->tp.dst = sctp_port_key->sctp_dst;
Joe Stringera175a722013-08-22 12:30:48 -0700528 }
529
530 return 0;
531}
532
Andy Zhou738967b2014-09-08 00:35:02 -0700533static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
Jesse Grossccb13522011-10-25 19:26:31 -0700534{
Andy Zhou738967b2014-09-08 00:35:02 -0700535 struct vport *vport = ovs_vport_rcu(dp, out_port);
Jesse Grossccb13522011-10-25 19:26:31 -0700536
Andy Zhou738967b2014-09-08 00:35:02 -0700537 if (likely(vport))
538 ovs_vport_send(vport, skb);
539 else
Jesse Grossccb13522011-10-25 19:26:31 -0700540 kfree_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700541}
542
543static int output_userspace(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700544 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700545{
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800546 struct ovs_tunnel_info info;
Jesse Grossccb13522011-10-25 19:26:31 -0700547 struct dp_upcall_info upcall;
548 const struct nlattr *a;
549 int rem;
550
551 upcall.cmd = OVS_PACKET_CMD_ACTION;
Jesse Grossccb13522011-10-25 19:26:31 -0700552 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000553 upcall.portid = 0;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800554 upcall.egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700555
556 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
557 a = nla_next(a, &rem)) {
558 switch (nla_type(a)) {
559 case OVS_USERSPACE_ATTR_USERDATA:
560 upcall.userdata = a;
561 break;
562
563 case OVS_USERSPACE_ATTR_PID:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000564 upcall.portid = nla_get_u32(a);
Jesse Grossccb13522011-10-25 19:26:31 -0700565 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800566
567 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
568 /* Get out tunnel info. */
569 struct vport *vport;
570
571 vport = ovs_vport_rcu(dp, nla_get_u32(a));
572 if (vport) {
573 int err;
574
575 err = ovs_vport_get_egress_tun_info(vport, skb,
576 &info);
577 if (!err)
578 upcall.egress_tun_info = &info;
579 }
580 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700581 }
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800582
583 } /* End of switch. */
Jesse Grossccb13522011-10-25 19:26:31 -0700584 }
585
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800586 return ovs_dp_upcall(dp, skb, key, &upcall);
Jesse Grossccb13522011-10-25 19:26:31 -0700587}
588
589static int sample(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700590 struct sw_flow_key *key, const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700591{
592 const struct nlattr *acts_list = NULL;
593 const struct nlattr *a;
594 int rem;
595
596 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
597 a = nla_next(a, &rem)) {
598 switch (nla_type(a)) {
599 case OVS_SAMPLE_ATTR_PROBABILITY:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500600 if (prandom_u32() >= nla_get_u32(a))
Jesse Grossccb13522011-10-25 19:26:31 -0700601 return 0;
602 break;
603
604 case OVS_SAMPLE_ATTR_ACTIONS:
605 acts_list = a;
606 break;
607 }
608 }
609
Simon Horman651887b2014-07-21 15:12:34 -0700610 rem = nla_len(acts_list);
611 a = nla_data(acts_list);
612
Andy Zhou32ae87f2014-09-15 19:33:50 -0700613 /* Actions list is empty, do nothing */
614 if (unlikely(!rem))
615 return 0;
Simon Horman651887b2014-07-21 15:12:34 -0700616
Andy Zhou32ae87f2014-09-15 19:33:50 -0700617 /* The only known usage of sample action is having a single user-space
618 * action. Treat this usage as a special case.
619 * The output_userspace() should clone the skb to be sent to the
620 * user space. This skb will be consumed by its caller.
Simon Horman651887b2014-07-21 15:12:34 -0700621 */
Andy Zhou32ae87f2014-09-15 19:33:50 -0700622 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
Simon Horman941d8eb2014-10-27 16:12:16 +0900623 nla_is_last(a, rem)))
Andy Zhou32ae87f2014-09-15 19:33:50 -0700624 return output_userspace(dp, skb, key, a);
625
626 skb = skb_clone(skb, GFP_ATOMIC);
627 if (!skb)
628 /* Skip the sample action when out of memory. */
629 return 0;
630
Andy Zhou971427f32014-09-15 19:37:25 -0700631 if (!add_deferred_actions(skb, key, a)) {
632 if (net_ratelimit())
633 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
634 ovs_dp_name(dp));
635
636 kfree_skb(skb);
637 }
638 return 0;
639}
640
641static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
642 const struct nlattr *attr)
643{
644 struct ovs_action_hash *hash_act = nla_data(attr);
645 u32 hash = 0;
646
647 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
648 hash = skb_get_hash(skb);
649 hash = jhash_1word(hash, hash_act->hash_basis);
650 if (!hash)
651 hash = 0x1;
652
653 key->ovs_flow_hash = hash;
Jesse Grossccb13522011-10-25 19:26:31 -0700654}
655
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800656static int execute_set_action(struct sk_buff *skb, struct sw_flow_key *key,
657 const struct nlattr *nested_attr)
Jesse Grossccb13522011-10-25 19:26:31 -0700658{
659 int err = 0;
660
661 switch (nla_type(nested_attr)) {
662 case OVS_KEY_ATTR_PRIORITY:
663 skb->priority = nla_get_u32(nested_attr);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800664 key->phy.priority = skb->priority;
Jesse Grossccb13522011-10-25 19:26:31 -0700665 break;
666
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800667 case OVS_KEY_ATTR_SKB_MARK:
668 skb->mark = nla_get_u32(nested_attr);
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800669 key->phy.skb_mark = skb->mark;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800670 break;
671
Jesse Grossf0b128c2014-10-03 15:35:31 -0700672 case OVS_KEY_ATTR_TUNNEL_INFO:
673 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700674 break;
675
Jesse Grossccb13522011-10-25 19:26:31 -0700676 case OVS_KEY_ATTR_ETHERNET:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800677 err = set_eth_addr(skb, key, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700678 break;
679
680 case OVS_KEY_ATTR_IPV4:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800681 err = set_ipv4(skb, key, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700682 break;
683
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800684 case OVS_KEY_ATTR_IPV6:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800685 err = set_ipv6(skb, key, nla_data(nested_attr));
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800686 break;
687
Jesse Grossccb13522011-10-25 19:26:31 -0700688 case OVS_KEY_ATTR_TCP:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800689 err = set_tcp(skb, key, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700690 break;
691
692 case OVS_KEY_ATTR_UDP:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800693 err = set_udp(skb, key, nla_data(nested_attr));
Jesse Grossccb13522011-10-25 19:26:31 -0700694 break;
Joe Stringera175a722013-08-22 12:30:48 -0700695
696 case OVS_KEY_ATTR_SCTP:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800697 err = set_sctp(skb, key, nla_data(nested_attr));
Joe Stringera175a722013-08-22 12:30:48 -0700698 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -0700699
700 case OVS_KEY_ATTR_MPLS:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800701 err = set_mpls(skb, key, nla_data(nested_attr));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700702 break;
Jesse Grossccb13522011-10-25 19:26:31 -0700703 }
704
705 return err;
706}
707
Andy Zhou971427f32014-09-15 19:37:25 -0700708static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
709 struct sw_flow_key *key,
710 const struct nlattr *a, int rem)
711{
712 struct deferred_action *da;
Andy Zhou971427f32014-09-15 19:37:25 -0700713
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800714 if (!is_flow_key_valid(key)) {
715 int err;
716
717 err = ovs_flow_key_update(skb, key);
718 if (err)
719 return err;
720 }
721 BUG_ON(!is_flow_key_valid(key));
Andy Zhou971427f32014-09-15 19:37:25 -0700722
Simon Horman941d8eb2014-10-27 16:12:16 +0900723 if (!nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700724 /* Recirc action is the not the last action
725 * of the action list, need to clone the skb.
726 */
727 skb = skb_clone(skb, GFP_ATOMIC);
728
729 /* Skip the recirc action when out of memory, but
730 * continue on with the rest of the action list.
731 */
732 if (!skb)
733 return 0;
734 }
735
736 da = add_deferred_actions(skb, key, NULL);
737 if (da) {
738 da->pkt_key.recirc_id = nla_get_u32(a);
739 } else {
740 kfree_skb(skb);
741
742 if (net_ratelimit())
743 pr_warn("%s: deferred action limit reached, drop recirc action\n",
744 ovs_dp_name(dp));
745 }
746
747 return 0;
748}
749
Jesse Grossccb13522011-10-25 19:26:31 -0700750/* Execute a list of actions against 'skb'. */
751static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700752 struct sw_flow_key *key,
Simon Horman651887b2014-07-21 15:12:34 -0700753 const struct nlattr *attr, int len)
Jesse Grossccb13522011-10-25 19:26:31 -0700754{
755 /* Every output action needs a separate clone of 'skb', but the common
756 * case is just a single output action, so that doing a clone and
757 * then freeing the original skbuff is wasteful. So the following code
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800758 * is slightly obscure just to avoid that.
759 */
Jesse Grossccb13522011-10-25 19:26:31 -0700760 int prev_port = -1;
761 const struct nlattr *a;
762 int rem;
763
764 for (a = attr, rem = len; rem > 0;
765 a = nla_next(a, &rem)) {
766 int err = 0;
767
Andy Zhou738967b2014-09-08 00:35:02 -0700768 if (unlikely(prev_port != -1)) {
769 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
770
771 if (out_skb)
772 do_output(dp, out_skb, prev_port);
773
Jesse Grossccb13522011-10-25 19:26:31 -0700774 prev_port = -1;
775 }
776
777 switch (nla_type(a)) {
778 case OVS_ACTION_ATTR_OUTPUT:
779 prev_port = nla_get_u32(a);
780 break;
781
782 case OVS_ACTION_ATTR_USERSPACE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700783 output_userspace(dp, skb, key, a);
Jesse Grossccb13522011-10-25 19:26:31 -0700784 break;
785
Andy Zhou971427f32014-09-15 19:37:25 -0700786 case OVS_ACTION_ATTR_HASH:
787 execute_hash(skb, key, a);
788 break;
789
Simon Horman25cd9ba2014-10-06 05:05:13 -0700790 case OVS_ACTION_ATTR_PUSH_MPLS:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800791 err = push_mpls(skb, key, nla_data(a));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700792 break;
793
794 case OVS_ACTION_ATTR_POP_MPLS:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800795 err = pop_mpls(skb, key, nla_get_be16(a));
Simon Horman25cd9ba2014-10-06 05:05:13 -0700796 break;
797
Jesse Grossccb13522011-10-25 19:26:31 -0700798 case OVS_ACTION_ATTR_PUSH_VLAN:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800799 err = push_vlan(skb, key, nla_data(a));
Jesse Grossccb13522011-10-25 19:26:31 -0700800 break;
801
802 case OVS_ACTION_ATTR_POP_VLAN:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800803 err = pop_vlan(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700804 break;
805
Andy Zhou971427f32014-09-15 19:37:25 -0700806 case OVS_ACTION_ATTR_RECIRC:
807 err = execute_recirc(dp, skb, key, a, rem);
Simon Horman941d8eb2014-10-27 16:12:16 +0900808 if (nla_is_last(a, rem)) {
Andy Zhou971427f32014-09-15 19:37:25 -0700809 /* If this is the last action, the skb has
810 * been consumed or freed.
811 * Return immediately.
812 */
813 return err;
814 }
815 break;
816
Jesse Grossccb13522011-10-25 19:26:31 -0700817 case OVS_ACTION_ATTR_SET:
Pravin B Shelarfff06c32014-11-06 06:55:14 -0800818 err = execute_set_action(skb, key, nla_data(a));
Jesse Grossccb13522011-10-25 19:26:31 -0700819 break;
820
821 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700822 err = sample(dp, skb, key, a);
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,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800868 const struct sw_flow_actions *acts,
869 struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700870{
Andy Zhou971427f32014-09-15 19:37:25 -0700871 int level = this_cpu_read(exec_actions_level);
Andy Zhou971427f32014-09-15 19:37:25 -0700872 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700873
Andy Zhou971427f32014-09-15 19:37:25 -0700874 this_cpu_inc(exec_actions_level);
Jesse Grossf0b128c2014-10-03 15:35:31 -0700875 OVS_CB(skb)->egress_tun_info = NULL;
Andy Zhou971427f32014-09-15 19:37:25 -0700876 err = do_execute_actions(dp, skb, key,
877 acts->actions, acts->actions_len);
878
879 if (!level)
880 process_deferred_actions(dp);
881
882 this_cpu_dec(exec_actions_level);
883 return err;
884}
885
886int action_fifos_init(void)
887{
888 action_fifos = alloc_percpu(struct action_fifo);
889 if (!action_fifos)
890 return -ENOMEM;
891
892 return 0;
893}
894
895void action_fifos_exit(void)
896{
897 free_percpu(action_fifos);
Jesse Grossccb13522011-10-25 19:26:31 -0700898}