2 * Copyright (c) 2007-2013 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
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/smp.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/icmp.h>
42 #include <linux/icmpv6.h>
43 #include <linux/rculist.h>
46 #include <net/ndisc.h>
50 u64 ovs_flow_used_time(unsigned long flow_jiffies)
52 struct timespec cur_ts;
55 ktime_get_ts(&cur_ts);
56 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
57 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
58 cur_ts.tv_nsec / NSEC_PER_MSEC;
60 return cur_ms - idle_ms;
63 #define TCP_FLAGS_OFFSET 13
64 #define TCP_FLAG_MASK 0x3f
66 void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
68 struct sw_flow_stats *stats = &flow->stats[smp_processor_id()];
71 if ((flow->key.eth.type == htons(ETH_P_IP) ||
72 flow->key.eth.type == htons(ETH_P_IPV6)) &&
73 flow->key.ip.proto == IPPROTO_TCP &&
74 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
75 u8 *tcp = (u8 *)tcp_hdr(skb);
76 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
79 spin_lock(&stats->lock);
80 stats->used = jiffies;
81 stats->packet_count++;
82 stats->byte_count += skb->len;
83 stats->tcp_flags |= tcp_flags;
84 spin_unlock(&stats->lock);
87 void ovs_flow_stats_get(struct sw_flow *flow, struct sw_flow_stats *res)
91 memset(res, 0, sizeof(*res));
94 for_each_possible_cpu(cpu) {
95 struct sw_flow_stats *stats = &flow->stats[cpu];
100 spin_lock(&stats->lock);
101 if (time_after(stats->used, res->used))
102 res->used = stats->used;
103 res->packet_count += stats->packet_count;
104 res->byte_count += stats->byte_count;
105 res->tcp_flags |= stats->tcp_flags;
106 spin_unlock(&stats->lock);
115 void ovs_flow_stats_clear(struct sw_flow *flow)
120 for_each_possible_cpu(cpu) {
121 struct sw_flow_stats *stats = &flow->stats[cpu];
126 spin_lock(&stats->lock);
128 stats->packet_count = 0;
129 stats->byte_count = 0;
130 stats->tcp_flags = 0;
131 spin_unlock(&stats->lock);
139 static int check_header(struct sk_buff *skb, int len)
141 if (unlikely(skb->len < len))
143 if (unlikely(!pskb_may_pull(skb, len)))
148 static bool arphdr_ok(struct sk_buff *skb)
150 return pskb_may_pull(skb, skb_network_offset(skb) +
151 sizeof(struct arp_eth_header));
154 static int check_iphdr(struct sk_buff *skb)
156 unsigned int nh_ofs = skb_network_offset(skb);
160 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
164 ip_len = ip_hdrlen(skb);
165 if (unlikely(ip_len < sizeof(struct iphdr) ||
166 skb->len < nh_ofs + ip_len))
169 skb_set_transport_header(skb, nh_ofs + ip_len);
173 static bool tcphdr_ok(struct sk_buff *skb)
175 int th_ofs = skb_transport_offset(skb);
178 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
181 tcp_len = tcp_hdrlen(skb);
182 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
183 skb->len < th_ofs + tcp_len))
189 static bool udphdr_ok(struct sk_buff *skb)
191 return pskb_may_pull(skb, skb_transport_offset(skb) +
192 sizeof(struct udphdr));
195 static bool sctphdr_ok(struct sk_buff *skb)
197 return pskb_may_pull(skb, skb_transport_offset(skb) +
198 sizeof(struct sctphdr));
201 static bool icmphdr_ok(struct sk_buff *skb)
203 return pskb_may_pull(skb, skb_transport_offset(skb) +
204 sizeof(struct icmphdr));
207 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
209 unsigned int nh_ofs = skb_network_offset(skb);
217 err = check_header(skb, nh_ofs + sizeof(*nh));
222 nexthdr = nh->nexthdr;
223 payload_ofs = (u8 *)(nh + 1) - skb->data;
225 key->ip.proto = NEXTHDR_NONE;
226 key->ip.tos = ipv6_get_dsfield(nh);
227 key->ip.ttl = nh->hop_limit;
228 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
229 key->ipv6.addr.src = nh->saddr;
230 key->ipv6.addr.dst = nh->daddr;
232 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
233 if (unlikely(payload_ofs < 0))
237 if (frag_off & htons(~0x7))
238 key->ip.frag = OVS_FRAG_TYPE_LATER;
240 key->ip.frag = OVS_FRAG_TYPE_FIRST;
243 nh_len = payload_ofs - nh_ofs;
244 skb_set_transport_header(skb, nh_ofs + nh_len);
245 key->ip.proto = nexthdr;
249 static bool icmp6hdr_ok(struct sk_buff *skb)
251 return pskb_may_pull(skb, skb_transport_offset(skb) +
252 sizeof(struct icmp6hdr));
255 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
258 __be16 eth_type; /* ETH_P_8021Q */
261 struct qtag_prefix *qp;
263 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
266 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
270 qp = (struct qtag_prefix *) skb->data;
271 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
272 __skb_pull(skb, sizeof(struct qtag_prefix));
277 static __be16 parse_ethertype(struct sk_buff *skb)
279 struct llc_snap_hdr {
280 u8 dsap; /* Always 0xAA */
281 u8 ssap; /* Always 0xAA */
286 struct llc_snap_hdr *llc;
289 proto = *(__be16 *) skb->data;
290 __skb_pull(skb, sizeof(__be16));
292 if (ntohs(proto) >= ETH_P_802_3_MIN)
295 if (skb->len < sizeof(struct llc_snap_hdr))
296 return htons(ETH_P_802_2);
298 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
301 llc = (struct llc_snap_hdr *) skb->data;
302 if (llc->dsap != LLC_SAP_SNAP ||
303 llc->ssap != LLC_SAP_SNAP ||
304 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
305 return htons(ETH_P_802_2);
307 __skb_pull(skb, sizeof(struct llc_snap_hdr));
309 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
310 return llc->ethertype;
312 return htons(ETH_P_802_2);
315 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
318 struct icmp6hdr *icmp = icmp6_hdr(skb);
320 /* The ICMPv6 type and code fields use the 16-bit transport port
321 * fields, so we need to store them in 16-bit network byte order.
323 key->ipv6.tp.src = htons(icmp->icmp6_type);
324 key->ipv6.tp.dst = htons(icmp->icmp6_code);
326 if (icmp->icmp6_code == 0 &&
327 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
328 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
329 int icmp_len = skb->len - skb_transport_offset(skb);
333 /* In order to process neighbor discovery options, we need the
336 if (unlikely(icmp_len < sizeof(*nd)))
339 if (unlikely(skb_linearize(skb)))
342 nd = (struct nd_msg *)skb_transport_header(skb);
343 key->ipv6.nd.target = nd->target;
345 icmp_len -= sizeof(*nd);
347 while (icmp_len >= 8) {
348 struct nd_opt_hdr *nd_opt =
349 (struct nd_opt_hdr *)(nd->opt + offset);
350 int opt_len = nd_opt->nd_opt_len * 8;
352 if (unlikely(!opt_len || opt_len > icmp_len))
355 /* Store the link layer address if the appropriate
356 * option is provided. It is considered an error if
357 * the same link layer option is specified twice.
359 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
361 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
363 memcpy(key->ipv6.nd.sll,
364 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
365 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
367 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
369 memcpy(key->ipv6.nd.tll,
370 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
381 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
382 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
383 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
389 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
390 * @skb: sk_buff that contains the frame, with skb->data pointing to the
392 * @in_port: port number on which @skb was received.
393 * @key: output flow key
395 * The caller must ensure that skb->len >= ETH_HLEN.
397 * Returns 0 if successful, otherwise a negative errno value.
399 * Initializes @skb header pointers as follows:
401 * - skb->mac_header: the Ethernet header.
403 * - skb->network_header: just past the Ethernet header, or just past the
404 * VLAN header, to the first byte of the Ethernet payload.
406 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
407 * on output, then just past the IP header, if one is present and
408 * of a correct length, otherwise the same as skb->network_header.
409 * For other key->eth.type values it is left untouched.
411 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
416 memset(key, 0, sizeof(*key));
418 key->phy.priority = skb->priority;
419 if (OVS_CB(skb)->tun_key)
420 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
421 key->phy.in_port = in_port;
422 key->phy.skb_mark = skb->mark;
424 skb_reset_mac_header(skb);
426 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
427 * header in the linear data area.
430 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
431 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
433 __skb_pull(skb, 2 * ETH_ALEN);
434 /* We are going to push all headers that we pull, so no need to
435 * update skb->csum here. */
437 if (vlan_tx_tag_present(skb))
438 key->eth.tci = htons(vlan_get_tci(skb));
439 else if (eth->h_proto == htons(ETH_P_8021Q))
440 if (unlikely(parse_vlan(skb, key)))
443 key->eth.type = parse_ethertype(skb);
444 if (unlikely(key->eth.type == htons(0)))
447 skb_reset_network_header(skb);
448 __skb_push(skb, skb->data - skb_mac_header(skb));
451 if (key->eth.type == htons(ETH_P_IP)) {
455 error = check_iphdr(skb);
456 if (unlikely(error)) {
457 if (error == -EINVAL) {
458 skb->transport_header = skb->network_header;
465 key->ipv4.addr.src = nh->saddr;
466 key->ipv4.addr.dst = nh->daddr;
468 key->ip.proto = nh->protocol;
469 key->ip.tos = nh->tos;
470 key->ip.ttl = nh->ttl;
472 offset = nh->frag_off & htons(IP_OFFSET);
474 key->ip.frag = OVS_FRAG_TYPE_LATER;
477 if (nh->frag_off & htons(IP_MF) ||
478 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
479 key->ip.frag = OVS_FRAG_TYPE_FIRST;
481 /* Transport layer. */
482 if (key->ip.proto == IPPROTO_TCP) {
483 if (tcphdr_ok(skb)) {
484 struct tcphdr *tcp = tcp_hdr(skb);
485 key->ipv4.tp.src = tcp->source;
486 key->ipv4.tp.dst = tcp->dest;
488 } else if (key->ip.proto == IPPROTO_UDP) {
489 if (udphdr_ok(skb)) {
490 struct udphdr *udp = udp_hdr(skb);
491 key->ipv4.tp.src = udp->source;
492 key->ipv4.tp.dst = udp->dest;
494 } else if (key->ip.proto == IPPROTO_SCTP) {
495 if (sctphdr_ok(skb)) {
496 struct sctphdr *sctp = sctp_hdr(skb);
497 key->ipv4.tp.src = sctp->source;
498 key->ipv4.tp.dst = sctp->dest;
500 } else if (key->ip.proto == IPPROTO_ICMP) {
501 if (icmphdr_ok(skb)) {
502 struct icmphdr *icmp = icmp_hdr(skb);
503 /* The ICMP type and code fields use the 16-bit
504 * transport port fields, so we need to store
505 * them in 16-bit network byte order. */
506 key->ipv4.tp.src = htons(icmp->type);
507 key->ipv4.tp.dst = htons(icmp->code);
511 } else if ((key->eth.type == htons(ETH_P_ARP) ||
512 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
513 struct arp_eth_header *arp;
515 arp = (struct arp_eth_header *)skb_network_header(skb);
517 if (arp->ar_hrd == htons(ARPHRD_ETHER)
518 && arp->ar_pro == htons(ETH_P_IP)
519 && arp->ar_hln == ETH_ALEN
520 && arp->ar_pln == 4) {
522 /* We only match on the lower 8 bits of the opcode. */
523 if (ntohs(arp->ar_op) <= 0xff)
524 key->ip.proto = ntohs(arp->ar_op);
525 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
526 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
527 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
528 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
530 } else if (key->eth.type == htons(ETH_P_IPV6)) {
531 int nh_len; /* IPv6 Header + Extensions */
533 nh_len = parse_ipv6hdr(skb, key);
534 if (unlikely(nh_len < 0)) {
535 if (nh_len == -EINVAL) {
536 skb->transport_header = skb->network_header;
544 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
546 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
547 key->ip.frag = OVS_FRAG_TYPE_FIRST;
549 /* Transport layer. */
550 if (key->ip.proto == NEXTHDR_TCP) {
551 if (tcphdr_ok(skb)) {
552 struct tcphdr *tcp = tcp_hdr(skb);
553 key->ipv6.tp.src = tcp->source;
554 key->ipv6.tp.dst = tcp->dest;
556 } else if (key->ip.proto == NEXTHDR_UDP) {
557 if (udphdr_ok(skb)) {
558 struct udphdr *udp = udp_hdr(skb);
559 key->ipv6.tp.src = udp->source;
560 key->ipv6.tp.dst = udp->dest;
562 } else if (key->ip.proto == NEXTHDR_SCTP) {
563 if (sctphdr_ok(skb)) {
564 struct sctphdr *sctp = sctp_hdr(skb);
565 key->ipv6.tp.src = sctp->source;
566 key->ipv6.tp.dst = sctp->dest;
568 } else if (key->ip.proto == NEXTHDR_ICMP) {
569 if (icmp6hdr_ok(skb)) {
570 error = parse_icmpv6(skb, key, nh_len);