]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/openvswitch/flow.c
openvswitch: fix flow stats accounting when node 0 is not possible
[mirror_ubuntu-hirsute-kernel.git] / net / openvswitch / flow.c
1 /*
2 * Copyright (c) 2007-2014 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 #include <linux/uaccess.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_vlan.h>
24 #include <net/llc_pdu.h>
25 #include <linux/kernel.h>
26 #include <linux/jhash.h>
27 #include <linux/jiffies.h>
28 #include <linux/llc.h>
29 #include <linux/module.h>
30 #include <linux/in.h>
31 #include <linux/rcupdate.h>
32 #include <linux/if_arp.h>
33 #include <linux/ip.h>
34 #include <linux/ipv6.h>
35 #include <linux/mpls.h>
36 #include <linux/sctp.h>
37 #include <linux/smp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/ip.h>
44 #include <net/ip_tunnels.h>
45 #include <net/ipv6.h>
46 #include <net/mpls.h>
47 #include <net/ndisc.h>
48
49 #include "conntrack.h"
50 #include "datapath.h"
51 #include "flow.h"
52 #include "flow_netlink.h"
53 #include "vport.h"
54
55 u64 ovs_flow_used_time(unsigned long flow_jiffies)
56 {
57 struct timespec cur_ts;
58 u64 cur_ms, idle_ms;
59
60 ktime_get_ts(&cur_ts);
61 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
62 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
63 cur_ts.tv_nsec / NSEC_PER_MSEC;
64
65 return cur_ms - idle_ms;
66 }
67
68 #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
69
70 void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
71 const struct sk_buff *skb)
72 {
73 struct flow_stats *stats;
74 int node = numa_node_id();
75 int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
76
77 stats = rcu_dereference(flow->stats[node]);
78
79 /* Check if already have node-specific stats. */
80 if (likely(stats)) {
81 spin_lock(&stats->lock);
82 /* Mark if we write on the pre-allocated stats. */
83 if (node == 0 && unlikely(flow->stats_last_writer != node))
84 flow->stats_last_writer = node;
85 } else {
86 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
87 spin_lock(&stats->lock);
88
89 /* If the current NUMA-node is the only writer on the
90 * pre-allocated stats keep using them.
91 */
92 if (unlikely(flow->stats_last_writer != node)) {
93 /* A previous locker may have already allocated the
94 * stats, so we need to check again. If node-specific
95 * stats were already allocated, we update the pre-
96 * allocated stats as we have already locked them.
97 */
98 if (likely(flow->stats_last_writer != NUMA_NO_NODE)
99 && likely(!rcu_access_pointer(flow->stats[node]))) {
100 /* Try to allocate node-specific stats. */
101 struct flow_stats *new_stats;
102
103 new_stats =
104 kmem_cache_alloc_node(flow_stats_cache,
105 GFP_NOWAIT |
106 __GFP_THISNODE |
107 __GFP_NOWARN |
108 __GFP_NOMEMALLOC,
109 node);
110 if (likely(new_stats)) {
111 new_stats->used = jiffies;
112 new_stats->packet_count = 1;
113 new_stats->byte_count = len;
114 new_stats->tcp_flags = tcp_flags;
115 spin_lock_init(&new_stats->lock);
116
117 rcu_assign_pointer(flow->stats[node],
118 new_stats);
119 goto unlock;
120 }
121 }
122 flow->stats_last_writer = node;
123 }
124 }
125
126 stats->used = jiffies;
127 stats->packet_count++;
128 stats->byte_count += len;
129 stats->tcp_flags |= tcp_flags;
130 unlock:
131 spin_unlock(&stats->lock);
132 }
133
134 /* Must be called with rcu_read_lock or ovs_mutex. */
135 void ovs_flow_stats_get(const struct sw_flow *flow,
136 struct ovs_flow_stats *ovs_stats,
137 unsigned long *used, __be16 *tcp_flags)
138 {
139 int node;
140
141 *used = 0;
142 *tcp_flags = 0;
143 memset(ovs_stats, 0, sizeof(*ovs_stats));
144
145 /* We open code this to make sure node 0 is always considered */
146 for (node = 0; node < MAX_NUMNODES; node = next_node(node, node_possible_map)) {
147 struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
148
149 if (stats) {
150 /* Local CPU may write on non-local stats, so we must
151 * block bottom-halves here.
152 */
153 spin_lock_bh(&stats->lock);
154 if (!*used || time_after(stats->used, *used))
155 *used = stats->used;
156 *tcp_flags |= stats->tcp_flags;
157 ovs_stats->n_packets += stats->packet_count;
158 ovs_stats->n_bytes += stats->byte_count;
159 spin_unlock_bh(&stats->lock);
160 }
161 }
162 }
163
164 /* Called with ovs_mutex. */
165 void ovs_flow_stats_clear(struct sw_flow *flow)
166 {
167 int node;
168
169 /* We open code this to make sure node 0 is always considered */
170 for (node = 0; node < MAX_NUMNODES; node = next_node(node, node_possible_map)) {
171 struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
172
173 if (stats) {
174 spin_lock_bh(&stats->lock);
175 stats->used = 0;
176 stats->packet_count = 0;
177 stats->byte_count = 0;
178 stats->tcp_flags = 0;
179 spin_unlock_bh(&stats->lock);
180 }
181 }
182 }
183
184 static int check_header(struct sk_buff *skb, int len)
185 {
186 if (unlikely(skb->len < len))
187 return -EINVAL;
188 if (unlikely(!pskb_may_pull(skb, len)))
189 return -ENOMEM;
190 return 0;
191 }
192
193 static bool arphdr_ok(struct sk_buff *skb)
194 {
195 return pskb_may_pull(skb, skb_network_offset(skb) +
196 sizeof(struct arp_eth_header));
197 }
198
199 static int check_iphdr(struct sk_buff *skb)
200 {
201 unsigned int nh_ofs = skb_network_offset(skb);
202 unsigned int ip_len;
203 int err;
204
205 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
206 if (unlikely(err))
207 return err;
208
209 ip_len = ip_hdrlen(skb);
210 if (unlikely(ip_len < sizeof(struct iphdr) ||
211 skb->len < nh_ofs + ip_len))
212 return -EINVAL;
213
214 skb_set_transport_header(skb, nh_ofs + ip_len);
215 return 0;
216 }
217
218 static bool tcphdr_ok(struct sk_buff *skb)
219 {
220 int th_ofs = skb_transport_offset(skb);
221 int tcp_len;
222
223 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
224 return false;
225
226 tcp_len = tcp_hdrlen(skb);
227 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
228 skb->len < th_ofs + tcp_len))
229 return false;
230
231 return true;
232 }
233
234 static bool udphdr_ok(struct sk_buff *skb)
235 {
236 return pskb_may_pull(skb, skb_transport_offset(skb) +
237 sizeof(struct udphdr));
238 }
239
240 static bool sctphdr_ok(struct sk_buff *skb)
241 {
242 return pskb_may_pull(skb, skb_transport_offset(skb) +
243 sizeof(struct sctphdr));
244 }
245
246 static bool icmphdr_ok(struct sk_buff *skb)
247 {
248 return pskb_may_pull(skb, skb_transport_offset(skb) +
249 sizeof(struct icmphdr));
250 }
251
252 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
253 {
254 unsigned int nh_ofs = skb_network_offset(skb);
255 unsigned int nh_len;
256 int payload_ofs;
257 struct ipv6hdr *nh;
258 uint8_t nexthdr;
259 __be16 frag_off;
260 int err;
261
262 err = check_header(skb, nh_ofs + sizeof(*nh));
263 if (unlikely(err))
264 return err;
265
266 nh = ipv6_hdr(skb);
267 nexthdr = nh->nexthdr;
268 payload_ofs = (u8 *)(nh + 1) - skb->data;
269
270 key->ip.proto = NEXTHDR_NONE;
271 key->ip.tos = ipv6_get_dsfield(nh);
272 key->ip.ttl = nh->hop_limit;
273 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
274 key->ipv6.addr.src = nh->saddr;
275 key->ipv6.addr.dst = nh->daddr;
276
277 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
278
279 if (frag_off) {
280 if (frag_off & htons(~0x7))
281 key->ip.frag = OVS_FRAG_TYPE_LATER;
282 else
283 key->ip.frag = OVS_FRAG_TYPE_FIRST;
284 } else {
285 key->ip.frag = OVS_FRAG_TYPE_NONE;
286 }
287
288 /* Delayed handling of error in ipv6_skip_exthdr() as it
289 * always sets frag_off to a valid value which may be
290 * used to set key->ip.frag above.
291 */
292 if (unlikely(payload_ofs < 0))
293 return -EPROTO;
294
295 nh_len = payload_ofs - nh_ofs;
296 skb_set_transport_header(skb, nh_ofs + nh_len);
297 key->ip.proto = nexthdr;
298 return nh_len;
299 }
300
301 static bool icmp6hdr_ok(struct sk_buff *skb)
302 {
303 return pskb_may_pull(skb, skb_transport_offset(skb) +
304 sizeof(struct icmp6hdr));
305 }
306
307 /**
308 * Parse vlan tag from vlan header.
309 * Returns ERROR on memory error.
310 * Returns 0 if it encounters a non-vlan or incomplete packet.
311 * Returns 1 after successfully parsing vlan tag.
312 */
313 static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh)
314 {
315 struct vlan_head *vh = (struct vlan_head *)skb->data;
316
317 if (likely(!eth_type_vlan(vh->tpid)))
318 return 0;
319
320 if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))
321 return 0;
322
323 if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +
324 sizeof(__be16))))
325 return -ENOMEM;
326
327 vh = (struct vlan_head *)skb->data;
328 key_vh->tci = vh->tci | htons(VLAN_TAG_PRESENT);
329 key_vh->tpid = vh->tpid;
330
331 __skb_pull(skb, sizeof(struct vlan_head));
332 return 1;
333 }
334
335 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
336 {
337 int res;
338
339 key->eth.vlan.tci = 0;
340 key->eth.vlan.tpid = 0;
341 key->eth.cvlan.tci = 0;
342 key->eth.cvlan.tpid = 0;
343
344 if (likely(skb_vlan_tag_present(skb))) {
345 key->eth.vlan.tci = htons(skb->vlan_tci);
346 key->eth.vlan.tpid = skb->vlan_proto;
347 } else {
348 /* Parse outer vlan tag in the non-accelerated case. */
349 res = parse_vlan_tag(skb, &key->eth.vlan);
350 if (res <= 0)
351 return res;
352 }
353
354 /* Parse inner vlan tag. */
355 res = parse_vlan_tag(skb, &key->eth.cvlan);
356 if (res <= 0)
357 return res;
358
359 return 0;
360 }
361
362 static __be16 parse_ethertype(struct sk_buff *skb)
363 {
364 struct llc_snap_hdr {
365 u8 dsap; /* Always 0xAA */
366 u8 ssap; /* Always 0xAA */
367 u8 ctrl;
368 u8 oui[3];
369 __be16 ethertype;
370 };
371 struct llc_snap_hdr *llc;
372 __be16 proto;
373
374 proto = *(__be16 *) skb->data;
375 __skb_pull(skb, sizeof(__be16));
376
377 if (eth_proto_is_802_3(proto))
378 return proto;
379
380 if (skb->len < sizeof(struct llc_snap_hdr))
381 return htons(ETH_P_802_2);
382
383 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
384 return htons(0);
385
386 llc = (struct llc_snap_hdr *) skb->data;
387 if (llc->dsap != LLC_SAP_SNAP ||
388 llc->ssap != LLC_SAP_SNAP ||
389 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
390 return htons(ETH_P_802_2);
391
392 __skb_pull(skb, sizeof(struct llc_snap_hdr));
393
394 if (eth_proto_is_802_3(llc->ethertype))
395 return llc->ethertype;
396
397 return htons(ETH_P_802_2);
398 }
399
400 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
401 int nh_len)
402 {
403 struct icmp6hdr *icmp = icmp6_hdr(skb);
404
405 /* The ICMPv6 type and code fields use the 16-bit transport port
406 * fields, so we need to store them in 16-bit network byte order.
407 */
408 key->tp.src = htons(icmp->icmp6_type);
409 key->tp.dst = htons(icmp->icmp6_code);
410 memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
411
412 if (icmp->icmp6_code == 0 &&
413 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
414 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
415 int icmp_len = skb->len - skb_transport_offset(skb);
416 struct nd_msg *nd;
417 int offset;
418
419 /* In order to process neighbor discovery options, we need the
420 * entire packet.
421 */
422 if (unlikely(icmp_len < sizeof(*nd)))
423 return 0;
424
425 if (unlikely(skb_linearize(skb)))
426 return -ENOMEM;
427
428 nd = (struct nd_msg *)skb_transport_header(skb);
429 key->ipv6.nd.target = nd->target;
430
431 icmp_len -= sizeof(*nd);
432 offset = 0;
433 while (icmp_len >= 8) {
434 struct nd_opt_hdr *nd_opt =
435 (struct nd_opt_hdr *)(nd->opt + offset);
436 int opt_len = nd_opt->nd_opt_len * 8;
437
438 if (unlikely(!opt_len || opt_len > icmp_len))
439 return 0;
440
441 /* Store the link layer address if the appropriate
442 * option is provided. It is considered an error if
443 * the same link layer option is specified twice.
444 */
445 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
446 && opt_len == 8) {
447 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
448 goto invalid;
449 ether_addr_copy(key->ipv6.nd.sll,
450 &nd->opt[offset+sizeof(*nd_opt)]);
451 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
452 && opt_len == 8) {
453 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
454 goto invalid;
455 ether_addr_copy(key->ipv6.nd.tll,
456 &nd->opt[offset+sizeof(*nd_opt)]);
457 }
458
459 icmp_len -= opt_len;
460 offset += opt_len;
461 }
462 }
463
464 return 0;
465
466 invalid:
467 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
468 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
469 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
470
471 return 0;
472 }
473
474 /**
475 * key_extract - extracts a flow key from an Ethernet frame.
476 * @skb: sk_buff that contains the frame, with skb->data pointing to the
477 * Ethernet header
478 * @key: output flow key
479 *
480 * The caller must ensure that skb->len >= ETH_HLEN.
481 *
482 * Returns 0 if successful, otherwise a negative errno value.
483 *
484 * Initializes @skb header pointers as follows:
485 *
486 * - skb->mac_header: the Ethernet header.
487 *
488 * - skb->network_header: just past the Ethernet header, or just past the
489 * VLAN header, to the first byte of the Ethernet payload.
490 *
491 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
492 * on output, then just past the IP header, if one is present and
493 * of a correct length, otherwise the same as skb->network_header.
494 * For other key->eth.type values it is left untouched.
495 */
496 static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
497 {
498 int error;
499 struct ethhdr *eth;
500
501 /* Flags are always used as part of stats */
502 key->tp.flags = 0;
503
504 skb_reset_mac_header(skb);
505
506 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
507 * header in the linear data area.
508 */
509 eth = eth_hdr(skb);
510 ether_addr_copy(key->eth.src, eth->h_source);
511 ether_addr_copy(key->eth.dst, eth->h_dest);
512
513 __skb_pull(skb, 2 * ETH_ALEN);
514 /* We are going to push all headers that we pull, so no need to
515 * update skb->csum here.
516 */
517
518 if (unlikely(parse_vlan(skb, key)))
519 return -ENOMEM;
520
521 key->eth.type = parse_ethertype(skb);
522 if (unlikely(key->eth.type == htons(0)))
523 return -ENOMEM;
524
525 skb_reset_network_header(skb);
526 skb_reset_mac_len(skb);
527 __skb_push(skb, skb->data - skb_mac_header(skb));
528
529 /* Network layer. */
530 if (key->eth.type == htons(ETH_P_IP)) {
531 struct iphdr *nh;
532 __be16 offset;
533
534 error = check_iphdr(skb);
535 if (unlikely(error)) {
536 memset(&key->ip, 0, sizeof(key->ip));
537 memset(&key->ipv4, 0, sizeof(key->ipv4));
538 if (error == -EINVAL) {
539 skb->transport_header = skb->network_header;
540 error = 0;
541 }
542 return error;
543 }
544
545 nh = ip_hdr(skb);
546 key->ipv4.addr.src = nh->saddr;
547 key->ipv4.addr.dst = nh->daddr;
548
549 key->ip.proto = nh->protocol;
550 key->ip.tos = nh->tos;
551 key->ip.ttl = nh->ttl;
552
553 offset = nh->frag_off & htons(IP_OFFSET);
554 if (offset) {
555 key->ip.frag = OVS_FRAG_TYPE_LATER;
556 return 0;
557 }
558 if (nh->frag_off & htons(IP_MF) ||
559 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
560 key->ip.frag = OVS_FRAG_TYPE_FIRST;
561 else
562 key->ip.frag = OVS_FRAG_TYPE_NONE;
563
564 /* Transport layer. */
565 if (key->ip.proto == IPPROTO_TCP) {
566 if (tcphdr_ok(skb)) {
567 struct tcphdr *tcp = tcp_hdr(skb);
568 key->tp.src = tcp->source;
569 key->tp.dst = tcp->dest;
570 key->tp.flags = TCP_FLAGS_BE16(tcp);
571 } else {
572 memset(&key->tp, 0, sizeof(key->tp));
573 }
574
575 } else if (key->ip.proto == IPPROTO_UDP) {
576 if (udphdr_ok(skb)) {
577 struct udphdr *udp = udp_hdr(skb);
578 key->tp.src = udp->source;
579 key->tp.dst = udp->dest;
580 } else {
581 memset(&key->tp, 0, sizeof(key->tp));
582 }
583 } else if (key->ip.proto == IPPROTO_SCTP) {
584 if (sctphdr_ok(skb)) {
585 struct sctphdr *sctp = sctp_hdr(skb);
586 key->tp.src = sctp->source;
587 key->tp.dst = sctp->dest;
588 } else {
589 memset(&key->tp, 0, sizeof(key->tp));
590 }
591 } else if (key->ip.proto == IPPROTO_ICMP) {
592 if (icmphdr_ok(skb)) {
593 struct icmphdr *icmp = icmp_hdr(skb);
594 /* The ICMP type and code fields use the 16-bit
595 * transport port fields, so we need to store
596 * them in 16-bit network byte order. */
597 key->tp.src = htons(icmp->type);
598 key->tp.dst = htons(icmp->code);
599 } else {
600 memset(&key->tp, 0, sizeof(key->tp));
601 }
602 }
603
604 } else if (key->eth.type == htons(ETH_P_ARP) ||
605 key->eth.type == htons(ETH_P_RARP)) {
606 struct arp_eth_header *arp;
607 bool arp_available = arphdr_ok(skb);
608
609 arp = (struct arp_eth_header *)skb_network_header(skb);
610
611 if (arp_available &&
612 arp->ar_hrd == htons(ARPHRD_ETHER) &&
613 arp->ar_pro == htons(ETH_P_IP) &&
614 arp->ar_hln == ETH_ALEN &&
615 arp->ar_pln == 4) {
616
617 /* We only match on the lower 8 bits of the opcode. */
618 if (ntohs(arp->ar_op) <= 0xff)
619 key->ip.proto = ntohs(arp->ar_op);
620 else
621 key->ip.proto = 0;
622
623 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
624 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
625 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
626 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
627 } else {
628 memset(&key->ip, 0, sizeof(key->ip));
629 memset(&key->ipv4, 0, sizeof(key->ipv4));
630 }
631 } else if (eth_p_mpls(key->eth.type)) {
632 size_t stack_len = MPLS_HLEN;
633
634 /* In the presence of an MPLS label stack the end of the L2
635 * header and the beginning of the L3 header differ.
636 *
637 * Advance network_header to the beginning of the L3
638 * header. mac_len corresponds to the end of the L2 header.
639 */
640 while (1) {
641 __be32 lse;
642
643 error = check_header(skb, skb->mac_len + stack_len);
644 if (unlikely(error))
645 return 0;
646
647 memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
648
649 if (stack_len == MPLS_HLEN)
650 memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
651
652 skb_set_network_header(skb, skb->mac_len + stack_len);
653 if (lse & htonl(MPLS_LS_S_MASK))
654 break;
655
656 stack_len += MPLS_HLEN;
657 }
658 } else if (key->eth.type == htons(ETH_P_IPV6)) {
659 int nh_len; /* IPv6 Header + Extensions */
660
661 nh_len = parse_ipv6hdr(skb, key);
662 if (unlikely(nh_len < 0)) {
663 switch (nh_len) {
664 case -EINVAL:
665 memset(&key->ip, 0, sizeof(key->ip));
666 memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
667 /* fall-through */
668 case -EPROTO:
669 skb->transport_header = skb->network_header;
670 error = 0;
671 break;
672 default:
673 error = nh_len;
674 }
675 return error;
676 }
677
678 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
679 return 0;
680 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
681 key->ip.frag = OVS_FRAG_TYPE_FIRST;
682
683 /* Transport layer. */
684 if (key->ip.proto == NEXTHDR_TCP) {
685 if (tcphdr_ok(skb)) {
686 struct tcphdr *tcp = tcp_hdr(skb);
687 key->tp.src = tcp->source;
688 key->tp.dst = tcp->dest;
689 key->tp.flags = TCP_FLAGS_BE16(tcp);
690 } else {
691 memset(&key->tp, 0, sizeof(key->tp));
692 }
693 } else if (key->ip.proto == NEXTHDR_UDP) {
694 if (udphdr_ok(skb)) {
695 struct udphdr *udp = udp_hdr(skb);
696 key->tp.src = udp->source;
697 key->tp.dst = udp->dest;
698 } else {
699 memset(&key->tp, 0, sizeof(key->tp));
700 }
701 } else if (key->ip.proto == NEXTHDR_SCTP) {
702 if (sctphdr_ok(skb)) {
703 struct sctphdr *sctp = sctp_hdr(skb);
704 key->tp.src = sctp->source;
705 key->tp.dst = sctp->dest;
706 } else {
707 memset(&key->tp, 0, sizeof(key->tp));
708 }
709 } else if (key->ip.proto == NEXTHDR_ICMP) {
710 if (icmp6hdr_ok(skb)) {
711 error = parse_icmpv6(skb, key, nh_len);
712 if (error)
713 return error;
714 } else {
715 memset(&key->tp, 0, sizeof(key->tp));
716 }
717 }
718 }
719 return 0;
720 }
721
722 int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
723 {
724 return key_extract(skb, key);
725 }
726
727 int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
728 struct sk_buff *skb, struct sw_flow_key *key)
729 {
730 /* Extract metadata from packet. */
731 if (tun_info) {
732 key->tun_proto = ip_tunnel_info_af(tun_info);
733 memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
734
735 if (tun_info->options_len) {
736 BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
737 8)) - 1
738 > sizeof(key->tun_opts));
739
740 ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
741 tun_info);
742 key->tun_opts_len = tun_info->options_len;
743 } else {
744 key->tun_opts_len = 0;
745 }
746 } else {
747 key->tun_proto = 0;
748 key->tun_opts_len = 0;
749 memset(&key->tun_key, 0, sizeof(key->tun_key));
750 }
751
752 key->phy.priority = skb->priority;
753 key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
754 key->phy.skb_mark = skb->mark;
755 ovs_ct_fill_key(skb, key);
756 key->ovs_flow_hash = 0;
757 key->recirc_id = 0;
758
759 return key_extract(skb, key);
760 }
761
762 int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
763 struct sk_buff *skb,
764 struct sw_flow_key *key, bool log)
765 {
766 int err;
767
768 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
769
770 /* Extract metadata from netlink attributes. */
771 err = ovs_nla_get_flow_metadata(net, attr, key, log);
772 if (err)
773 return err;
774
775 return key_extract(skb, key);
776 }