2 * Copyright (c) 2007-2014 Nicira, Inc.
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.
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.
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
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>
31 #include <linux/rcupdate.h>
32 #include <linux/if_arp.h>
34 #include <linux/ipv6.h>
35 #include <linux/sctp.h>
36 #include <linux/smp.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
44 #include <net/ndisc.h>
48 #include "flow_netlink.h"
53 u64 ovs_flow_used_time(unsigned long flow_jiffies)
55 struct timespec cur_ts;
58 ktime_get_ts(&cur_ts);
59 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
60 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
61 cur_ts.tv_nsec / NSEC_PER_MSEC;
63 return cur_ms - idle_ms;
66 #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
68 void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
71 struct flow_stats *stats;
72 int node = numa_node_id();
74 stats = rcu_dereference(flow->stats[node]);
76 /* Check if already have node-specific stats. */
78 spin_lock(&stats->lock);
79 /* Mark if we write on the pre-allocated stats. */
80 if (node == 0 && unlikely(flow->stats_last_writer != node))
81 flow->stats_last_writer = node;
83 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
84 spin_lock(&stats->lock);
86 /* If the current NUMA-node is the only writer on the
87 * pre-allocated stats keep using them.
89 if (unlikely(flow->stats_last_writer != node)) {
90 /* A previous locker may have already allocated the
91 * stats, so we need to check again. If node-specific
92 * stats were already allocated, we update the pre-
93 * allocated stats as we have already locked them.
95 if (likely(flow->stats_last_writer != NUMA_NO_NODE)
96 && likely(!rcu_access_pointer(flow->stats[node]))) {
97 /* Try to allocate node-specific stats. */
98 struct flow_stats *new_stats;
101 kmem_cache_alloc_node(flow_stats_cache,
105 if (likely(new_stats)) {
106 new_stats->used = jiffies;
107 new_stats->packet_count = 1;
108 new_stats->byte_count = skb->len;
109 new_stats->tcp_flags = tcp_flags;
110 spin_lock_init(&new_stats->lock);
112 rcu_assign_pointer(flow->stats[node],
117 flow->stats_last_writer = node;
121 stats->used = jiffies;
122 stats->packet_count++;
123 stats->byte_count += skb->len;
124 stats->tcp_flags |= tcp_flags;
126 spin_unlock(&stats->lock);
129 /* Must be called with rcu_read_lock or ovs_mutex. */
130 void ovs_flow_stats_get(const struct sw_flow *flow,
131 struct ovs_flow_stats *ovs_stats,
132 unsigned long *used, __be16 *tcp_flags)
138 memset(ovs_stats, 0, sizeof(*ovs_stats));
140 for_each_node(node) {
141 struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
144 /* Local CPU may write on non-local stats, so we must
145 * block bottom-halves here.
147 spin_lock_bh(&stats->lock);
148 if (!*used || time_after(stats->used, *used))
150 *tcp_flags |= stats->tcp_flags;
151 ovs_stats->n_packets += stats->packet_count;
152 ovs_stats->n_bytes += stats->byte_count;
153 spin_unlock_bh(&stats->lock);
158 /* Called with ovs_mutex. */
159 void ovs_flow_stats_clear(struct sw_flow *flow)
163 for_each_node(node) {
164 struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
167 spin_lock_bh(&stats->lock);
169 stats->packet_count = 0;
170 stats->byte_count = 0;
171 stats->tcp_flags = 0;
172 spin_unlock_bh(&stats->lock);
177 static int check_header(struct sk_buff *skb, int len)
179 if (unlikely(skb->len < len))
181 if (unlikely(!pskb_may_pull(skb, len)))
186 static bool arphdr_ok(struct sk_buff *skb)
188 return pskb_may_pull(skb, skb_network_offset(skb) +
189 sizeof(struct arp_eth_header));
192 static int check_iphdr(struct sk_buff *skb)
194 unsigned int nh_ofs = skb_network_offset(skb);
198 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
202 ip_len = ip_hdrlen(skb);
203 if (unlikely(ip_len < sizeof(struct iphdr) ||
204 skb->len < nh_ofs + ip_len))
207 skb_set_transport_header(skb, nh_ofs + ip_len);
211 static bool tcphdr_ok(struct sk_buff *skb)
213 int th_ofs = skb_transport_offset(skb);
216 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
219 tcp_len = tcp_hdrlen(skb);
220 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
221 skb->len < th_ofs + tcp_len))
227 static bool udphdr_ok(struct sk_buff *skb)
229 return pskb_may_pull(skb, skb_transport_offset(skb) +
230 sizeof(struct udphdr));
233 static bool sctphdr_ok(struct sk_buff *skb)
235 return pskb_may_pull(skb, skb_transport_offset(skb) +
236 sizeof(struct sctphdr));
239 static bool icmphdr_ok(struct sk_buff *skb)
241 return pskb_may_pull(skb, skb_transport_offset(skb) +
242 sizeof(struct icmphdr));
245 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
247 unsigned int nh_ofs = skb_network_offset(skb);
255 err = check_header(skb, nh_ofs + sizeof(*nh));
260 nexthdr = nh->nexthdr;
261 payload_ofs = (u8 *)(nh + 1) - skb->data;
263 key->ip.proto = NEXTHDR_NONE;
264 key->ip.tos = ipv6_get_dsfield(nh);
265 key->ip.ttl = nh->hop_limit;
266 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
267 key->ipv6.addr.src = nh->saddr;
268 key->ipv6.addr.dst = nh->daddr;
270 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
271 if (unlikely(payload_ofs < 0))
275 if (frag_off & htons(~0x7))
276 key->ip.frag = OVS_FRAG_TYPE_LATER;
278 key->ip.frag = OVS_FRAG_TYPE_FIRST;
280 key->ip.frag = OVS_FRAG_TYPE_NONE;
283 nh_len = payload_ofs - nh_ofs;
284 skb_set_transport_header(skb, nh_ofs + nh_len);
285 key->ip.proto = nexthdr;
289 static bool icmp6hdr_ok(struct sk_buff *skb)
291 return pskb_may_pull(skb, skb_transport_offset(skb) +
292 sizeof(struct icmp6hdr));
295 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
298 __be16 eth_type; /* ETH_P_8021Q */
301 struct qtag_prefix *qp;
303 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
306 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
310 qp = (struct qtag_prefix *) skb->data;
311 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
312 __skb_pull(skb, sizeof(struct qtag_prefix));
317 static __be16 parse_ethertype(struct sk_buff *skb)
319 struct llc_snap_hdr {
320 u8 dsap; /* Always 0xAA */
321 u8 ssap; /* Always 0xAA */
326 struct llc_snap_hdr *llc;
329 proto = *(__be16 *) skb->data;
330 __skb_pull(skb, sizeof(__be16));
332 if (ntohs(proto) >= ETH_P_802_3_MIN)
335 if (skb->len < sizeof(struct llc_snap_hdr))
336 return htons(ETH_P_802_2);
338 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
341 llc = (struct llc_snap_hdr *) skb->data;
342 if (llc->dsap != LLC_SAP_SNAP ||
343 llc->ssap != LLC_SAP_SNAP ||
344 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
345 return htons(ETH_P_802_2);
347 __skb_pull(skb, sizeof(struct llc_snap_hdr));
349 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
350 return llc->ethertype;
352 return htons(ETH_P_802_2);
355 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
358 struct icmp6hdr *icmp = icmp6_hdr(skb);
360 /* The ICMPv6 type and code fields use the 16-bit transport port
361 * fields, so we need to store them in 16-bit network byte order.
363 key->tp.src = htons(icmp->icmp6_type);
364 key->tp.dst = htons(icmp->icmp6_code);
365 memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
367 if (icmp->icmp6_code == 0 &&
368 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
369 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
370 int icmp_len = skb->len - skb_transport_offset(skb);
374 /* In order to process neighbor discovery options, we need the
377 if (unlikely(icmp_len < sizeof(*nd)))
380 if (unlikely(skb_linearize(skb)))
383 nd = (struct nd_msg *)skb_transport_header(skb);
384 key->ipv6.nd.target = nd->target;
386 icmp_len -= sizeof(*nd);
388 while (icmp_len >= 8) {
389 struct nd_opt_hdr *nd_opt =
390 (struct nd_opt_hdr *)(nd->opt + offset);
391 int opt_len = nd_opt->nd_opt_len * 8;
393 if (unlikely(!opt_len || opt_len > icmp_len))
396 /* Store the link layer address if the appropriate
397 * option is provided. It is considered an error if
398 * the same link layer option is specified twice.
400 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
402 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
404 ether_addr_copy(key->ipv6.nd.sll,
405 &nd->opt[offset+sizeof(*nd_opt)]);
406 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
408 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
410 ether_addr_copy(key->ipv6.nd.tll,
411 &nd->opt[offset+sizeof(*nd_opt)]);
422 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
423 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
424 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
430 * key_extract - extracts a flow key from an Ethernet frame.
431 * @skb: sk_buff that contains the frame, with skb->data pointing to the
433 * @key: output flow key
435 * The caller must ensure that skb->len >= ETH_HLEN.
437 * Returns 0 if successful, otherwise a negative errno value.
439 * Initializes @skb header pointers as follows:
441 * - skb->mac_header: the Ethernet header.
443 * - skb->network_header: just past the Ethernet header, or just past the
444 * VLAN header, to the first byte of the Ethernet payload.
446 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
447 * on output, then just past the IP header, if one is present and
448 * of a correct length, otherwise the same as skb->network_header.
449 * For other key->eth.type values it is left untouched.
451 static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
456 /* Flags are always used as part of stats. */
459 skb_reset_mac_header(skb);
461 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
462 * header in the linear data area.
465 ether_addr_copy(key->eth.src, eth->h_source);
466 ether_addr_copy(key->eth.dst, eth->h_dest);
468 __skb_pull(skb, 2 * ETH_ALEN);
469 /* We are going to push all headers that we pull, so no need to
470 * update skb->csum here. */
473 if (vlan_tx_tag_present(skb))
474 key->eth.tci = htons(vlan_get_tci(skb));
475 else if (eth->h_proto == htons(ETH_P_8021Q))
476 if (unlikely(parse_vlan(skb, key)))
479 key->eth.type = parse_ethertype(skb);
480 if (unlikely(key->eth.type == htons(0)))
483 skb_reset_network_header(skb);
484 skb_reset_mac_len(skb);
485 __skb_push(skb, skb->data - skb_mac_header(skb));
488 if (key->eth.type == htons(ETH_P_IP)) {
492 error = check_iphdr(skb);
493 if (unlikely(error)) {
494 memset(&key->ip, 0, sizeof(key->ip));
495 memset(&key->ipv4, 0, sizeof(key->ipv4));
496 if (error == -EINVAL) {
497 skb->transport_header = skb->network_header;
504 key->ipv4.addr.src = nh->saddr;
505 key->ipv4.addr.dst = nh->daddr;
507 key->ip.proto = nh->protocol;
508 key->ip.tos = nh->tos;
509 key->ip.ttl = nh->ttl;
511 offset = nh->frag_off & htons(IP_OFFSET);
513 key->ip.frag = OVS_FRAG_TYPE_LATER;
516 if (nh->frag_off & htons(IP_MF) ||
517 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
518 key->ip.frag = OVS_FRAG_TYPE_FIRST;
520 key->ip.frag = OVS_FRAG_TYPE_NONE;
522 /* Transport layer. */
523 if (key->ip.proto == IPPROTO_TCP) {
524 if (tcphdr_ok(skb)) {
525 struct tcphdr *tcp = tcp_hdr(skb);
526 key->tp.src = tcp->source;
527 key->tp.dst = tcp->dest;
528 key->tp.flags = TCP_FLAGS_BE16(tcp);
530 memset(&key->tp, 0, sizeof(key->tp));
532 } else if (key->ip.proto == IPPROTO_UDP) {
533 if (udphdr_ok(skb)) {
534 struct udphdr *udp = udp_hdr(skb);
535 key->tp.src = udp->source;
536 key->tp.dst = udp->dest;
538 memset(&key->tp, 0, sizeof(key->tp));
540 } else if (key->ip.proto == IPPROTO_SCTP) {
541 if (sctphdr_ok(skb)) {
542 struct sctphdr *sctp = sctp_hdr(skb);
543 key->tp.src = sctp->source;
544 key->tp.dst = sctp->dest;
546 memset(&key->tp, 0, sizeof(key->tp));
548 } else if (key->ip.proto == IPPROTO_ICMP) {
549 if (icmphdr_ok(skb)) {
550 struct icmphdr *icmp = icmp_hdr(skb);
551 /* The ICMP type and code fields use the 16-bit
552 * transport port fields, so we need to store
553 * them in 16-bit network byte order. */
554 key->tp.src = htons(icmp->type);
555 key->tp.dst = htons(icmp->code);
557 memset(&key->tp, 0, sizeof(key->tp));
561 } else if (key->eth.type == htons(ETH_P_ARP) ||
562 key->eth.type == htons(ETH_P_RARP)) {
563 struct arp_eth_header *arp;
565 arp = (struct arp_eth_header *)skb_network_header(skb);
568 && arp->ar_hrd == htons(ARPHRD_ETHER)
569 && arp->ar_pro == htons(ETH_P_IP)
570 && arp->ar_hln == ETH_ALEN
571 && arp->ar_pln == 4) {
573 /* We only match on the lower 8 bits of the opcode. */
574 if (ntohs(arp->ar_op) <= 0xff)
575 key->ip.proto = ntohs(arp->ar_op);
579 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
580 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
581 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
582 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
584 memset(&key->ip, 0, sizeof(key->ip));
585 memset(&key->ipv4, 0, sizeof(key->ipv4));
587 } else if (eth_p_mpls(key->eth.type)) {
588 size_t stack_len = MPLS_HLEN;
590 /* In the presence of an MPLS label stack the end of the L2
591 * header and the beginning of the L3 header differ.
593 * Advance network_header to the beginning of the L3
594 * header. mac_len corresponds to the end of the L2 header.
599 error = check_header(skb, skb->mac_len + stack_len);
603 memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
605 if (stack_len == MPLS_HLEN)
606 memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
608 skb_set_network_header(skb, skb->mac_len + stack_len);
609 if (lse & htonl(MPLS_BOS_MASK))
612 stack_len += MPLS_HLEN;
614 } else if (key->eth.type == htons(ETH_P_IPV6)) {
615 int nh_len; /* IPv6 Header + Extensions */
617 nh_len = parse_ipv6hdr(skb, key);
618 if (unlikely(nh_len < 0)) {
619 memset(&key->ip, 0, sizeof(key->ip));
620 memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
621 if (nh_len == -EINVAL) {
622 skb->transport_header = skb->network_header;
630 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
632 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
633 key->ip.frag = OVS_FRAG_TYPE_FIRST;
635 /* Transport layer. */
636 if (key->ip.proto == NEXTHDR_TCP) {
637 if (tcphdr_ok(skb)) {
638 struct tcphdr *tcp = tcp_hdr(skb);
639 key->tp.src = tcp->source;
640 key->tp.dst = tcp->dest;
641 key->tp.flags = TCP_FLAGS_BE16(tcp);
643 memset(&key->tp, 0, sizeof(key->tp));
645 } else if (key->ip.proto == NEXTHDR_UDP) {
646 if (udphdr_ok(skb)) {
647 struct udphdr *udp = udp_hdr(skb);
648 key->tp.src = udp->source;
649 key->tp.dst = udp->dest;
651 memset(&key->tp, 0, sizeof(key->tp));
653 } else if (key->ip.proto == NEXTHDR_SCTP) {
654 if (sctphdr_ok(skb)) {
655 struct sctphdr *sctp = sctp_hdr(skb);
656 key->tp.src = sctp->source;
657 key->tp.dst = sctp->dest;
659 memset(&key->tp, 0, sizeof(key->tp));
661 } else if (key->ip.proto == NEXTHDR_ICMP) {
662 if (icmp6hdr_ok(skb)) {
663 error = parse_icmpv6(skb, key, nh_len);
667 memset(&key->tp, 0, sizeof(key->tp));
672 OVS_CB(skb)->pkt_key = key;
676 int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
678 return key_extract(skb, key);
681 int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
683 struct sw_flow_key *key)
685 /* Extract metadata from packet. */
687 memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
689 BUILD_BUG_ON(((1 << (sizeof(tun_info->options_len) * 8)) - 1) >
690 sizeof(key->tun_opts));
692 if (tun_info->options) {
693 memcpy(GENEVE_OPTS(key, tun_info->options_len),
694 tun_info->options, tun_info->options_len);
695 key->tun_opts_len = tun_info->options_len;
697 key->tun_opts_len = 0;
700 key->tun_opts_len = 0;
701 memset(&key->tun_key, 0, sizeof(key->tun_key));
704 key->phy.priority = skb->priority;
705 key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
706 key->phy.skb_mark = skb->mark;
707 key->ovs_flow_hash = 0;
710 return key_extract(skb, key);
713 int ovs_flow_key_extract_userspace(const struct nlattr *attr,
715 struct sw_flow_key *key)
719 /* Extract metadata from netlink attributes. */
720 err = ovs_nla_get_flow_metadata(attr, key);
724 return key_extract(skb, key);