]>
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> |
10f72e3d | 35 | #include <net/sctp/checksum.h> |
f2459fe7 | 36 | |
f2459fe7 | 37 | #include "datapath.h" |
6ce39213 | 38 | #include "vlan.h" |
f2459fe7 | 39 | #include "vport.h" |
064af421 | 40 | |
6ff686f2 | 41 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
fbf4f74d | 42 | const struct nlattr *attr, int len); |
871dfe07 | 43 | |
10db8b20 | 44 | static int make_writable(struct sk_buff *skb, int write_len) |
064af421 | 45 | { |
10db8b20 JG |
46 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
47 | return 0; | |
0cd8a05e | 48 | |
10db8b20 | 49 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
064af421 BP |
50 | } |
51 | ||
b2492cb7 | 52 | /* remove VLAN header from packet and update csum accordingly. */ |
d9065a90 | 53 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) |
064af421 | 54 | { |
ceb176fd | 55 | struct vlan_hdr *vhdr; |
10db8b20 | 56 | int err; |
064af421 | 57 | |
10db8b20 JG |
58 | err = make_writable(skb, VLAN_ETH_HLEN); |
59 | if (unlikely(err)) | |
60 | return err; | |
6ce39213 | 61 | |
237c4f2a | 62 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
635c9298 | 63 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
af9d14a8 | 64 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
635c9298 | 65 | |
ceb176fd PS |
66 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
67 | *current_tci = vhdr->h_vlan_TCI; | |
d9065a90 | 68 | |
6ce39213 | 69 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
ceb176fd | 70 | __skb_pull(skb, VLAN_HLEN); |
064af421 | 71 | |
ceb176fd | 72 | vlan_set_encap_proto(skb, vhdr); |
064af421 | 73 | skb->mac_header += VLAN_HLEN; |
ceb176fd | 74 | skb_reset_mac_len(skb); |
064af421 | 75 | |
10db8b20 | 76 | return 0; |
064af421 BP |
77 | } |
78 | ||
d9065a90 | 79 | static int pop_vlan(struct sk_buff *skb) |
064af421 | 80 | { |
d9065a90 PS |
81 | __be16 tci; |
82 | int err; | |
10db8b20 | 83 | |
d9065a90 PS |
84 | if (likely(vlan_tx_tag_present(skb))) { |
85 | vlan_set_tci(skb, 0); | |
86 | } else { | |
87 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 88 | skb->len < VLAN_ETH_HLEN)) |
10db8b20 | 89 | return 0; |
064af421 | 90 | |
d9065a90 PS |
91 | err = __pop_vlan_tci(skb, &tci); |
92 | if (err) | |
10db8b20 | 93 | return err; |
064af421 | 94 | } |
d9065a90 PS |
95 | /* move next vlan tag to hw accel tag */ |
96 | if (likely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 97 | skb->len < VLAN_ETH_HLEN)) |
d9065a90 PS |
98 | return 0; |
99 | ||
100 | err = __pop_vlan_tci(skb, &tci); | |
101 | if (unlikely(err)) | |
102 | return err; | |
064af421 | 103 | |
9b764edf | 104 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci)); |
d9065a90 PS |
105 | return 0; |
106 | } | |
107 | ||
fea393b1 | 108 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
d9065a90 PS |
109 | { |
110 | if (unlikely(vlan_tx_tag_present(skb))) { | |
111 | u16 current_tag; | |
112 | ||
113 | /* push down current VLAN tag */ | |
114 | current_tag = vlan_tx_tag_get(skb); | |
115 | ||
9b764edf | 116 | if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag)) |
d9065a90 | 117 | return -ENOMEM; |
10db8b20 | 118 | |
237c4f2a | 119 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
d9065a90 | 120 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
af9d14a8 | 121 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
d9065a90 PS |
122 | |
123 | } | |
9b764edf | 124 | __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
10db8b20 | 125 | return 0; |
064af421 BP |
126 | } |
127 | ||
4edb9ae9 PS |
128 | static int set_eth_addr(struct sk_buff *skb, |
129 | const struct ovs_key_ethernet *eth_key) | |
ca78c6b6 | 130 | { |
4edb9ae9 PS |
131 | int err; |
132 | err = make_writable(skb, ETH_HLEN); | |
133 | if (unlikely(err)) | |
134 | return err; | |
135 | ||
237c4f2a | 136 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
3cfede14 | 137 | |
982a47ec JP |
138 | ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src); |
139 | ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst); | |
4edb9ae9 | 140 | |
237c4f2a | 141 | ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
3cfede14 | 142 | |
4edb9ae9 | 143 | return 0; |
ca78c6b6 BP |
144 | } |
145 | ||
4edb9ae9 PS |
146 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
147 | __be32 *addr, __be32 new_addr) | |
ca78c6b6 BP |
148 | { |
149 | int transport_len = skb->len - skb_transport_offset(skb); | |
4edb9ae9 PS |
150 | |
151 | if (nh->protocol == IPPROTO_TCP) { | |
ca78c6b6 | 152 | if (likely(transport_len >= sizeof(struct tcphdr))) |
4edb9ae9 PS |
153 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
154 | *addr, new_addr, 1); | |
155 | } else if (nh->protocol == IPPROTO_UDP) { | |
55ce87bc JG |
156 | if (likely(transport_len >= sizeof(struct udphdr))) { |
157 | struct udphdr *uh = udp_hdr(skb); | |
158 | ||
237c4f2a | 159 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
55ce87bc JG |
160 | inet_proto_csum_replace4(&uh->check, skb, |
161 | *addr, new_addr, 1); | |
162 | if (!uh->check) | |
163 | uh->check = CSUM_MANGLED_0; | |
164 | } | |
165 | } | |
ca78c6b6 | 166 | } |
4edb9ae9 PS |
167 | |
168 | csum_replace4(&nh->check, *addr, new_addr); | |
e2f3178f | 169 | skb_clear_hash(skb); |
4edb9ae9 | 170 | *addr = new_addr; |
ca78c6b6 BP |
171 | } |
172 | ||
bc7a5acd AA |
173 | static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, |
174 | __be32 addr[4], const __be32 new_addr[4]) | |
175 | { | |
176 | int transport_len = skb->len - skb_transport_offset(skb); | |
177 | ||
178 | if (l4_proto == IPPROTO_TCP) { | |
179 | if (likely(transport_len >= sizeof(struct tcphdr))) | |
180 | inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, | |
181 | addr, new_addr, 1); | |
182 | } else if (l4_proto == IPPROTO_UDP) { | |
183 | if (likely(transport_len >= sizeof(struct udphdr))) { | |
184 | struct udphdr *uh = udp_hdr(skb); | |
185 | ||
237c4f2a | 186 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
bc7a5acd AA |
187 | inet_proto_csum_replace16(&uh->check, skb, |
188 | addr, new_addr, 1); | |
189 | if (!uh->check) | |
190 | uh->check = CSUM_MANGLED_0; | |
191 | } | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, | |
197 | __be32 addr[4], const __be32 new_addr[4], | |
198 | bool recalculate_csum) | |
199 | { | |
200 | if (recalculate_csum) | |
201 | update_ipv6_checksum(skb, l4_proto, addr, new_addr); | |
202 | ||
e2f3178f | 203 | skb_clear_hash(skb); |
bc7a5acd AA |
204 | memcpy(addr, new_addr, sizeof(__be32[4])); |
205 | } | |
206 | ||
207 | static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc) | |
208 | { | |
209 | nh->priority = tc >> 4; | |
210 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4); | |
211 | } | |
212 | ||
213 | static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl) | |
214 | { | |
215 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16; | |
216 | nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8; | |
217 | nh->flow_lbl[2] = fl & 0x000000FF; | |
218 | } | |
219 | ||
a61680c6 JP |
220 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
221 | { | |
222 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); | |
223 | nh->ttl = new_ttl; | |
224 | } | |
225 | ||
4edb9ae9 | 226 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
064af421 | 227 | { |
ca78c6b6 | 228 | struct iphdr *nh; |
10db8b20 | 229 | int err; |
ca78c6b6 | 230 | |
10db8b20 JG |
231 | err = make_writable(skb, skb_network_offset(skb) + |
232 | sizeof(struct iphdr)); | |
233 | if (unlikely(err)) | |
234 | return err; | |
ca78c6b6 BP |
235 | |
236 | nh = ip_hdr(skb); | |
ca78c6b6 | 237 | |
4edb9ae9 PS |
238 | if (ipv4_key->ipv4_src != nh->saddr) |
239 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); | |
ca78c6b6 | 240 | |
4edb9ae9 PS |
241 | if (ipv4_key->ipv4_dst != nh->daddr) |
242 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); | |
a4a26436 | 243 | |
4edb9ae9 | 244 | if (ipv4_key->ipv4_tos != nh->tos) |
530180fd | 245 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
ca78c6b6 | 246 | |
a61680c6 JP |
247 | if (ipv4_key->ipv4_ttl != nh->ttl) |
248 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); | |
249 | ||
10db8b20 | 250 | return 0; |
064af421 BP |
251 | } |
252 | ||
bc7a5acd AA |
253 | static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key) |
254 | { | |
255 | struct ipv6hdr *nh; | |
256 | int err; | |
257 | __be32 *saddr; | |
258 | __be32 *daddr; | |
259 | ||
260 | err = make_writable(skb, skb_network_offset(skb) + | |
261 | sizeof(struct ipv6hdr)); | |
262 | if (unlikely(err)) | |
263 | return err; | |
264 | ||
265 | nh = ipv6_hdr(skb); | |
266 | saddr = (__be32 *)&nh->saddr; | |
267 | daddr = (__be32 *)&nh->daddr; | |
268 | ||
269 | if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) | |
270 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr, | |
271 | ipv6_key->ipv6_src, true); | |
272 | ||
273 | if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) { | |
274 | unsigned int offset = 0; | |
275 | int flags = OVS_IP6T_FH_F_SKIP_RH; | |
276 | bool recalc_csum = true; | |
277 | ||
278 | if (ipv6_ext_hdr(nh->nexthdr)) | |
279 | recalc_csum = ipv6_find_hdr(skb, &offset, | |
280 | NEXTHDR_ROUTING, NULL, | |
281 | &flags) != NEXTHDR_ROUTING; | |
282 | ||
283 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr, | |
284 | ipv6_key->ipv6_dst, recalc_csum); | |
285 | } | |
286 | ||
287 | set_ipv6_tc(nh, ipv6_key->ipv6_tclass); | |
288 | set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label)); | |
289 | nh->hop_limit = ipv6_key->ipv6_hlimit; | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
4edb9ae9 PS |
294 | /* Must follow make_writable() since that can move the skb data. */ |
295 | static void set_tp_port(struct sk_buff *skb, __be16 *port, | |
296 | __be16 new_port, __sum16 *check) | |
959a2ecd | 297 | { |
4edb9ae9 PS |
298 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
299 | *port = new_port; | |
e2f3178f | 300 | skb_clear_hash(skb); |
4edb9ae9 | 301 | } |
10db8b20 | 302 | |
55ce87bc JG |
303 | static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port) |
304 | { | |
305 | struct udphdr *uh = udp_hdr(skb); | |
306 | ||
237c4f2a | 307 | if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) { |
55ce87bc JG |
308 | set_tp_port(skb, port, new_port, &uh->check); |
309 | ||
310 | if (!uh->check) | |
311 | uh->check = CSUM_MANGLED_0; | |
312 | } else { | |
313 | *port = new_port; | |
e2f3178f | 314 | skb_clear_hash(skb); |
55ce87bc JG |
315 | } |
316 | } | |
317 | ||
318 | static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key) | |
4edb9ae9 PS |
319 | { |
320 | struct udphdr *uh; | |
321 | int err; | |
10db8b20 | 322 | |
4edb9ae9 PS |
323 | err = make_writable(skb, skb_transport_offset(skb) + |
324 | sizeof(struct udphdr)); | |
10db8b20 JG |
325 | if (unlikely(err)) |
326 | return err; | |
327 | ||
4edb9ae9 PS |
328 | uh = udp_hdr(skb); |
329 | if (udp_port_key->udp_src != uh->source) | |
55ce87bc | 330 | set_udp_port(skb, &uh->source, udp_port_key->udp_src); |
4edb9ae9 PS |
331 | |
332 | if (udp_port_key->udp_dst != uh->dest) | |
55ce87bc | 333 | set_udp_port(skb, &uh->dest, udp_port_key->udp_dst); |
10db8b20 JG |
334 | |
335 | return 0; | |
959a2ecd JP |
336 | } |
337 | ||
55ce87bc | 338 | static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key) |
064af421 | 339 | { |
4edb9ae9 | 340 | struct tcphdr *th; |
10db8b20 | 341 | int err; |
064af421 | 342 | |
10db8b20 JG |
343 | err = make_writable(skb, skb_transport_offset(skb) + |
344 | sizeof(struct tcphdr)); | |
345 | if (unlikely(err)) | |
346 | return err; | |
ca78c6b6 | 347 | |
4edb9ae9 PS |
348 | th = tcp_hdr(skb); |
349 | if (tcp_port_key->tcp_src != th->source) | |
350 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); | |
064af421 | 351 | |
4edb9ae9 PS |
352 | if (tcp_port_key->tcp_dst != th->dest) |
353 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); | |
ca78c6b6 | 354 | |
10db8b20 | 355 | return 0; |
064af421 BP |
356 | } |
357 | ||
10f72e3d JS |
358 | static int set_sctp(struct sk_buff *skb, |
359 | const struct ovs_key_sctp *sctp_port_key) | |
360 | { | |
361 | struct sctphdr *sh; | |
362 | int err; | |
363 | unsigned int sctphoff = skb_transport_offset(skb); | |
364 | ||
365 | err = make_writable(skb, sctphoff + sizeof(struct sctphdr)); | |
366 | if (unlikely(err)) | |
367 | return err; | |
368 | ||
369 | sh = sctp_hdr(skb); | |
370 | if (sctp_port_key->sctp_src != sh->source || | |
371 | sctp_port_key->sctp_dst != sh->dest) { | |
372 | __le32 old_correct_csum, new_csum, old_csum; | |
373 | ||
374 | old_csum = sh->checksum; | |
375 | old_correct_csum = sctp_compute_cksum(skb, sctphoff); | |
376 | ||
377 | sh->source = sctp_port_key->sctp_src; | |
378 | sh->dest = sctp_port_key->sctp_dst; | |
379 | ||
380 | new_csum = sctp_compute_cksum(skb, sctphoff); | |
381 | ||
382 | /* Carry any checksum errors through. */ | |
383 | sh->checksum = old_csum ^ old_correct_csum ^ new_csum; | |
384 | ||
e2f3178f | 385 | skb_clear_hash(skb); |
10f72e3d JS |
386 | } |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
f15c8639 | 391 | static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
064af421 | 392 | { |
f15c8639 | 393 | struct vport *vport; |
064af421 | 394 | |
f15c8639 JG |
395 | if (unlikely(!skb)) |
396 | return -ENOMEM; | |
064af421 | 397 | |
95b1d73a | 398 | vport = ovs_vport_rcu(dp, out_port); |
f15c8639 JG |
399 | if (unlikely(!vport)) { |
400 | kfree_skb(skb); | |
401 | return -ENODEV; | |
402 | } | |
064af421 | 403 | |
850b6b3b | 404 | ovs_vport_send(vport, skb); |
f15c8639 | 405 | return 0; |
064af421 BP |
406 | } |
407 | ||
98403001 BP |
408 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
409 | const struct nlattr *attr) | |
064af421 | 410 | { |
856081f6 | 411 | struct dp_upcall_info upcall; |
98403001 BP |
412 | const struct nlattr *a; |
413 | int rem; | |
856081f6 | 414 | |
d1d71a36 AZ |
415 | BUG_ON(!OVS_CB(skb)->pkt_key); |
416 | ||
df2c07f4 | 417 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
d1d71a36 | 418 | upcall.key = OVS_CB(skb)->pkt_key; |
98403001 | 419 | upcall.userdata = NULL; |
28aea917 | 420 | upcall.portid = 0; |
98403001 BP |
421 | |
422 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
423 | a = nla_next(a, &rem)) { | |
424 | switch (nla_type(a)) { | |
425 | case OVS_USERSPACE_ATTR_USERDATA: | |
426 | upcall.userdata = a; | |
427 | break; | |
428 | ||
429 | case OVS_USERSPACE_ATTR_PID: | |
28aea917 | 430 | upcall.portid = nla_get_u32(a); |
98403001 BP |
431 | break; |
432 | } | |
433 | } | |
434 | ||
850b6b3b | 435 | return ovs_dp_upcall(dp, skb, &upcall); |
064af421 BP |
436 | } |
437 | ||
fbf4f74d SH |
438 | static bool last_action(const struct nlattr *a, int rem) |
439 | { | |
440 | return a->nla_len == rem; | |
441 | } | |
442 | ||
6ff686f2 | 443 | static int sample(struct datapath *dp, struct sk_buff *skb, |
85c9de19 | 444 | const struct nlattr *attr) |
6ff686f2 PS |
445 | { |
446 | const struct nlattr *acts_list = NULL; | |
447 | const struct nlattr *a; | |
fbf4f74d | 448 | struct sk_buff *sample_skb; |
6ff686f2 PS |
449 | int rem; |
450 | ||
451 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
452 | a = nla_next(a, &rem)) { | |
453 | switch (nla_type(a)) { | |
454 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
e2f3178f | 455 | if (prandom_u32() >= nla_get_u32(a)) |
6ff686f2 PS |
456 | return 0; |
457 | break; | |
458 | ||
459 | case OVS_SAMPLE_ATTR_ACTIONS: | |
460 | acts_list = a; | |
461 | break; | |
462 | } | |
463 | } | |
464 | ||
fbf4f74d SH |
465 | rem = nla_len(acts_list); |
466 | a = nla_data(acts_list); | |
467 | ||
468 | /* Actions list is either empty or only contains a single user-space | |
469 | * action, the latter being a special case as it is the only known | |
470 | * usage of the sample action. | |
471 | * In these special cases don't clone the skb as there are no | |
472 | * side-effects in the nested actions. | |
473 | * Otherwise, clone in case the nested actions have side effects. */ | |
474 | if (likely(rem == 0 || | |
475 | (nla_type(a) == OVS_ACTION_ATTR_USERSPACE && | |
476 | last_action(a, rem)))) { | |
477 | sample_skb = skb; | |
478 | skb_get(skb); | |
479 | } else { | |
480 | sample_skb = skb_clone(skb, GFP_ATOMIC); | |
481 | } | |
482 | ||
483 | /* Note that do_execute_actions() never consumes skb. | |
484 | * In the case where skb has been cloned above it is the clone that | |
485 | * is consumed. Otherwise the skb_get(skb) call prevents | |
486 | * consumption by do_execute_actions(). Thus, it is safe to simply | |
487 | * return the error code and let the caller (also | |
488 | * do_execute_actions()) free skb on error. */ | |
489 | return do_execute_actions(dp, sample_skb, a, rem); | |
6ff686f2 PS |
490 | } |
491 | ||
7804df20 AZ |
492 | static void execute_hash(struct sk_buff *skb, const struct nlattr *attr) |
493 | { | |
494 | struct sw_flow_key *key = OVS_CB(skb)->pkt_key; | |
495 | struct ovs_action_hash *hash_act = nla_data(attr); | |
496 | u32 hash = 0; | |
497 | ||
498 | /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */ | |
e2f3178f | 499 | hash = skb_get_hash(skb); |
7804df20 AZ |
500 | hash = jhash_1word(hash, hash_act->hash_basis); |
501 | if (!hash) | |
502 | hash = 0x1; | |
503 | ||
504 | key->ovs_flow_hash = hash; | |
505 | } | |
506 | ||
4edb9ae9 | 507 | static int execute_set_action(struct sk_buff *skb, |
85c9de19 | 508 | const struct nlattr *nested_attr) |
4edb9ae9 | 509 | { |
15c39847 | 510 | int err = 0; |
4edb9ae9 PS |
511 | |
512 | switch (nla_type(nested_attr)) { | |
abff858b PS |
513 | case OVS_KEY_ATTR_PRIORITY: |
514 | skb->priority = nla_get_u32(nested_attr); | |
515 | break; | |
516 | ||
72e8bf28 | 517 | case OVS_KEY_ATTR_SKB_MARK: |
3025a772 | 518 | skb->mark = nla_get_u32(nested_attr); |
72e8bf28 AA |
519 | break; |
520 | ||
f0cd669f JG |
521 | case OVS_KEY_ATTR_TUNNEL_INFO: |
522 | OVS_CB(skb)->tun_info = nla_data(nested_attr); | |
4edb9ae9 PS |
523 | break; |
524 | ||
525 | case OVS_KEY_ATTR_ETHERNET: | |
526 | err = set_eth_addr(skb, nla_data(nested_attr)); | |
527 | break; | |
528 | ||
529 | case OVS_KEY_ATTR_IPV4: | |
530 | err = set_ipv4(skb, nla_data(nested_attr)); | |
531 | break; | |
532 | ||
bc7a5acd AA |
533 | case OVS_KEY_ATTR_IPV6: |
534 | err = set_ipv6(skb, nla_data(nested_attr)); | |
535 | break; | |
536 | ||
4edb9ae9 | 537 | case OVS_KEY_ATTR_TCP: |
55ce87bc | 538 | err = set_tcp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
539 | break; |
540 | ||
541 | case OVS_KEY_ATTR_UDP: | |
55ce87bc | 542 | err = set_udp(skb, nla_data(nested_attr)); |
4edb9ae9 | 543 | break; |
10f72e3d JS |
544 | |
545 | case OVS_KEY_ATTR_SCTP: | |
546 | err = set_sctp(skb, nla_data(nested_attr)); | |
547 | break; | |
4edb9ae9 | 548 | } |
15c39847 | 549 | |
4edb9ae9 PS |
550 | return err; |
551 | } | |
552 | ||
a6059080 AZ |
553 | static int execute_recirc(struct datapath *dp, struct sk_buff *skb, |
554 | const struct nlattr *a) | |
555 | { | |
556 | struct sw_flow_key recirc_key; | |
557 | const struct vport *p = OVS_CB(skb)->input_vport; | |
558 | uint32_t hash = OVS_CB(skb)->pkt_key->ovs_flow_hash; | |
559 | int err; | |
560 | ||
561 | err = ovs_flow_extract(skb, p->port_no, &recirc_key); | |
97a360ce SH |
562 | if (err) { |
563 | kfree_skb(skb); | |
a6059080 | 564 | return err; |
97a360ce | 565 | } |
a6059080 AZ |
566 | |
567 | recirc_key.ovs_flow_hash = hash; | |
568 | recirc_key.recirc_id = nla_get_u32(a); | |
569 | ||
ca93abce | 570 | ovs_dp_process_packet_with_key(skb, &recirc_key, true); |
a6059080 AZ |
571 | |
572 | return 0; | |
573 | } | |
574 | ||
064af421 | 575 | /* Execute a list of actions against 'skb'. */ |
871dfe07 | 576 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
fbf4f74d | 577 | const struct nlattr *attr, int len) |
064af421 BP |
578 | { |
579 | /* Every output action needs a separate clone of 'skb', but the common | |
580 | * case is just a single output action, so that doing a clone and | |
581 | * then freeing the original skbuff is wasteful. So the following code | |
582 | * is slightly obscure just to avoid that. */ | |
583 | int prev_port = -1; | |
cdee00fd | 584 | const struct nlattr *a; |
10db8b20 | 585 | int rem; |
72b06300 | 586 | |
6ff686f2 | 587 | for (a = attr, rem = len; rem > 0; |
a4af2475 | 588 | a = nla_next(a, &rem)) { |
10db8b20 JG |
589 | int err = 0; |
590 | ||
064af421 | 591 | if (prev_port != -1) { |
7956695a | 592 | do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port); |
064af421 BP |
593 | prev_port = -1; |
594 | } | |
595 | ||
cdee00fd | 596 | switch (nla_type(a)) { |
df2c07f4 | 597 | case OVS_ACTION_ATTR_OUTPUT: |
cdee00fd | 598 | prev_port = nla_get_u32(a); |
064af421 BP |
599 | break; |
600 | ||
df2c07f4 | 601 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 602 | output_userspace(dp, skb, a); |
064af421 | 603 | break; |
7804df20 AZ |
604 | |
605 | case OVS_ACTION_ATTR_HASH: | |
606 | execute_hash(skb, a); | |
607 | break; | |
064af421 | 608 | |
fea393b1 BP |
609 | case OVS_ACTION_ATTR_PUSH_VLAN: |
610 | err = push_vlan(skb, nla_data(a)); | |
4edb9ae9 | 611 | if (unlikely(err)) /* skb already freed. */ |
d9065a90 | 612 | return err; |
064af421 BP |
613 | break; |
614 | ||
fea393b1 | 615 | case OVS_ACTION_ATTR_POP_VLAN: |
d9065a90 | 616 | err = pop_vlan(skb); |
064af421 BP |
617 | break; |
618 | ||
a6059080 AZ |
619 | case OVS_ACTION_ATTR_RECIRC: { |
620 | struct sk_buff *recirc_skb; | |
a6059080 | 621 | |
fbf4f74d | 622 | if (!last_action(a, rem)) |
a6059080 AZ |
623 | recirc_skb = skb_clone(skb, GFP_ATOMIC); |
624 | else | |
625 | recirc_skb = skb; | |
626 | ||
627 | err = execute_recirc(dp, recirc_skb, a); | |
628 | ||
97a360ce | 629 | if (recirc_skb == skb) |
a6059080 AZ |
630 | return err; |
631 | ||
632 | break; | |
633 | } | |
634 | ||
4edb9ae9 | 635 | case OVS_ACTION_ATTR_SET: |
85c9de19 | 636 | err = execute_set_action(skb, nla_data(a)); |
064af421 | 637 | break; |
c1c9c9c4 | 638 | |
6ff686f2 | 639 | case OVS_ACTION_ATTR_SAMPLE: |
85c9de19 | 640 | err = sample(dp, skb, a); |
6ff686f2 | 641 | break; |
6ff686f2 | 642 | } |
15c39847 | 643 | |
10db8b20 JG |
644 | if (unlikely(err)) { |
645 | kfree_skb(skb); | |
646 | return err; | |
647 | } | |
064af421 | 648 | } |
6c222e55 | 649 | |
fbf4f74d | 650 | if (prev_port != -1) |
064af421 | 651 | do_output(dp, skb, prev_port); |
fbf4f74d | 652 | else |
5b95ab0e | 653 | consume_skb(skb); |
10db8b20 | 654 | |
a5225dd6 | 655 | return 0; |
064af421 | 656 | } |
871dfe07 | 657 | |
e9141eec | 658 | /* We limit the number of times that we pass into execute_actions() |
ca93abce AZ |
659 | * to avoid blowing out the stack in the event that we have a loop. |
660 | * | |
661 | * Each loop adds some (estimated) cost to the kernel stack. | |
662 | * The loop terminates when the max cost is exceeded. | |
663 | * */ | |
664 | #define RECIRC_STACK_COST 1 | |
665 | #define DEFAULT_STACK_COST 4 | |
666 | /* Allow up to 4 regular services, and up to 3 recirculations */ | |
667 | #define MAX_STACK_COST (DEFAULT_STACK_COST * 4 + RECIRC_STACK_COST * 3) | |
e9141eec PS |
668 | |
669 | struct loop_counter { | |
ca93abce | 670 | u8 stack_cost; /* loop stack cost. */ |
e9141eec PS |
671 | bool looping; /* Loop detected? */ |
672 | }; | |
673 | ||
674 | static DEFINE_PER_CPU(struct loop_counter, loop_counters); | |
675 | ||
676 | static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions) | |
677 | { | |
678 | if (net_ratelimit()) | |
ca93abce AZ |
679 | pr_warn("%s: flow loop detected, dropping\n", |
680 | ovs_dp_name(dp)); | |
e9141eec PS |
681 | actions->actions_len = 0; |
682 | return -ELOOP; | |
683 | } | |
684 | ||
871dfe07 | 685 | /* Execute a list of actions against 'skb'. */ |
ca93abce | 686 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, bool recirc) |
871dfe07 | 687 | { |
a4af2475 | 688 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
ca93abce | 689 | const u8 stack_cost = recirc ? RECIRC_STACK_COST : DEFAULT_STACK_COST; |
a4af2475 BP |
690 | struct loop_counter *loop; |
691 | int error; | |
692 | ||
693 | /* Check whether we've looped too much. */ | |
e9141eec | 694 | loop = &__get_cpu_var(loop_counters); |
ca93abce AZ |
695 | loop->stack_cost += stack_cost; |
696 | if (unlikely(loop->stack_cost > MAX_STACK_COST)) | |
a4af2475 BP |
697 | loop->looping = true; |
698 | if (unlikely(loop->looping)) { | |
699 | error = loop_suppress(dp, acts); | |
700 | kfree_skb(skb); | |
701 | goto out_loop; | |
702 | } | |
871dfe07 | 703 | |
f0cd669f | 704 | OVS_CB(skb)->tun_info = NULL; |
fbf4f74d | 705 | error = do_execute_actions(dp, skb, acts->actions, acts->actions_len); |
a4af2475 BP |
706 | |
707 | /* Check whether sub-actions looped too much. */ | |
708 | if (unlikely(loop->looping)) | |
709 | error = loop_suppress(dp, acts); | |
710 | ||
711 | out_loop: | |
ca93abce AZ |
712 | /* Decrement loop stack cost. */ |
713 | loop->stack_cost -= stack_cost; | |
714 | if (!loop->stack_cost) | |
a4af2475 | 715 | loop->looping = false; |
871dfe07 | 716 | |
a4af2475 | 717 | return error; |
871dfe07 | 718 | } |