Support controller discovery in Debian packages.
[sliver-openvswitch.git] / datapath / flow.c
index a755947..8cf09a1 100644 (file)
@@ -9,11 +9,10 @@
 #include <linux/etherdevice.h>
 #include <linux/if_ether.h>
 #include <linux/if_vlan.h>
-#include <linux/if_arp.h>
 #include <net/llc_pdu.h>
 #include <linux/ip.h>
-#include <linux/ipv6.h>
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/tcp.h>
 #include <linux/udp.h>
 #include <linux/in.h>
@@ -44,18 +43,17 @@ int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
 
 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
  * modulo wildcards, zero otherwise. */
-inline
 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
 {
        return flow_fields_match(a, b, (a->wildcards | b->wildcards));
 }
+EXPORT_SYMBOL(flow_matches);
 
 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
  * describing the deletion) match, that is, if their fields are 
  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
  * wildcards must match in both 't_key' and 'd_key'.  Note that the
  * table's wildcards are ignored unless 'strict' is set. */
-inline
 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
 {
        if (strict && (t->wildcards != d->wildcards))
@@ -63,21 +61,52 @@ int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, i
 
        return flow_fields_match(t, d, d->wildcards);
 }
+EXPORT_SYMBOL(flow_del_matches);
 
 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
 {
        to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
-       to->in_port   = from->in_port;
-       to->dl_vlan   = from->dl_vlan;
+       memset(to->pad, '\0', sizeof(to->pad));
+       to->in_port = from->in_port;
+       to->dl_vlan = from->dl_vlan;
        memcpy(to->dl_src, from->dl_src, ETH_ALEN);
        memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
-       to->dl_type   = from->dl_type;
-       to->nw_src        = from->nw_src;
-       to->nw_dst        = from->nw_dst;
-       to->nw_proto  = from->nw_proto;
-       to->tp_src        = from->tp_src;
-       to->tp_dst        = from->tp_dst;
-       memset(to->pad, '\0', sizeof(to->pad));
+       to->dl_type = from->dl_type;
+
+       to->nw_src = to->nw_dst = to->nw_proto = 0;
+       to->tp_src = to->tp_dst = 0;
+
+#define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
+#define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
+       if (to->wildcards & OFPFW_DL_TYPE) {
+               /* Can't sensibly match on network or transport headers if the
+                * data link type is unknown. */
+               to->wildcards |= OFPFW_NW | OFPFW_TP;
+       } else if (from->dl_type == htons(ETH_P_IP)) {
+               to->nw_src   = from->nw_src;
+               to->nw_dst   = from->nw_dst;
+               to->nw_proto = from->nw_proto;
+
+               if (to->wildcards & OFPFW_NW_PROTO) {
+                       /* Can't sensibly match on transport headers if the
+                        * network protocol is unknown. */
+                       to->wildcards |= OFPFW_TP;
+               } else if (from->nw_proto == IPPROTO_TCP
+                          || from->nw_proto == IPPROTO_UDP) {
+                       to->tp_src = from->tp_src;
+                       to->tp_dst = from->tp_dst;
+               } else {
+                       /* Transport layer fields are undefined.  Mark them as
+                        * exact-match to allow such flows to reside in
+                        * table-hash, instead of falling into table-linear. */
+                       to->wildcards &= ~OFPFW_TP;
+               }
+       } else {
+               /* Network and transport layer fields are undefined.  Mark them
+                * as exact-match to allow such flows to reside in table-hash,
+                * instead of falling into table-linear. */
+               to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
+       }
 }
 
 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
@@ -104,6 +133,7 @@ int flow_del(struct sw_flow *flow)
 {
        return !atomic_cmpxchg(&flow->deleted, 0, 1);
 }
+EXPORT_SYMBOL(flow_del);
 
 /* Allocates and returns a new flow with 'n_actions' action, using allocation
  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
@@ -131,6 +161,7 @@ void flow_free(struct sw_flow *flow)
        kfree(flow->actions);
        kmem_cache_free(flow_cache, flow);
 }
+EXPORT_SYMBOL(flow_free);
 
 /* RCU callback used by flow_deferred_free. */
 static void rcu_callback(struct rcu_head *rcu)
@@ -145,6 +176,7 @@ void flow_deferred_free(struct sw_flow *flow)
 {
        call_rcu(&flow->rcu, rcu_callback);
 }
+EXPORT_SYMBOL(flow_deferred_free);
 
 /* Prints a representation of 'key' to the kernel log. */
 void print_flow(const struct sw_flow_key *key)
@@ -168,26 +200,7 @@ void print_flow(const struct sw_flow_key *key)
                        ((unsigned char *)&key->nw_dst)[3],
                        ntohs(key->tp_src), ntohs(key->tp_dst));
 }
-
-uint32_t hash_in6(const struct in6_addr *in)
-{
-       return (in->s6_addr32[0] ^ in->s6_addr32[1]
-                       ^ in->s6_addr32[2] ^ in->s6_addr32[3]);
-}
-
-// with inspiration from linux/if_arp.h
-struct arp_eth_hdr {
-       uint16_t  ar_hrd;  /* format of hardware address    */
-       uint16_t  ar_pro;  /* format of protocol address    */
-       uint8_t   ar_hln;  /* length of hardware address    */
-       uint8_t   ar_pln;  /* length of protocol address    */
-       uint16_t  ar_op;   /* ARP opcode (command)          */
-
-       uint8_t   ar_sha[ETH_ALEN]; /* source hardware addr */
-       uint32_t  ar_sip;           /* source protocol addr */
-       uint8_t   ar_tha[ETH_ALEN]; /* dest hardware addr   */
-       uint32_t  ar_tip;           /* dest protocol addr   */
-} __attribute__((packed));
+EXPORT_SYMBOL(print_flow);
 
 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
  * and initializes 'key' to match. */
@@ -256,30 +269,6 @@ void flow_extract(struct sk_buff *skb, uint16_t in_port,
                key->tp_dst = th->dest;
 
                return;
-       } else if (key->dl_type == htons(ETH_P_IPV6)) {
-               struct ipv6hdr *nh = ipv6_hdr(skb);
-               key->nw_src = hash_in6(&nh->saddr);
-               key->nw_dst = hash_in6(&nh->daddr);
-               /* FIXME: Need to traverse next-headers until we find the
-                * upper-layer header. */
-               key->nw_proto = 0;
-               goto no_th;
-       } else if (key->dl_type == htons(ETH_P_ARP)) {
-               /* just barely within 46-byte minimum packet */
-               struct arp_eth_hdr *ah = (struct arp_eth_hdr *)skb_network_header(skb);
-               if (ah->ar_hrd == htons(ARPHRD_ETHER)
-                   && ah->ar_pro == htons(ETH_P_IP)
-                   && ah->ar_hln == ETH_ALEN
-                   && ah->ar_pln == sizeof(key->nw_src))
-               {
-                       /* check if sha/tha match dl_src/dl_dst? */
-                       key->nw_src = ah->ar_sip;
-                       key->nw_dst = ah->ar_tip;
-                       key->nw_proto = 0;
-                       goto no_th;
-               }
-       } else {
-               /* Fall through. */
        }
 
        key->nw_src = 0;