]> git.proxmox.com Git - ovs.git/blob - datapath/actions.c
datapath: backport: ovs: set name assign type of internal port
[ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2015 Nicira, Inc.
3 *
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>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/in6.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_vlan.h>
32
33 #include <net/dst.h>
34 #include <net/ip.h>
35 #include <net/ipv6.h>
36 #include <net/checksum.h>
37 #include <net/dsfield.h>
38 #include <net/mpls.h>
39 #include <net/sctp/checksum.h>
40
41 #include "datapath.h"
42 #include "conntrack.h"
43 #include "gso.h"
44 #include "vport.h"
45
46 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
47 struct sw_flow_key *key,
48 const struct nlattr *attr, int len);
49
50 struct deferred_action {
51 struct sk_buff *skb;
52 const struct nlattr *actions;
53
54 /* Store pkt_key clone when creating deferred action. */
55 struct sw_flow_key pkt_key;
56 };
57
58 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
59 struct ovs_frag_data {
60 unsigned long dst;
61 struct vport *vport;
62 struct ovs_gso_cb cb;
63 __be16 inner_protocol;
64 __u16 vlan_tci;
65 __be16 vlan_proto;
66 unsigned int l2_len;
67 u8 l2_data[MAX_L2_LEN];
68 };
69
70 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
71
72 #define DEFERRED_ACTION_FIFO_SIZE 10
73 struct action_fifo {
74 int head;
75 int tail;
76 /* Deferred action fifo queue storage. */
77 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
78 };
79
80 static struct action_fifo __percpu *action_fifos;
81 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
82 * looping by the network stack
83 */
84 static DEFINE_PER_CPU(int, exec_actions_level);
85
86 static void action_fifo_init(struct action_fifo *fifo)
87 {
88 fifo->head = 0;
89 fifo->tail = 0;
90 }
91
92 static bool action_fifo_is_empty(const struct action_fifo *fifo)
93 {
94 return (fifo->head == fifo->tail);
95 }
96
97 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
98 {
99 if (action_fifo_is_empty(fifo))
100 return NULL;
101
102 return &fifo->fifo[fifo->tail++];
103 }
104
105 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
106 {
107 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
108 return NULL;
109
110 return &fifo->fifo[fifo->head++];
111 }
112
113 /* Return queue entry if fifo is not full */
114 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
115 const struct sw_flow_key *key,
116 const struct nlattr *attr)
117 {
118 struct action_fifo *fifo;
119 struct deferred_action *da;
120
121 fifo = this_cpu_ptr(action_fifos);
122 da = action_fifo_put(fifo);
123 if (da) {
124 da->skb = skb;
125 da->actions = attr;
126 da->pkt_key = *key;
127 }
128
129 return da;
130 }
131
132 static void invalidate_flow_key(struct sw_flow_key *key)
133 {
134 key->eth.type = htons(0);
135 }
136
137 static bool is_flow_key_valid(const struct sw_flow_key *key)
138 {
139 return !!key->eth.type;
140 }
141
142 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
143 const struct ovs_action_push_mpls *mpls)
144 {
145 __be32 *new_mpls_lse;
146 struct ethhdr *hdr;
147
148 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
149 if (skb->encapsulation)
150 return -ENOTSUPP;
151
152 if (skb_cow_head(skb, MPLS_HLEN) < 0)
153 return -ENOMEM;
154
155 skb_push(skb, MPLS_HLEN);
156 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
157 skb->mac_len);
158 skb_reset_mac_header(skb);
159
160 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
161 *new_mpls_lse = mpls->mpls_lse;
162
163 skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
164
165 hdr = eth_hdr(skb);
166 hdr->h_proto = mpls->mpls_ethertype;
167 if (!ovs_skb_get_inner_protocol(skb))
168 ovs_skb_set_inner_protocol(skb, skb->protocol);
169 skb->protocol = mpls->mpls_ethertype;
170
171 invalidate_flow_key(key);
172 return 0;
173 }
174
175 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
176 const __be16 ethertype)
177 {
178 struct ethhdr *hdr;
179 int err;
180
181 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
182 if (unlikely(err))
183 return err;
184
185 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
186
187 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
188 skb->mac_len);
189
190 __skb_pull(skb, MPLS_HLEN);
191 skb_reset_mac_header(skb);
192
193 /* skb_mpls_header() is used to locate the ethertype
194 * field correctly in the presence of VLAN tags.
195 */
196 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
197 hdr->h_proto = ethertype;
198 if (eth_p_mpls(skb->protocol))
199 skb->protocol = ethertype;
200
201 invalidate_flow_key(key);
202 return 0;
203 }
204
205 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
206 const __be32 *mpls_lse, const __be32 *mask)
207 {
208 __be32 *stack;
209 __be32 lse;
210 int err;
211
212 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
213 if (unlikely(err))
214 return err;
215
216 stack = (__be32 *)skb_mpls_header(skb);
217 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
218 if (skb->ip_summed == CHECKSUM_COMPLETE) {
219 __be32 diff[] = { ~(*stack), lse };
220
221 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
222 ~skb->csum);
223 }
224
225 *stack = lse;
226 flow_key->mpls.top_lse = lse;
227 return 0;
228 }
229
230 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
231 {
232 int err;
233
234 err = skb_vlan_pop(skb);
235 if (skb_vlan_tag_present(skb))
236 invalidate_flow_key(key);
237 else
238 key->eth.tci = 0;
239 return err;
240 }
241
242 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
243 const struct ovs_action_push_vlan *vlan)
244 {
245 if (skb_vlan_tag_present(skb))
246 invalidate_flow_key(key);
247 else
248 key->eth.tci = vlan->vlan_tci;
249 return skb_vlan_push(skb, vlan->vlan_tpid,
250 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
251 }
252
253 /* 'src' is already properly masked. */
254 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
255 {
256 u16 *dst = (u16 *)dst_;
257 const u16 *src = (const u16 *)src_;
258 const u16 *mask = (const u16 *)mask_;
259
260 OVS_SET_MASKED(dst[0], src[0], mask[0]);
261 OVS_SET_MASKED(dst[1], src[1], mask[1]);
262 OVS_SET_MASKED(dst[2], src[2], mask[2]);
263 }
264
265 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
266 const struct ovs_key_ethernet *key,
267 const struct ovs_key_ethernet *mask)
268 {
269 int err;
270
271 err = skb_ensure_writable(skb, ETH_HLEN);
272 if (unlikely(err))
273 return err;
274
275 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
276
277 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
278 mask->eth_src);
279 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
280 mask->eth_dst);
281
282 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
283
284 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
285 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
286 return 0;
287 }
288
289 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
290 __be32 addr, __be32 new_addr)
291 {
292 int transport_len = skb->len - skb_transport_offset(skb);
293
294 if (nh->frag_off & htons(IP_OFFSET))
295 return;
296
297 if (nh->protocol == IPPROTO_TCP) {
298 if (likely(transport_len >= sizeof(struct tcphdr)))
299 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
300 addr, new_addr, true);
301 } else if (nh->protocol == IPPROTO_UDP) {
302 if (likely(transport_len >= sizeof(struct udphdr))) {
303 struct udphdr *uh = udp_hdr(skb);
304
305 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
306 inet_proto_csum_replace4(&uh->check, skb,
307 addr, new_addr, true);
308 if (!uh->check)
309 uh->check = CSUM_MANGLED_0;
310 }
311 }
312 }
313
314 }
315
316 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
317 __be32 *addr, __be32 new_addr)
318 {
319 update_ip_l4_checksum(skb, nh, *addr, new_addr);
320 csum_replace4(&nh->check, *addr, new_addr);
321 skb_clear_hash(skb);
322 *addr = new_addr;
323 }
324
325 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
326 __be32 addr[4], const __be32 new_addr[4])
327 {
328 int transport_len = skb->len - skb_transport_offset(skb);
329
330 if (l4_proto == NEXTHDR_TCP) {
331 if (likely(transport_len >= sizeof(struct tcphdr)))
332 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
333 addr, new_addr, true);
334 } else if (l4_proto == NEXTHDR_UDP) {
335 if (likely(transport_len >= sizeof(struct udphdr))) {
336 struct udphdr *uh = udp_hdr(skb);
337
338 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
339 inet_proto_csum_replace16(&uh->check, skb,
340 addr, new_addr, true);
341 if (!uh->check)
342 uh->check = CSUM_MANGLED_0;
343 }
344 }
345 } else if (l4_proto == NEXTHDR_ICMP) {
346 if (likely(transport_len >= sizeof(struct icmp6hdr)))
347 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
348 skb, addr, new_addr, true);
349 }
350 }
351
352 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
353 const __be32 mask[4], __be32 masked[4])
354 {
355 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
356 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
357 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
358 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
359 }
360
361 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
362 __be32 addr[4], const __be32 new_addr[4],
363 bool recalculate_csum)
364 {
365 if (likely(recalculate_csum))
366 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
367
368 skb_clear_hash(skb);
369 memcpy(addr, new_addr, sizeof(__be32[4]));
370 }
371
372 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
373 {
374 /* Bits 21-24 are always unmasked, so this retains their values. */
375 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
376 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
377 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
378 }
379
380 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
381 u8 mask)
382 {
383 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
384
385 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
386 nh->ttl = new_ttl;
387 }
388
389 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
390 const struct ovs_key_ipv4 *key,
391 const struct ovs_key_ipv4 *mask)
392 {
393 struct iphdr *nh;
394 __be32 new_addr;
395 int err;
396
397 err = skb_ensure_writable(skb, skb_network_offset(skb) +
398 sizeof(struct iphdr));
399 if (unlikely(err))
400 return err;
401
402 nh = ip_hdr(skb);
403
404 /* Setting an IP addresses is typically only a side effect of
405 * matching on them in the current userspace implementation, so it
406 * makes sense to check if the value actually changed.
407 */
408 if (mask->ipv4_src) {
409 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
410
411 if (unlikely(new_addr != nh->saddr)) {
412 set_ip_addr(skb, nh, &nh->saddr, new_addr);
413 flow_key->ipv4.addr.src = new_addr;
414 }
415 }
416 if (mask->ipv4_dst) {
417 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
418
419 if (unlikely(new_addr != nh->daddr)) {
420 set_ip_addr(skb, nh, &nh->daddr, new_addr);
421 flow_key->ipv4.addr.dst = new_addr;
422 }
423 }
424 if (mask->ipv4_tos) {
425 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
426 flow_key->ip.tos = nh->tos;
427 }
428 if (mask->ipv4_ttl) {
429 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
430 flow_key->ip.ttl = nh->ttl;
431 }
432
433 return 0;
434 }
435
436 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
437 {
438 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
439 }
440
441 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
442 const struct ovs_key_ipv6 *key,
443 const struct ovs_key_ipv6 *mask)
444 {
445 struct ipv6hdr *nh;
446 int err;
447
448 err = skb_ensure_writable(skb, skb_network_offset(skb) +
449 sizeof(struct ipv6hdr));
450 if (unlikely(err))
451 return err;
452
453 nh = ipv6_hdr(skb);
454
455 /* Setting an IP addresses is typically only a side effect of
456 * matching on them in the current userspace implementation, so it
457 * makes sense to check if the value actually changed.
458 */
459 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
460 __be32 *saddr = (__be32 *)&nh->saddr;
461 __be32 masked[4];
462
463 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
464
465 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
466 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
467 true);
468 memcpy(&flow_key->ipv6.addr.src, masked,
469 sizeof(flow_key->ipv6.addr.src));
470 }
471 }
472 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
473 unsigned int offset = 0;
474 int flags = IP6_FH_F_SKIP_RH;
475 bool recalc_csum = true;
476 __be32 *daddr = (__be32 *)&nh->daddr;
477 __be32 masked[4];
478
479 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
480
481 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
482 if (ipv6_ext_hdr(nh->nexthdr))
483 recalc_csum = (ipv6_find_hdr(skb, &offset,
484 NEXTHDR_ROUTING,
485 NULL, &flags)
486 != NEXTHDR_ROUTING);
487
488 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
489 recalc_csum);
490 memcpy(&flow_key->ipv6.addr.dst, masked,
491 sizeof(flow_key->ipv6.addr.dst));
492 }
493 }
494 if (mask->ipv6_tclass) {
495 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
496 flow_key->ip.tos = ipv6_get_dsfield(nh);
497 }
498 if (mask->ipv6_label) {
499 set_ipv6_fl(nh, ntohl(key->ipv6_label),
500 ntohl(mask->ipv6_label));
501 flow_key->ipv6.label =
502 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
503 }
504 if (mask->ipv6_hlimit) {
505 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
506 mask->ipv6_hlimit);
507 flow_key->ip.ttl = nh->hop_limit;
508 }
509 return 0;
510 }
511
512 /* Must follow skb_ensure_writable() since that can move the skb data. */
513 static void set_tp_port(struct sk_buff *skb, __be16 *port,
514 __be16 new_port, __sum16 *check)
515 {
516 inet_proto_csum_replace2(check, skb, *port, new_port, false);
517 *port = new_port;
518 }
519
520 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
521 const struct ovs_key_udp *key,
522 const struct ovs_key_udp *mask)
523 {
524 struct udphdr *uh;
525 __be16 src, dst;
526 int err;
527
528 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
529 sizeof(struct udphdr));
530 if (unlikely(err))
531 return err;
532
533 uh = udp_hdr(skb);
534 /* Either of the masks is non-zero, so do not bother checking them. */
535 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
536 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
537
538 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
539 if (likely(src != uh->source)) {
540 set_tp_port(skb, &uh->source, src, &uh->check);
541 flow_key->tp.src = src;
542 }
543 if (likely(dst != uh->dest)) {
544 set_tp_port(skb, &uh->dest, dst, &uh->check);
545 flow_key->tp.dst = dst;
546 }
547
548 if (unlikely(!uh->check))
549 uh->check = CSUM_MANGLED_0;
550 } else {
551 uh->source = src;
552 uh->dest = dst;
553 flow_key->tp.src = src;
554 flow_key->tp.dst = dst;
555 }
556
557 skb_clear_hash(skb);
558
559 return 0;
560 }
561
562 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
563 const struct ovs_key_tcp *key,
564 const struct ovs_key_tcp *mask)
565 {
566 struct tcphdr *th;
567 __be16 src, dst;
568 int err;
569
570 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
571 sizeof(struct tcphdr));
572 if (unlikely(err))
573 return err;
574
575 th = tcp_hdr(skb);
576 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
577 if (likely(src != th->source)) {
578 set_tp_port(skb, &th->source, src, &th->check);
579 flow_key->tp.src = src;
580 }
581 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
582 if (likely(dst != th->dest)) {
583 set_tp_port(skb, &th->dest, dst, &th->check);
584 flow_key->tp.dst = dst;
585 }
586 skb_clear_hash(skb);
587
588 return 0;
589 }
590
591 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
592 const struct ovs_key_sctp *key,
593 const struct ovs_key_sctp *mask)
594 {
595 unsigned int sctphoff = skb_transport_offset(skb);
596 struct sctphdr *sh;
597 __le32 old_correct_csum, new_csum, old_csum;
598 int err;
599
600 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
601 if (unlikely(err))
602 return err;
603
604 sh = sctp_hdr(skb);
605 old_csum = sh->checksum;
606 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
607
608 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
609 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
610
611 new_csum = sctp_compute_cksum(skb, sctphoff);
612
613 /* Carry any checksum errors through. */
614 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
615
616 skb_clear_hash(skb);
617 flow_key->tp.src = sh->source;
618 flow_key->tp.dst = sh->dest;
619
620 return 0;
621 }
622
623 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
624 {
625 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
626 struct vport *vport = data->vport;
627
628 if (skb_cow_head(skb, data->l2_len) < 0) {
629 kfree_skb(skb);
630 return -ENOMEM;
631 }
632
633 __skb_dst_copy(skb, data->dst);
634 *OVS_GSO_CB(skb) = data->cb;
635 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
636 skb->vlan_tci = data->vlan_tci;
637 skb->vlan_proto = data->vlan_proto;
638
639 /* Reconstruct the MAC header. */
640 skb_push(skb, data->l2_len);
641 memcpy(skb->data, &data->l2_data, data->l2_len);
642 skb_postpush_rcsum(skb, skb->data, data->l2_len);
643 skb_reset_mac_header(skb);
644
645 ovs_vport_send(vport, skb);
646 return 0;
647 }
648
649 static unsigned int
650 ovs_dst_get_mtu(const struct dst_entry *dst)
651 {
652 return dst->dev->mtu;
653 }
654
655 static struct dst_ops ovs_dst_ops = {
656 .family = AF_UNSPEC,
657 .mtu = ovs_dst_get_mtu,
658 };
659
660 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
661 * ovs_vport_output(), which is called once per fragmented packet.
662 */
663 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
664 {
665 unsigned int hlen = skb_network_offset(skb);
666 struct ovs_frag_data *data;
667
668 data = this_cpu_ptr(&ovs_frag_data_storage);
669 data->dst = (unsigned long) skb_dst(skb);
670 data->vport = vport;
671 data->cb = *OVS_GSO_CB(skb);
672 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
673 data->vlan_tci = skb->vlan_tci;
674 data->vlan_proto = skb->vlan_proto;
675 data->l2_len = hlen;
676 memcpy(&data->l2_data, skb->data, hlen);
677
678 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
679 skb_pull(skb, hlen);
680 }
681
682 static void ovs_fragment(struct net *net, struct vport *vport,
683 struct sk_buff *skb, u16 mru, __be16 ethertype)
684 {
685 if (skb_network_offset(skb) > MAX_L2_LEN) {
686 OVS_NLERR(1, "L2 header too long to fragment");
687 goto err;
688 }
689
690 if (ethertype == htons(ETH_P_IP)) {
691 struct dst_entry ovs_dst;
692 unsigned long orig_dst;
693
694 prepare_frag(vport, skb);
695 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
696 DST_OBSOLETE_NONE, DST_NOCOUNT);
697 ovs_dst.dev = vport->dev;
698
699 orig_dst = (unsigned long) skb_dst(skb);
700 skb_dst_set_noref(skb, &ovs_dst);
701 IPCB(skb)->frag_max_size = mru;
702
703 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
704 refdst_drop(orig_dst);
705 } else if (ethertype == htons(ETH_P_IPV6)) {
706 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
707 unsigned long orig_dst;
708 struct rt6_info ovs_rt;
709
710 if (!v6ops) {
711 goto err;
712 }
713
714 prepare_frag(vport, skb);
715 memset(&ovs_rt, 0, sizeof(ovs_rt));
716 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
717 DST_OBSOLETE_NONE, DST_NOCOUNT);
718 ovs_rt.dst.dev = vport->dev;
719
720 orig_dst = (unsigned long) skb_dst(skb);
721 skb_dst_set_noref(skb, &ovs_rt.dst);
722 IP6CB(skb)->frag_max_size = mru;
723
724 v6ops->fragment(skb->sk, skb, ovs_vport_output);
725 refdst_drop(orig_dst);
726 } else {
727 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
728 ovs_vport_name(vport), ntohs(ethertype), mru,
729 vport->dev->mtu);
730 goto err;
731 }
732
733 return;
734 err:
735 kfree_skb(skb);
736 }
737
738 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
739 struct sw_flow_key *key)
740 {
741 struct vport *vport = ovs_vport_rcu(dp, out_port);
742
743 if (likely(vport)) {
744 u16 mru = OVS_CB(skb)->mru;
745 u32 cutlen = OVS_CB(skb)->cutlen;
746
747 if (unlikely(cutlen > 0)) {
748 if (skb->len - cutlen > ETH_HLEN)
749 pskb_trim(skb, skb->len - cutlen);
750 else
751 pskb_trim(skb, ETH_HLEN);
752 }
753
754 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
755 ovs_vport_send(vport, skb);
756 } else if (mru <= vport->dev->mtu) {
757 struct net *net = ovs_dp_get_net(dp);
758 __be16 ethertype = key->eth.type;
759
760 if (!is_flow_key_valid(key)) {
761 if (eth_p_mpls(skb->protocol))
762 ethertype = ovs_skb_get_inner_protocol(skb);
763 else
764 ethertype = vlan_get_protocol(skb);
765 }
766
767 ovs_fragment(net, vport, skb, mru, ethertype);
768 } else {
769 OVS_NLERR(true, "Cannot fragment IP frames");
770 kfree_skb(skb);
771 }
772 } else {
773 kfree_skb(skb);
774 }
775 }
776
777 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
778 struct sw_flow_key *key, const struct nlattr *attr,
779 const struct nlattr *actions, int actions_len,
780 uint32_t cutlen)
781 {
782 struct dp_upcall_info upcall;
783 const struct nlattr *a;
784 int rem, err;
785
786 memset(&upcall, 0, sizeof(upcall));
787 upcall.cmd = OVS_PACKET_CMD_ACTION;
788 upcall.mru = OVS_CB(skb)->mru;
789
790 SKB_INIT_FILL_METADATA_DST(skb);
791 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
792 a = nla_next(a, &rem)) {
793 switch (nla_type(a)) {
794 case OVS_USERSPACE_ATTR_USERDATA:
795 upcall.userdata = a;
796 break;
797
798 case OVS_USERSPACE_ATTR_PID:
799 upcall.portid = nla_get_u32(a);
800 break;
801
802 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
803 /* Get out tunnel info. */
804 struct vport *vport;
805
806 vport = ovs_vport_rcu(dp, nla_get_u32(a));
807 if (vport) {
808 int err;
809
810 err = dev_fill_metadata_dst(vport->dev, skb);
811 if (!err)
812 upcall.egress_tun_info = skb_tunnel_info(skb);
813 }
814
815 break;
816 }
817
818 case OVS_USERSPACE_ATTR_ACTIONS: {
819 /* Include actions. */
820 upcall.actions = actions;
821 upcall.actions_len = actions_len;
822 break;
823 }
824
825 } /* End of switch. */
826 }
827
828 err = ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
829 SKB_RESTORE_FILL_METADATA_DST(skb);
830 return err;
831 }
832
833 static int sample(struct datapath *dp, struct sk_buff *skb,
834 struct sw_flow_key *key, const struct nlattr *attr,
835 const struct nlattr *actions, int actions_len)
836 {
837 const struct nlattr *acts_list = NULL;
838 const struct nlattr *a;
839 int rem;
840 u32 cutlen = 0;
841
842 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
843 a = nla_next(a, &rem)) {
844 u32 probability;
845
846 switch (nla_type(a)) {
847 case OVS_SAMPLE_ATTR_PROBABILITY:
848 probability = nla_get_u32(a);
849 if (!probability || prandom_u32() > probability)
850 return 0;
851 break;
852
853 case OVS_SAMPLE_ATTR_ACTIONS:
854 acts_list = a;
855 break;
856 }
857 }
858
859 rem = nla_len(acts_list);
860 a = nla_data(acts_list);
861
862 /* Actions list is empty, do nothing */
863 if (unlikely(!rem))
864 return 0;
865
866 /* The only known usage of sample action is having a single user-space
867 * action, or having a truncate action followed by a single user-space
868 * action. Treat this usage as a special case.
869 * The output_userspace() should clone the skb to be sent to the
870 * user space. This skb will be consumed by its caller.
871 */
872 if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
873 struct ovs_action_trunc *trunc = nla_data(a);
874
875 if (skb->len > trunc->max_len)
876 cutlen = skb->len - trunc->max_len;
877
878 a = nla_next(a, &rem);
879 }
880
881 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
882 nla_is_last(a, rem)))
883 return output_userspace(dp, skb, key, a, actions,
884 actions_len, cutlen);
885
886 skb = skb_clone(skb, GFP_ATOMIC);
887 if (!skb)
888 /* Skip the sample action when out of memory. */
889 return 0;
890
891 if (!add_deferred_actions(skb, key, a)) {
892 if (net_ratelimit())
893 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
894 ovs_dp_name(dp));
895
896 kfree_skb(skb);
897 }
898 return 0;
899 }
900
901 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
902 const struct nlattr *attr)
903 {
904 struct ovs_action_hash *hash_act = nla_data(attr);
905 u32 hash = 0;
906
907 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
908 hash = skb_get_hash(skb);
909 hash = jhash_1word(hash, hash_act->hash_basis);
910 if (!hash)
911 hash = 0x1;
912
913 key->ovs_flow_hash = hash;
914 }
915
916 static int execute_set_action(struct sk_buff *skb,
917 struct sw_flow_key *flow_key,
918 const struct nlattr *a)
919 {
920 /* Only tunnel set execution is supported without a mask. */
921 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
922 struct ovs_tunnel_info *tun = nla_data(a);
923
924 ovs_skb_dst_drop(skb);
925 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
926 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
927 return 0;
928 }
929
930 return -EINVAL;
931 }
932
933 /* Mask is at the midpoint of the data. */
934 #define get_mask(a, type) ((const type)nla_data(a) + 1)
935
936 static int execute_masked_set_action(struct sk_buff *skb,
937 struct sw_flow_key *flow_key,
938 const struct nlattr *a)
939 {
940 int err = 0;
941
942 switch (nla_type(a)) {
943 case OVS_KEY_ATTR_PRIORITY:
944 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
945 *get_mask(a, u32 *));
946 flow_key->phy.priority = skb->priority;
947 break;
948
949 case OVS_KEY_ATTR_SKB_MARK:
950 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
951 flow_key->phy.skb_mark = skb->mark;
952 break;
953
954 case OVS_KEY_ATTR_TUNNEL_INFO:
955 /* Masked data not supported for tunnel. */
956 err = -EINVAL;
957 break;
958
959 case OVS_KEY_ATTR_ETHERNET:
960 err = set_eth_addr(skb, flow_key, nla_data(a),
961 get_mask(a, struct ovs_key_ethernet *));
962 break;
963
964 case OVS_KEY_ATTR_IPV4:
965 err = set_ipv4(skb, flow_key, nla_data(a),
966 get_mask(a, struct ovs_key_ipv4 *));
967 break;
968
969 case OVS_KEY_ATTR_IPV6:
970 err = set_ipv6(skb, flow_key, nla_data(a),
971 get_mask(a, struct ovs_key_ipv6 *));
972 break;
973
974 case OVS_KEY_ATTR_TCP:
975 err = set_tcp(skb, flow_key, nla_data(a),
976 get_mask(a, struct ovs_key_tcp *));
977 break;
978
979 case OVS_KEY_ATTR_UDP:
980 err = set_udp(skb, flow_key, nla_data(a),
981 get_mask(a, struct ovs_key_udp *));
982 break;
983
984 case OVS_KEY_ATTR_SCTP:
985 err = set_sctp(skb, flow_key, nla_data(a),
986 get_mask(a, struct ovs_key_sctp *));
987 break;
988
989 case OVS_KEY_ATTR_MPLS:
990 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
991 __be32 *));
992 break;
993
994 case OVS_KEY_ATTR_CT_STATE:
995 case OVS_KEY_ATTR_CT_ZONE:
996 case OVS_KEY_ATTR_CT_MARK:
997 case OVS_KEY_ATTR_CT_LABELS:
998 err = -EINVAL;
999 break;
1000 }
1001
1002 return err;
1003 }
1004
1005 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1006 struct sw_flow_key *key,
1007 const struct nlattr *a, int rem)
1008 {
1009 struct deferred_action *da;
1010
1011 if (!is_flow_key_valid(key)) {
1012 int err;
1013
1014 err = ovs_flow_key_update(skb, key);
1015 if (err)
1016 return err;
1017 }
1018 BUG_ON(!is_flow_key_valid(key));
1019
1020 if (!nla_is_last(a, rem)) {
1021 /* Recirc action is the not the last action
1022 * of the action list, need to clone the skb.
1023 */
1024 skb = skb_clone(skb, GFP_ATOMIC);
1025
1026 /* Skip the recirc action when out of memory, but
1027 * continue on with the rest of the action list.
1028 */
1029 if (!skb)
1030 return 0;
1031 }
1032
1033 da = add_deferred_actions(skb, key, NULL);
1034 if (da) {
1035 da->pkt_key.recirc_id = nla_get_u32(a);
1036 } else {
1037 kfree_skb(skb);
1038
1039 if (net_ratelimit())
1040 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1041 ovs_dp_name(dp));
1042 }
1043
1044 return 0;
1045 }
1046
1047 /* Execute a list of actions against 'skb'. */
1048 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1049 struct sw_flow_key *key,
1050 const struct nlattr *attr, int len)
1051 {
1052 /* Every output action needs a separate clone of 'skb', but the common
1053 * case is just a single output action, so that doing a clone and
1054 * then freeing the original skbuff is wasteful. So the following code
1055 * is slightly obscure just to avoid that.
1056 */
1057 int prev_port = -1;
1058 const struct nlattr *a;
1059 int rem;
1060
1061 for (a = attr, rem = len; rem > 0;
1062 a = nla_next(a, &rem)) {
1063 int err = 0;
1064
1065 if (unlikely(prev_port != -1)) {
1066 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1067
1068 if (out_skb)
1069 do_output(dp, out_skb, prev_port, key);
1070
1071 OVS_CB(skb)->cutlen = 0;
1072 prev_port = -1;
1073 }
1074
1075 switch (nla_type(a)) {
1076 case OVS_ACTION_ATTR_OUTPUT:
1077 prev_port = nla_get_u32(a);
1078 break;
1079
1080 case OVS_ACTION_ATTR_TRUNC: {
1081 struct ovs_action_trunc *trunc = nla_data(a);
1082
1083 if (skb->len > trunc->max_len)
1084 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1085 break;
1086 }
1087
1088 case OVS_ACTION_ATTR_USERSPACE:
1089 output_userspace(dp, skb, key, a, attr,
1090 len, OVS_CB(skb)->cutlen);
1091 OVS_CB(skb)->cutlen = 0;
1092 break;
1093
1094 case OVS_ACTION_ATTR_HASH:
1095 execute_hash(skb, key, a);
1096 break;
1097
1098 case OVS_ACTION_ATTR_PUSH_MPLS:
1099 err = push_mpls(skb, key, nla_data(a));
1100 break;
1101
1102 case OVS_ACTION_ATTR_POP_MPLS:
1103 err = pop_mpls(skb, key, nla_get_be16(a));
1104 break;
1105
1106 case OVS_ACTION_ATTR_PUSH_VLAN:
1107 err = push_vlan(skb, key, nla_data(a));
1108 break;
1109
1110 case OVS_ACTION_ATTR_POP_VLAN:
1111 err = pop_vlan(skb, key);
1112 break;
1113
1114 case OVS_ACTION_ATTR_RECIRC:
1115 err = execute_recirc(dp, skb, key, a, rem);
1116 if (nla_is_last(a, rem)) {
1117 /* If this is the last action, the skb has
1118 * been consumed or freed.
1119 * Return immediately.
1120 */
1121 return err;
1122 }
1123 break;
1124
1125 case OVS_ACTION_ATTR_SET:
1126 err = execute_set_action(skb, key, nla_data(a));
1127 break;
1128
1129 case OVS_ACTION_ATTR_SET_MASKED:
1130 case OVS_ACTION_ATTR_SET_TO_MASKED:
1131 err = execute_masked_set_action(skb, key, nla_data(a));
1132 break;
1133
1134 case OVS_ACTION_ATTR_SAMPLE:
1135 err = sample(dp, skb, key, a, attr, len);
1136 break;
1137
1138 case OVS_ACTION_ATTR_CT:
1139 if (!is_flow_key_valid(key)) {
1140 err = ovs_flow_key_update(skb, key);
1141 if (err)
1142 return err;
1143 }
1144
1145 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1146 nla_data(a));
1147
1148 /* Hide stolen IP fragments from user space. */
1149 if (err)
1150 return err == -EINPROGRESS ? 0 : err;
1151 break;
1152 }
1153
1154 if (unlikely(err)) {
1155 kfree_skb(skb);
1156 return err;
1157 }
1158 }
1159
1160 if (prev_port != -1)
1161 do_output(dp, skb, prev_port, key);
1162 else
1163 consume_skb(skb);
1164
1165 return 0;
1166 }
1167
1168 static void process_deferred_actions(struct datapath *dp)
1169 {
1170 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1171
1172 /* Do not touch the FIFO in case there is no deferred actions. */
1173 if (action_fifo_is_empty(fifo))
1174 return;
1175
1176 /* Finishing executing all deferred actions. */
1177 do {
1178 struct deferred_action *da = action_fifo_get(fifo);
1179 struct sk_buff *skb = da->skb;
1180 struct sw_flow_key *key = &da->pkt_key;
1181 const struct nlattr *actions = da->actions;
1182
1183 if (actions)
1184 do_execute_actions(dp, skb, key, actions,
1185 nla_len(actions));
1186 else
1187 ovs_dp_process_packet(skb, key);
1188 } while (!action_fifo_is_empty(fifo));
1189
1190 /* Reset FIFO for the next packet. */
1191 action_fifo_init(fifo);
1192 }
1193
1194 /* Execute a list of actions against 'skb'. */
1195 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1196 const struct sw_flow_actions *acts,
1197 struct sw_flow_key *key)
1198 {
1199 int level = this_cpu_read(exec_actions_level);
1200 int err;
1201
1202 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
1203 if (net_ratelimit())
1204 pr_warn("%s: packet loop detected, dropping.\n",
1205 ovs_dp_name(dp));
1206
1207 kfree_skb(skb);
1208 return -ELOOP;
1209 }
1210
1211 this_cpu_inc(exec_actions_level);
1212 err = do_execute_actions(dp, skb, key,
1213 acts->actions, acts->actions_len);
1214
1215 if (!level)
1216 process_deferred_actions(dp);
1217
1218 this_cpu_dec(exec_actions_level);
1219
1220 /* This return status currently does not reflect the errors
1221 * encounted during deferred actions execution. Probably needs to
1222 * be fixed in the future.
1223 */
1224 return err;
1225 }
1226
1227 int action_fifos_init(void)
1228 {
1229 action_fifos = alloc_percpu(struct action_fifo);
1230 if (!action_fifos)
1231 return -ENOMEM;
1232
1233 return 0;
1234 }
1235
1236 void action_fifos_exit(void)
1237 {
1238 free_percpu(action_fifos);
1239 }