]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2007-2012 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> |
064af421 BP |
25 | #include <linux/tcp.h> |
26 | #include <linux/udp.h> | |
27 | #include <linux/in6.h> | |
401eeb92 | 28 | #include <linux/if_arp.h> |
064af421 BP |
29 | #include <linux/if_vlan.h> |
30 | #include <net/ip.h> | |
bc7a5acd | 31 | #include <net/ipv6.h> |
064af421 | 32 | #include <net/checksum.h> |
530180fd | 33 | #include <net/dsfield.h> |
f2459fe7 | 34 | |
dd8d6b8c | 35 | #include "checksum.h" |
f2459fe7 | 36 | #include "datapath.h" |
6ce39213 | 37 | #include "vlan.h" |
f2459fe7 | 38 | #include "vport.h" |
064af421 | 39 | |
6ff686f2 | 40 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
85c9de19 | 41 | const struct nlattr *attr, int len, bool keep_skb); |
871dfe07 | 42 | |
10db8b20 | 43 | static int make_writable(struct sk_buff *skb, int write_len) |
064af421 | 44 | { |
10db8b20 JG |
45 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
46 | return 0; | |
0cd8a05e | 47 | |
10db8b20 | 48 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
064af421 BP |
49 | } |
50 | ||
b2492cb7 | 51 | /* remove VLAN header from packet and update csum accordingly. */ |
d9065a90 | 52 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) |
064af421 | 53 | { |
ceb176fd | 54 | struct vlan_hdr *vhdr; |
10db8b20 | 55 | int err; |
064af421 | 56 | |
10db8b20 JG |
57 | err = make_writable(skb, VLAN_ETH_HLEN); |
58 | if (unlikely(err)) | |
59 | return err; | |
6ce39213 | 60 | |
dd8d6b8c | 61 | if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) |
635c9298 | 62 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
af9d14a8 | 63 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
635c9298 | 64 | |
ceb176fd PS |
65 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
66 | *current_tci = vhdr->h_vlan_TCI; | |
d9065a90 | 67 | |
6ce39213 | 68 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
ceb176fd | 69 | __skb_pull(skb, VLAN_HLEN); |
064af421 | 70 | |
ceb176fd | 71 | vlan_set_encap_proto(skb, vhdr); |
064af421 | 72 | skb->mac_header += VLAN_HLEN; |
ceb176fd | 73 | skb_reset_mac_len(skb); |
064af421 | 74 | |
10db8b20 | 75 | return 0; |
064af421 BP |
76 | } |
77 | ||
d9065a90 | 78 | static int pop_vlan(struct sk_buff *skb) |
064af421 | 79 | { |
d9065a90 PS |
80 | __be16 tci; |
81 | int err; | |
10db8b20 | 82 | |
d9065a90 PS |
83 | if (likely(vlan_tx_tag_present(skb))) { |
84 | vlan_set_tci(skb, 0); | |
85 | } else { | |
86 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 87 | skb->len < VLAN_ETH_HLEN)) |
10db8b20 | 88 | return 0; |
064af421 | 89 | |
d9065a90 PS |
90 | err = __pop_vlan_tci(skb, &tci); |
91 | if (err) | |
10db8b20 | 92 | return err; |
064af421 | 93 | } |
d9065a90 PS |
94 | /* move next vlan tag to hw accel tag */ |
95 | if (likely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 96 | skb->len < VLAN_ETH_HLEN)) |
d9065a90 PS |
97 | return 0; |
98 | ||
99 | err = __pop_vlan_tci(skb, &tci); | |
100 | if (unlikely(err)) | |
101 | return err; | |
064af421 | 102 | |
10db8b20 | 103 | __vlan_hwaccel_put_tag(skb, ntohs(tci)); |
d9065a90 PS |
104 | return 0; |
105 | } | |
106 | ||
fea393b1 | 107 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
d9065a90 PS |
108 | { |
109 | if (unlikely(vlan_tx_tag_present(skb))) { | |
110 | u16 current_tag; | |
111 | ||
112 | /* push down current VLAN tag */ | |
113 | current_tag = vlan_tx_tag_get(skb); | |
114 | ||
115 | if (!__vlan_put_tag(skb, current_tag)) | |
116 | return -ENOMEM; | |
10db8b20 | 117 | |
d9065a90 PS |
118 | if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) |
119 | skb->csum = csum_add(skb->csum, csum_partial(skb->data | |
af9d14a8 | 120 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
d9065a90 PS |
121 | |
122 | } | |
8ddc056d | 123 | __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
10db8b20 | 124 | return 0; |
064af421 BP |
125 | } |
126 | ||
4edb9ae9 PS |
127 | static int set_eth_addr(struct sk_buff *skb, |
128 | const struct ovs_key_ethernet *eth_key) | |
ca78c6b6 | 129 | { |
4edb9ae9 PS |
130 | int err; |
131 | err = make_writable(skb, ETH_HLEN); | |
132 | if (unlikely(err)) | |
133 | return err; | |
134 | ||
39640c1b PS |
135 | memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN); |
136 | memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN); | |
4edb9ae9 PS |
137 | |
138 | return 0; | |
ca78c6b6 BP |
139 | } |
140 | ||
4edb9ae9 PS |
141 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
142 | __be32 *addr, __be32 new_addr) | |
ca78c6b6 BP |
143 | { |
144 | int transport_len = skb->len - skb_transport_offset(skb); | |
4edb9ae9 PS |
145 | |
146 | if (nh->protocol == IPPROTO_TCP) { | |
ca78c6b6 | 147 | if (likely(transport_len >= sizeof(struct tcphdr))) |
4edb9ae9 PS |
148 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
149 | *addr, new_addr, 1); | |
150 | } else if (nh->protocol == IPPROTO_UDP) { | |
55ce87bc JG |
151 | if (likely(transport_len >= sizeof(struct udphdr))) { |
152 | struct udphdr *uh = udp_hdr(skb); | |
153 | ||
154 | if (uh->check || | |
155 | get_ip_summed(skb) == OVS_CSUM_PARTIAL) { | |
156 | inet_proto_csum_replace4(&uh->check, skb, | |
157 | *addr, new_addr, 1); | |
158 | if (!uh->check) | |
159 | uh->check = CSUM_MANGLED_0; | |
160 | } | |
161 | } | |
ca78c6b6 | 162 | } |
4edb9ae9 PS |
163 | |
164 | csum_replace4(&nh->check, *addr, new_addr); | |
165 | skb_clear_rxhash(skb); | |
166 | *addr = new_addr; | |
ca78c6b6 BP |
167 | } |
168 | ||
bc7a5acd AA |
169 | static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, |
170 | __be32 addr[4], const __be32 new_addr[4]) | |
171 | { | |
172 | int transport_len = skb->len - skb_transport_offset(skb); | |
173 | ||
174 | if (l4_proto == IPPROTO_TCP) { | |
175 | if (likely(transport_len >= sizeof(struct tcphdr))) | |
176 | inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, | |
177 | addr, new_addr, 1); | |
178 | } else if (l4_proto == IPPROTO_UDP) { | |
179 | if (likely(transport_len >= sizeof(struct udphdr))) { | |
180 | struct udphdr *uh = udp_hdr(skb); | |
181 | ||
182 | if (uh->check || | |
183 | get_ip_summed(skb) == OVS_CSUM_PARTIAL) { | |
184 | inet_proto_csum_replace16(&uh->check, skb, | |
185 | addr, new_addr, 1); | |
186 | if (!uh->check) | |
187 | uh->check = CSUM_MANGLED_0; | |
188 | } | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, | |
194 | __be32 addr[4], const __be32 new_addr[4], | |
195 | bool recalculate_csum) | |
196 | { | |
197 | if (recalculate_csum) | |
198 | update_ipv6_checksum(skb, l4_proto, addr, new_addr); | |
199 | ||
200 | skb_clear_rxhash(skb); | |
201 | memcpy(addr, new_addr, sizeof(__be32[4])); | |
202 | } | |
203 | ||
204 | static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc) | |
205 | { | |
206 | nh->priority = tc >> 4; | |
207 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4); | |
208 | } | |
209 | ||
210 | static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl) | |
211 | { | |
212 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16; | |
213 | nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8; | |
214 | nh->flow_lbl[2] = fl & 0x000000FF; | |
215 | } | |
216 | ||
a61680c6 JP |
217 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
218 | { | |
219 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); | |
220 | nh->ttl = new_ttl; | |
221 | } | |
222 | ||
4edb9ae9 | 223 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
064af421 | 224 | { |
ca78c6b6 | 225 | struct iphdr *nh; |
10db8b20 | 226 | int err; |
ca78c6b6 | 227 | |
10db8b20 JG |
228 | err = make_writable(skb, skb_network_offset(skb) + |
229 | sizeof(struct iphdr)); | |
230 | if (unlikely(err)) | |
231 | return err; | |
ca78c6b6 BP |
232 | |
233 | nh = ip_hdr(skb); | |
ca78c6b6 | 234 | |
4edb9ae9 PS |
235 | if (ipv4_key->ipv4_src != nh->saddr) |
236 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); | |
ca78c6b6 | 237 | |
4edb9ae9 PS |
238 | if (ipv4_key->ipv4_dst != nh->daddr) |
239 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); | |
a4a26436 | 240 | |
4edb9ae9 | 241 | if (ipv4_key->ipv4_tos != nh->tos) |
530180fd | 242 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
ca78c6b6 | 243 | |
a61680c6 JP |
244 | if (ipv4_key->ipv4_ttl != nh->ttl) |
245 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); | |
246 | ||
10db8b20 | 247 | return 0; |
064af421 BP |
248 | } |
249 | ||
bc7a5acd AA |
250 | static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key) |
251 | { | |
252 | struct ipv6hdr *nh; | |
253 | int err; | |
254 | __be32 *saddr; | |
255 | __be32 *daddr; | |
256 | ||
257 | err = make_writable(skb, skb_network_offset(skb) + | |
258 | sizeof(struct ipv6hdr)); | |
259 | if (unlikely(err)) | |
260 | return err; | |
261 | ||
262 | nh = ipv6_hdr(skb); | |
263 | saddr = (__be32 *)&nh->saddr; | |
264 | daddr = (__be32 *)&nh->daddr; | |
265 | ||
266 | if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) | |
267 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr, | |
268 | ipv6_key->ipv6_src, true); | |
269 | ||
270 | if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) { | |
271 | unsigned int offset = 0; | |
272 | int flags = OVS_IP6T_FH_F_SKIP_RH; | |
273 | bool recalc_csum = true; | |
274 | ||
275 | if (ipv6_ext_hdr(nh->nexthdr)) | |
276 | recalc_csum = ipv6_find_hdr(skb, &offset, | |
277 | NEXTHDR_ROUTING, NULL, | |
278 | &flags) != NEXTHDR_ROUTING; | |
279 | ||
280 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr, | |
281 | ipv6_key->ipv6_dst, recalc_csum); | |
282 | } | |
283 | ||
284 | set_ipv6_tc(nh, ipv6_key->ipv6_tclass); | |
285 | set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label)); | |
286 | nh->hop_limit = ipv6_key->ipv6_hlimit; | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
4edb9ae9 PS |
291 | /* Must follow make_writable() since that can move the skb data. */ |
292 | static void set_tp_port(struct sk_buff *skb, __be16 *port, | |
293 | __be16 new_port, __sum16 *check) | |
959a2ecd | 294 | { |
4edb9ae9 PS |
295 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
296 | *port = new_port; | |
297 | skb_clear_rxhash(skb); | |
298 | } | |
10db8b20 | 299 | |
55ce87bc JG |
300 | static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port) |
301 | { | |
302 | struct udphdr *uh = udp_hdr(skb); | |
303 | ||
304 | if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) { | |
305 | set_tp_port(skb, port, new_port, &uh->check); | |
306 | ||
307 | if (!uh->check) | |
308 | uh->check = CSUM_MANGLED_0; | |
309 | } else { | |
310 | *port = new_port; | |
311 | skb_clear_rxhash(skb); | |
312 | } | |
313 | } | |
314 | ||
315 | static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key) | |
4edb9ae9 PS |
316 | { |
317 | struct udphdr *uh; | |
318 | int err; | |
10db8b20 | 319 | |
4edb9ae9 PS |
320 | err = make_writable(skb, skb_transport_offset(skb) + |
321 | sizeof(struct udphdr)); | |
10db8b20 JG |
322 | if (unlikely(err)) |
323 | return err; | |
324 | ||
4edb9ae9 PS |
325 | uh = udp_hdr(skb); |
326 | if (udp_port_key->udp_src != uh->source) | |
55ce87bc | 327 | set_udp_port(skb, &uh->source, udp_port_key->udp_src); |
4edb9ae9 PS |
328 | |
329 | if (udp_port_key->udp_dst != uh->dest) | |
55ce87bc | 330 | set_udp_port(skb, &uh->dest, udp_port_key->udp_dst); |
10db8b20 JG |
331 | |
332 | return 0; | |
959a2ecd JP |
333 | } |
334 | ||
55ce87bc | 335 | static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key) |
064af421 | 336 | { |
4edb9ae9 | 337 | struct tcphdr *th; |
10db8b20 | 338 | int err; |
064af421 | 339 | |
10db8b20 JG |
340 | err = make_writable(skb, skb_transport_offset(skb) + |
341 | sizeof(struct tcphdr)); | |
342 | if (unlikely(err)) | |
343 | return err; | |
ca78c6b6 | 344 | |
4edb9ae9 PS |
345 | th = tcp_hdr(skb); |
346 | if (tcp_port_key->tcp_src != th->source) | |
347 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); | |
064af421 | 348 | |
4edb9ae9 PS |
349 | if (tcp_port_key->tcp_dst != th->dest) |
350 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); | |
ca78c6b6 | 351 | |
10db8b20 | 352 | return 0; |
064af421 BP |
353 | } |
354 | ||
f15c8639 | 355 | static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
064af421 | 356 | { |
f15c8639 | 357 | struct vport *vport; |
064af421 | 358 | |
f15c8639 JG |
359 | if (unlikely(!skb)) |
360 | return -ENOMEM; | |
064af421 | 361 | |
95b1d73a | 362 | vport = ovs_vport_rcu(dp, out_port); |
f15c8639 JG |
363 | if (unlikely(!vport)) { |
364 | kfree_skb(skb); | |
365 | return -ENODEV; | |
366 | } | |
064af421 | 367 | |
850b6b3b | 368 | ovs_vport_send(vport, skb); |
f15c8639 | 369 | return 0; |
064af421 BP |
370 | } |
371 | ||
98403001 BP |
372 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
373 | const struct nlattr *attr) | |
064af421 | 374 | { |
856081f6 | 375 | struct dp_upcall_info upcall; |
98403001 BP |
376 | const struct nlattr *a; |
377 | int rem; | |
856081f6 | 378 | |
df2c07f4 | 379 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
a4af2475 | 380 | upcall.key = &OVS_CB(skb)->flow->key; |
98403001 | 381 | upcall.userdata = NULL; |
28aea917 | 382 | upcall.portid = 0; |
98403001 BP |
383 | |
384 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
385 | a = nla_next(a, &rem)) { | |
386 | switch (nla_type(a)) { | |
387 | case OVS_USERSPACE_ATTR_USERDATA: | |
388 | upcall.userdata = a; | |
389 | break; | |
390 | ||
391 | case OVS_USERSPACE_ATTR_PID: | |
28aea917 | 392 | upcall.portid = nla_get_u32(a); |
98403001 BP |
393 | break; |
394 | } | |
395 | } | |
396 | ||
850b6b3b | 397 | return ovs_dp_upcall(dp, skb, &upcall); |
064af421 BP |
398 | } |
399 | ||
6ff686f2 | 400 | static int sample(struct datapath *dp, struct sk_buff *skb, |
85c9de19 | 401 | const struct nlattr *attr) |
6ff686f2 PS |
402 | { |
403 | const struct nlattr *acts_list = NULL; | |
404 | const struct nlattr *a; | |
405 | int rem; | |
406 | ||
407 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
408 | a = nla_next(a, &rem)) { | |
409 | switch (nla_type(a)) { | |
410 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
411 | if (net_random() >= nla_get_u32(a)) | |
412 | return 0; | |
413 | break; | |
414 | ||
415 | case OVS_SAMPLE_ATTR_ACTIONS: | |
416 | acts_list = a; | |
417 | break; | |
418 | } | |
419 | } | |
420 | ||
421 | return do_execute_actions(dp, skb, nla_data(acts_list), | |
85c9de19 | 422 | nla_len(acts_list), true); |
6ff686f2 PS |
423 | } |
424 | ||
4edb9ae9 | 425 | static int execute_set_action(struct sk_buff *skb, |
85c9de19 | 426 | const struct nlattr *nested_attr) |
4edb9ae9 | 427 | { |
15c39847 | 428 | int err = 0; |
4edb9ae9 PS |
429 | |
430 | switch (nla_type(nested_attr)) { | |
abff858b PS |
431 | case OVS_KEY_ATTR_PRIORITY: |
432 | skb->priority = nla_get_u32(nested_attr); | |
433 | break; | |
434 | ||
72e8bf28 AA |
435 | case OVS_KEY_ATTR_SKB_MARK: |
436 | skb_set_mark(skb, nla_get_u32(nested_attr)); | |
437 | break; | |
438 | ||
356af50b KM |
439 | case OVS_KEY_ATTR_IPV4_TUNNEL: |
440 | OVS_CB(skb)->tun_key = nla_data(nested_attr); | |
4edb9ae9 PS |
441 | break; |
442 | ||
443 | case OVS_KEY_ATTR_ETHERNET: | |
444 | err = set_eth_addr(skb, nla_data(nested_attr)); | |
445 | break; | |
446 | ||
447 | case OVS_KEY_ATTR_IPV4: | |
448 | err = set_ipv4(skb, nla_data(nested_attr)); | |
449 | break; | |
450 | ||
bc7a5acd AA |
451 | case OVS_KEY_ATTR_IPV6: |
452 | err = set_ipv6(skb, nla_data(nested_attr)); | |
453 | break; | |
454 | ||
4edb9ae9 | 455 | case OVS_KEY_ATTR_TCP: |
55ce87bc | 456 | err = set_tcp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
457 | break; |
458 | ||
459 | case OVS_KEY_ATTR_UDP: | |
55ce87bc | 460 | err = set_udp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
461 | break; |
462 | } | |
15c39847 | 463 | |
4edb9ae9 PS |
464 | return err; |
465 | } | |
466 | ||
064af421 | 467 | /* Execute a list of actions against 'skb'. */ |
871dfe07 | 468 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
85c9de19 | 469 | const struct nlattr *attr, int len, bool keep_skb) |
064af421 BP |
470 | { |
471 | /* Every output action needs a separate clone of 'skb', but the common | |
472 | * case is just a single output action, so that doing a clone and | |
473 | * then freeing the original skbuff is wasteful. So the following code | |
474 | * is slightly obscure just to avoid that. */ | |
475 | int prev_port = -1; | |
cdee00fd | 476 | const struct nlattr *a; |
10db8b20 | 477 | int rem; |
72b06300 | 478 | |
6ff686f2 | 479 | for (a = attr, rem = len; rem > 0; |
a4af2475 | 480 | a = nla_next(a, &rem)) { |
10db8b20 JG |
481 | int err = 0; |
482 | ||
064af421 | 483 | if (prev_port != -1) { |
7956695a | 484 | do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port); |
064af421 BP |
485 | prev_port = -1; |
486 | } | |
487 | ||
cdee00fd | 488 | switch (nla_type(a)) { |
df2c07f4 | 489 | case OVS_ACTION_ATTR_OUTPUT: |
cdee00fd | 490 | prev_port = nla_get_u32(a); |
064af421 BP |
491 | break; |
492 | ||
df2c07f4 | 493 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 494 | output_userspace(dp, skb, a); |
064af421 BP |
495 | break; |
496 | ||
fea393b1 BP |
497 | case OVS_ACTION_ATTR_PUSH_VLAN: |
498 | err = push_vlan(skb, nla_data(a)); | |
4edb9ae9 | 499 | if (unlikely(err)) /* skb already freed. */ |
d9065a90 | 500 | return err; |
064af421 BP |
501 | break; |
502 | ||
fea393b1 | 503 | case OVS_ACTION_ATTR_POP_VLAN: |
d9065a90 | 504 | err = pop_vlan(skb); |
064af421 BP |
505 | break; |
506 | ||
4edb9ae9 | 507 | case OVS_ACTION_ATTR_SET: |
85c9de19 | 508 | err = execute_set_action(skb, nla_data(a)); |
064af421 | 509 | break; |
c1c9c9c4 | 510 | |
6ff686f2 | 511 | case OVS_ACTION_ATTR_SAMPLE: |
85c9de19 | 512 | err = sample(dp, skb, a); |
6ff686f2 | 513 | break; |
6ff686f2 | 514 | } |
15c39847 | 515 | |
10db8b20 JG |
516 | if (unlikely(err)) { |
517 | kfree_skb(skb); | |
518 | return err; | |
519 | } | |
064af421 | 520 | } |
6c222e55 | 521 | |
6ff686f2 PS |
522 | if (prev_port != -1) { |
523 | if (keep_skb) | |
524 | skb = skb_clone(skb, GFP_ATOMIC); | |
525 | ||
064af421 | 526 | do_output(dp, skb, prev_port); |
6ff686f2 | 527 | } else if (!keep_skb) |
5b95ab0e | 528 | consume_skb(skb); |
10db8b20 | 529 | |
a5225dd6 | 530 | return 0; |
064af421 | 531 | } |
871dfe07 | 532 | |
e9141eec PS |
533 | /* We limit the number of times that we pass into execute_actions() |
534 | * to avoid blowing out the stack in the event that we have a loop. */ | |
535 | #define MAX_LOOPS 5 | |
536 | ||
537 | struct loop_counter { | |
538 | u8 count; /* Count. */ | |
539 | bool looping; /* Loop detected? */ | |
540 | }; | |
541 | ||
542 | static DEFINE_PER_CPU(struct loop_counter, loop_counters); | |
543 | ||
544 | static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions) | |
545 | { | |
546 | if (net_ratelimit()) | |
547 | pr_warn("%s: flow looped %d times, dropping\n", | |
850b6b3b | 548 | ovs_dp_name(dp), MAX_LOOPS); |
e9141eec PS |
549 | actions->actions_len = 0; |
550 | return -ELOOP; | |
551 | } | |
552 | ||
871dfe07 | 553 | /* Execute a list of actions against 'skb'. */ |
850b6b3b | 554 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb) |
871dfe07 | 555 | { |
a4af2475 BP |
556 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
557 | struct loop_counter *loop; | |
558 | int error; | |
559 | ||
560 | /* Check whether we've looped too much. */ | |
e9141eec | 561 | loop = &__get_cpu_var(loop_counters); |
a4af2475 BP |
562 | if (unlikely(++loop->count > MAX_LOOPS)) |
563 | loop->looping = true; | |
564 | if (unlikely(loop->looping)) { | |
565 | error = loop_suppress(dp, acts); | |
566 | kfree_skb(skb); | |
567 | goto out_loop; | |
568 | } | |
871dfe07 | 569 | |
356af50b | 570 | OVS_CB(skb)->tun_key = NULL; |
6ff686f2 | 571 | error = do_execute_actions(dp, skb, acts->actions, |
85c9de19 | 572 | acts->actions_len, false); |
a4af2475 BP |
573 | |
574 | /* Check whether sub-actions looped too much. */ | |
575 | if (unlikely(loop->looping)) | |
576 | error = loop_suppress(dp, acts); | |
577 | ||
578 | out_loop: | |
579 | /* Decrement loop counter. */ | |
580 | if (!--loop->count) | |
581 | loop->looping = false; | |
871dfe07 | 582 | |
a4af2475 | 583 | return error; |
871dfe07 | 584 | } |