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