~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to datapath/flow.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <linux/if_arp.h>
33
33
#include <linux/ip.h>
34
34
#include <linux/ipv6.h>
 
35
#include <linux/mpls.h>
35
36
#include <linux/sctp.h>
36
37
#include <linux/smp.h>
37
38
#include <linux/tcp.h>
41
42
#include <linux/rculist.h>
42
43
#include <net/ip.h>
43
44
#include <net/ipv6.h>
 
45
#include <net/mpls.h>
44
46
#include <net/ndisc.h>
45
47
 
46
48
#include "datapath.h"
65
67
#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
66
68
 
67
69
void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
68
 
                           struct sk_buff *skb)
 
70
                           const struct sk_buff *skb)
69
71
{
70
72
        struct flow_stats *stats;
71
73
        int node = numa_node_id();
 
74
        int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
72
75
 
73
76
        stats = rcu_dereference(flow->stats[node]);
74
77
 
92
95
                         * allocated stats as we have already locked them.
93
96
                         */
94
97
                        if (likely(flow->stats_last_writer != NUMA_NO_NODE)
95
 
                            && likely(!rcu_dereference(flow->stats[node]))) {
 
98
                            && likely(!rcu_access_pointer(flow->stats[node]))) {
96
99
                                /* Try to allocate node-specific stats. */
97
100
                                struct flow_stats *new_stats;
98
101
 
104
107
                                if (likely(new_stats)) {
105
108
                                        new_stats->used = jiffies;
106
109
                                        new_stats->packet_count = 1;
107
 
                                        new_stats->byte_count = skb->len;
 
110
                                        new_stats->byte_count = len;
108
111
                                        new_stats->tcp_flags = tcp_flags;
109
112
                                        spin_lock_init(&new_stats->lock);
110
113
 
119
122
 
120
123
        stats->used = jiffies;
121
124
        stats->packet_count++;
122
 
        stats->byte_count += skb->len;
 
125
        stats->byte_count += len;
123
126
        stats->tcp_flags |= tcp_flags;
124
127
unlock:
125
128
        spin_unlock(&stats->lock);
275
278
                        key->ip.frag = OVS_FRAG_TYPE_LATER;
276
279
                else
277
280
                        key->ip.frag = OVS_FRAG_TYPE_FIRST;
 
281
        } else {
 
282
                key->ip.frag = OVS_FRAG_TYPE_NONE;
278
283
        }
279
284
 
280
285
        nh_len = payload_ofs - nh_ofs;
359
364
         */
360
365
        key->tp.src = htons(icmp->icmp6_type);
361
366
        key->tp.dst = htons(icmp->icmp6_code);
 
367
        memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
362
368
 
363
369
        if (icmp->icmp6_code == 0 &&
364
370
            (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
398
404
                                if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
399
405
                                        goto invalid;
400
406
                                ether_addr_copy(key->ipv6.nd.sll,
401
 
                                    &nd->opt[offset+sizeof(*nd_opt)]);
 
407
                                                &nd->opt[offset+sizeof(*nd_opt)]);
402
408
                        } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
403
409
                                   && opt_len == 8) {
404
410
                                if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
405
411
                                        goto invalid;
406
412
                                ether_addr_copy(key->ipv6.nd.tll,
407
 
                                    &nd->opt[offset+sizeof(*nd_opt)]);
 
413
                                                &nd->opt[offset+sizeof(*nd_opt)]);
408
414
                        }
409
415
 
410
416
                        icmp_len -= opt_len;
449
455
        int error;
450
456
        struct ethhdr *eth;
451
457
 
 
458
        /* Flags are always used as part of stats */
 
459
        key->tp.flags = 0;
 
460
 
452
461
        skb_reset_mac_header(skb);
453
462
 
454
463
        /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
460
469
 
461
470
        __skb_pull(skb, 2 * ETH_ALEN);
462
471
        /* We are going to push all headers that we pull, so no need to
463
 
         * update skb->csum here. */
 
472
         * update skb->csum here.
 
473
         */
464
474
 
465
 
        if (vlan_tx_tag_present(skb))
 
475
        key->eth.tci = 0;
 
476
        if (skb_vlan_tag_present(skb))
466
477
                key->eth.tci = htons(vlan_get_tci(skb));
467
478
        else if (eth->h_proto == htons(ETH_P_8021Q))
468
479
                if (unlikely(parse_vlan(skb, key)))
473
484
                return -ENOMEM;
474
485
 
475
486
        skb_reset_network_header(skb);
 
487
        skb_reset_mac_len(skb);
476
488
        __skb_push(skb, skb->data - skb_mac_header(skb));
477
489
 
478
490
        /* Network layer. */
482
494
 
483
495
                error = check_iphdr(skb);
484
496
                if (unlikely(error)) {
 
497
                        memset(&key->ip, 0, sizeof(key->ip));
 
498
                        memset(&key->ipv4, 0, sizeof(key->ipv4));
485
499
                        if (error == -EINVAL) {
486
500
                                skb->transport_header = skb->network_header;
487
501
                                error = 0;
503
517
                        return 0;
504
518
                }
505
519
                if (nh->frag_off & htons(IP_MF) ||
506
 
                         skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
 
520
                        skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
507
521
                        key->ip.frag = OVS_FRAG_TYPE_FIRST;
 
522
                else
 
523
                        key->ip.frag = OVS_FRAG_TYPE_NONE;
508
524
 
509
525
                /* Transport layer. */
510
526
                if (key->ip.proto == IPPROTO_TCP) {
513
529
                                key->tp.src = tcp->source;
514
530
                                key->tp.dst = tcp->dest;
515
531
                                key->tp.flags = TCP_FLAGS_BE16(tcp);
 
532
                        } else {
 
533
                                memset(&key->tp, 0, sizeof(key->tp));
516
534
                        }
 
535
 
517
536
                } else if (key->ip.proto == IPPROTO_UDP) {
518
537
                        if (udphdr_ok(skb)) {
519
538
                                struct udphdr *udp = udp_hdr(skb);
520
539
                                key->tp.src = udp->source;
521
540
                                key->tp.dst = udp->dest;
 
541
                        } else {
 
542
                                memset(&key->tp, 0, sizeof(key->tp));
522
543
                        }
523
544
                } else if (key->ip.proto == IPPROTO_SCTP) {
524
545
                        if (sctphdr_ok(skb)) {
525
546
                                struct sctphdr *sctp = sctp_hdr(skb);
526
547
                                key->tp.src = sctp->source;
527
548
                                key->tp.dst = sctp->dest;
 
549
                        } else {
 
550
                                memset(&key->tp, 0, sizeof(key->tp));
528
551
                        }
529
552
                } else if (key->ip.proto == IPPROTO_ICMP) {
530
553
                        if (icmphdr_ok(skb)) {
531
554
                                struct icmphdr *icmp = icmp_hdr(skb);
532
555
                                /* The ICMP type and code fields use the 16-bit
533
556
                                 * transport port fields, so we need to store
534
 
                                 * them in 16-bit network byte order. */
 
557
                                 * them in 16-bit network byte order.
 
558
                                 */
535
559
                                key->tp.src = htons(icmp->type);
536
560
                                key->tp.dst = htons(icmp->code);
 
561
                        } else {
 
562
                                memset(&key->tp, 0, sizeof(key->tp));
537
563
                        }
538
564
                }
539
565
 
540
 
        } else if ((key->eth.type == htons(ETH_P_ARP) ||
541
 
                   key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
 
566
        } else if (key->eth.type == htons(ETH_P_ARP) ||
 
567
                   key->eth.type == htons(ETH_P_RARP)) {
 
568
                struct arp_eth_header *arp;
542
569
                bool arp_available = arphdr_ok(skb);
543
 
                struct arp_eth_header *arp;
544
570
 
545
571
                arp = (struct arp_eth_header *)skb_network_header(skb);
546
572
 
549
575
                    arp->ar_pro == htons(ETH_P_IP) &&
550
576
                    arp->ar_hln == ETH_ALEN &&
551
577
                    arp->ar_pln == 4) {
 
578
 
552
579
                        /* We only match on the lower 8 bits of the opcode. */
553
580
                        if (ntohs(arp->ar_op) <= 0xff)
554
581
                                key->ip.proto = ntohs(arp->ar_op);
 
582
                        else
 
583
                                key->ip.proto = 0;
 
584
 
555
585
                        memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
556
586
                        memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
557
587
                        ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
558
588
                        ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
 
589
                } else {
 
590
                        memset(&key->ip, 0, sizeof(key->ip));
 
591
                        memset(&key->ipv4, 0, sizeof(key->ipv4));
 
592
                }
 
593
        } else if (eth_p_mpls(key->eth.type)) {
 
594
                size_t stack_len = MPLS_HLEN;
 
595
 
 
596
                /* In the presence of an MPLS label stack the end of the L2
 
597
                 * header and the beginning of the L3 header differ.
 
598
                 *
 
599
                 * Advance network_header to the beginning of the L3
 
600
                 * header. mac_len corresponds to the end of the L2 header.
 
601
                 */
 
602
                while (1) {
 
603
                        __be32 lse;
 
604
 
 
605
                        error = check_header(skb, skb->mac_len + stack_len);
 
606
                        if (unlikely(error))
 
607
                                return 0;
 
608
 
 
609
                        memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
 
610
 
 
611
                        if (stack_len == MPLS_HLEN)
 
612
                                memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
 
613
 
 
614
                        skb_set_network_header(skb, skb->mac_len + stack_len);
 
615
                        if (lse & htonl(MPLS_LS_S_MASK))
 
616
                                break;
 
617
 
 
618
                        stack_len += MPLS_HLEN;
559
619
                }
560
620
        } else if (key->eth.type == htons(ETH_P_IPV6)) {
561
621
                int nh_len;             /* IPv6 Header + Extensions */
562
622
 
563
623
                nh_len = parse_ipv6hdr(skb, key);
564
624
                if (unlikely(nh_len < 0)) {
 
625
                        memset(&key->ip, 0, sizeof(key->ip));
 
626
                        memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
565
627
                        if (nh_len == -EINVAL) {
566
628
                                skb->transport_header = skb->network_header;
567
629
                                error = 0;
583
645
                                key->tp.src = tcp->source;
584
646
                                key->tp.dst = tcp->dest;
585
647
                                key->tp.flags = TCP_FLAGS_BE16(tcp);
 
648
                        } else {
 
649
                                memset(&key->tp, 0, sizeof(key->tp));
586
650
                        }
587
651
                } else if (key->ip.proto == NEXTHDR_UDP) {
588
652
                        if (udphdr_ok(skb)) {
589
653
                                struct udphdr *udp = udp_hdr(skb);
590
654
                                key->tp.src = udp->source;
591
655
                                key->tp.dst = udp->dest;
 
656
                        } else {
 
657
                                memset(&key->tp, 0, sizeof(key->tp));
592
658
                        }
593
659
                } else if (key->ip.proto == NEXTHDR_SCTP) {
594
660
                        if (sctphdr_ok(skb)) {
595
661
                                struct sctphdr *sctp = sctp_hdr(skb);
596
662
                                key->tp.src = sctp->source;
597
663
                                key->tp.dst = sctp->dest;
 
664
                        } else {
 
665
                                memset(&key->tp, 0, sizeof(key->tp));
598
666
                        }
599
667
                } else if (key->ip.proto == NEXTHDR_ICMP) {
600
668
                        if (icmp6hdr_ok(skb)) {
601
669
                                error = parse_icmpv6(skb, key, nh_len);
602
670
                                if (error)
603
671
                                        return error;
 
672
                        } else {
 
673
                                memset(&key->tp, 0, sizeof(key->tp));
604
674
                        }
605
675
                }
606
676
        }
607
 
 
608
677
        return 0;
609
678
}
610
679
 
611
 
int ovs_flow_key_extract(struct sk_buff *skb, struct sw_flow_key *key)
 
680
int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
 
681
{
 
682
        return key_extract(skb, key);
 
683
}
 
684
 
 
685
int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
 
686
                         struct sk_buff *skb, struct sw_flow_key *key)
612
687
{
613
688
        /* Extract metadata from packet. */
614
 
 
615
 
        memset(key, 0, sizeof(*key));
616
 
        if (OVS_CB(skb)->tun_key)
617
 
                memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
 
689
        if (tun_info) {
 
690
                memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
 
691
 
 
692
                BUILD_BUG_ON(((1 << (sizeof(tun_info->options_len) * 8)) - 1) >
 
693
                             sizeof(key->tun_opts));
 
694
 
 
695
                if (tun_info->options) {
 
696
                        memcpy(TUN_METADATA_OPTS(key, tun_info->options_len),
 
697
                               tun_info->options, tun_info->options_len);
 
698
                        key->tun_opts_len = tun_info->options_len;
 
699
                } else {
 
700
                        key->tun_opts_len = 0;
 
701
                }
 
702
        } else {
 
703
                key->tun_opts_len = 0;
 
704
                memset(&key->tun_key, 0, sizeof(key->tun_key));
 
705
        }
618
706
 
619
707
        key->phy.priority = skb->priority;
620
708
        key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
621
709
        key->phy.skb_mark = skb->mark;
 
710
        key->ovs_flow_hash = 0;
 
711
        key->recirc_id = 0;
622
712
 
623
713
        return key_extract(skb, key);
624
714
}
625
715
 
626
716
int ovs_flow_key_extract_userspace(const struct nlattr *attr,
627
717
                                   struct sk_buff *skb,
628
 
                                   struct sw_flow_key *key)
 
718
                                   struct sw_flow_key *key, bool log)
629
719
{
630
720
        int err;
631
721
 
632
 
        memset(key, 0, sizeof(*key));
633
722
        /* Extract metadata from netlink attributes. */
634
 
        err = ovs_nla_get_flow_metadata(attr, key);
 
723
        err = ovs_nla_get_flow_metadata(attr, key, log);
635
724
        if (err)
636
725
                return err;
637
726
 
638
727
        return key_extract(skb, key);
639
728
}
640
 
 
641
 
int ovs_flow_key_extract_recirc(u32 recirc_id,
642
 
                                const struct sw_flow_key *key,
643
 
                                struct sk_buff *skb,
644
 
                                struct sw_flow_key *new_key)
645
 
{
646
 
        memset(new_key, 0, sizeof(*new_key));
647
 
        memcpy(new_key, key, OVS_SW_FLOW_KEY_METADATA_SIZE);
648
 
        new_key->recirc_id = recirc_id;
649
 
        return key_extract(skb, new_key);
650
 
}