Merge branch 'mainstream'
authorGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Wed, 28 Aug 2013 15:28:10 +0000 (17:28 +0200)
committerGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Wed, 28 Aug 2013 15:28:10 +0000 (17:28 +0200)
56 files changed:
AUTHORS
OPENFLOW-1.1+
datapath/flow.c
datapath/flow.h
datapath/linux/compat/include/linux/kernel.h
include/openflow/openflow-1.0.h
include/openvswitch/types.h
lib/automake.mk
lib/bfd.c
lib/classifier.c
lib/classifier.h
lib/compiler.h
lib/csum.c
lib/csum.h
lib/flow.c
lib/flow.h
lib/learn.c
lib/learning-switch.c
lib/learning-switch.h
lib/match.c
lib/match.h
lib/meta-flow.c
lib/meta-flow.h
lib/netdev-bsd.c
lib/netdev-dummy.c
lib/netdev-linux.c
lib/netdev-provider.h
lib/netdev-vport.c
lib/netdev.c
lib/netdev.h
lib/ofp-actions.h
lib/ofp-errors.h
lib/ofp-parse.c
lib/ofp-parse.h
lib/ofp-print.c
lib/ofp-util.c
lib/ofp-util.h
lib/ovs-atomic-clang.h [new file with mode: 0644]
lib/ovs-atomic-flag-gcc4.7+.h [new file with mode: 0644]
lib/ovs-atomic-gcc4.7+.h
lib/ovs-atomic.h
lib/packets.c
lib/packets.h
lib/unaligned.h
ofproto/ofproto-dpif-xlate.c
ofproto/ofproto-dpif.c
ofproto/ofproto-provider.h
ofproto/ofproto.c
ofproto/pktbuf.c
tests/learn.at
tests/ofproto-dpif.at
tests/ovs-ofctl.at
utilities/ovs-controller.c
utilities/ovs-ofctl.c
utilities/ovs-save
vswitchd/bridge.c

diff --git a/AUTHORS b/AUTHORS
index 890ed3a..af34bfe 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -164,6 +164,7 @@ Jari Sundell            sundell.software@gmail.com
 Jed Daniels             openvswitch@jeddaniels.com
 Jeff Merrick            jmerrick@vmware.com
 Jeongkeun Lee           jklee@hp.com
+Jian Qiu                swordqiu@gmail.com
 Joan Cirer              joan@ev0.net
 John Galgay             john@galgay.net
 Kevin Mancuso           kevin.mancuso@rackspace.com
index 190dd29..753dbba 100644 (file)
@@ -119,9 +119,6 @@ end of the OF1.2 spec.  I didn't compare the specs carefully yet.)
 
     * OFPT_FLOW_MOD:
 
-        * New flag OFPFF_RESET_COUNTS.
-          [required for OF1.2+]
-
         * Add ability to delete flow in all tables.
           [required for OF1.2+]
 
index 3dc1b44..7a697a4 100644 (file)
@@ -55,8 +55,8 @@ static void update_range__(struct sw_flow_match *match,
                          size_t offset, size_t size, bool is_mask)
 {
        struct sw_flow_key_range *range = NULL;
-       size_t start = offset;
-       size_t end = offset + size;
+       size_t start = rounddown(offset, sizeof(long));
+       size_t end = roundup(offset + size, sizeof(long));
 
        if (!is_mask)
                range = &match->range;
@@ -103,6 +103,11 @@ static void update_range__(struct sw_flow_match *match,
                }                                                           \
        } while (0)
 
+static u16 range_n_bytes(const struct sw_flow_key_range *range)
+{
+       return range->end - range->start;
+}
+
 void ovs_match_init(struct sw_flow_match *match,
                    struct sw_flow_key *key,
                    struct sw_flow_mask *mask)
@@ -371,16 +376,17 @@ static bool icmp6hdr_ok(struct sk_buff *skb)
 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
                       const struct sw_flow_mask *mask)
 {
-       u8 *m = (u8 *)&mask->key + mask->range.start;
-       u8 *s = (u8 *)src + mask->range.start;
-       u8 *d = (u8 *)dst + mask->range.start;
+       const long *m = (long *)((u8 *)&mask->key + mask->range.start);
+       const long *s = (long *)((u8 *)src + mask->range.start);
+       long *d = (long *)((u8 *)dst + mask->range.start);
        int i;
 
-       memset(dst, 0, sizeof(*dst));
-       for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
-               *d = *s & *m;
-               d++, s++, m++;
-       }
+       /* The memory outside of the 'mask->range' are not set since
+        * further operations on 'dst' only uses contents within
+        * 'mask->range'.
+        */
+       for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
+               *d++ = *s++ & *m++;
 }
 
 #define TCP_FLAGS_OFFSET 13
@@ -1008,8 +1014,13 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
                         int key_end)
 {
-       return jhash2((u32 *)((u8 *)key + key_start),
-                     DIV_ROUND_UP(key_end - key_start, sizeof(u32)), 0);
+       u32 *hash_key = (u32 *)((u8 *)key + key_start);
+       int hash_u32s = (key_end - key_start) >> 2;
+
+       /* Make sure number of hash bytes are multiple of u32. */
+       BUILD_BUG_ON(sizeof(long) % sizeof(u32));
+
+       return jhash2(hash_key, hash_u32s, 0);
 }
 
 static int flow_key_start(const struct sw_flow_key *key)
@@ -1017,17 +1028,25 @@ static int flow_key_start(const struct sw_flow_key *key)
        if (key->tun_key.ipv4_dst)
                return 0;
        else
-               return offsetof(struct sw_flow_key, phy);
+               return rounddown(offsetof(struct sw_flow_key, phy),
+                                         sizeof(long));
 }
 
 static bool __cmp_key(const struct sw_flow_key *key1,
                const struct sw_flow_key *key2,  int key_start, int key_end)
 {
-       return !memcmp((u8 *)key1 + key_start,
-                       (u8 *)key2 + key_start, (key_end - key_start));
+       const long *cp1 = (long *)((u8 *)key1 + key_start);
+       const long *cp2 = (long *)((u8 *)key2 + key_start);
+       long diffs = 0;
+       int i;
+
+       for (i = key_start; i < key_end;  i += sizeof(long))
+               diffs |= *cp1++ ^ *cp2++;
+
+       return diffs == 0;
 }
 
-static bool __flow_cmp_key(const struct sw_flow *flow,
+static bool __flow_cmp_masked_key(const struct sw_flow *flow,
                const struct sw_flow_key *key, int key_start, int key_end)
 {
        return __cmp_key(&flow->key, key, key_start, key_end);
@@ -1064,7 +1083,7 @@ struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
 }
 
 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
-                                   const struct sw_flow_key *flow_key,
+                                   const struct sw_flow_key *unmasked,
                                    struct sw_flow_mask *mask)
 {
        struct sw_flow *flow;
@@ -1074,12 +1093,13 @@ static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
        u32 hash;
        struct sw_flow_key masked_key;
 
-       ovs_flow_key_mask(&masked_key, flow_key, mask);
+       ovs_flow_key_mask(&masked_key, unmasked, mask);
        hash = ovs_flow_hash(&masked_key, key_start, key_end);
        head = find_bucket(table, hash);
        hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
                if (flow->mask == mask &&
-                   __flow_cmp_key(flow, &masked_key, key_start, key_end))
+                   __flow_cmp_masked_key(flow, &masked_key,
+                                         key_start, key_end))
                        return flow;
        }
        return NULL;
@@ -1975,6 +1995,8 @@ nla_put_failure:
  * Returns zero if successful or a negative error code. */
 int ovs_flow_init(void)
 {
+       BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
+
        flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
                                        0, NULL);
        if (flow_cache == NULL)
@@ -2037,7 +2059,7 @@ static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
 
        return  (a->range.end == b->range.end)
                && (a->range.start == b->range.start)
-               && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
+               && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
 }
 
 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
@@ -2074,5 +2096,5 @@ static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
        u8 *m = (u8 *)&mask->key + range->start;
 
        mask->range = *range;
-       memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
+       memset(m, val, range_n_bytes(range));
 }
index e1d92e4..eda74f3 100644 (file)
@@ -127,7 +127,7 @@ struct sw_flow_key {
                        } nd;
                } ipv6;
        };
-};
+} __aligned(__alignof__(long));
 
 struct sw_flow {
        struct rcu_head rcu;
@@ -151,11 +151,6 @@ struct sw_flow_key_range {
        size_t end;
 };
 
-static inline u16 ovs_sw_flow_key_range_actual_size(const struct sw_flow_key_range *range)
-{
-       return range->end - range->start;
-}
-
 struct sw_flow_match {
        struct sw_flow_key *key;
        struct sw_flow_key_range range;
@@ -255,18 +250,6 @@ struct sw_flow_mask {
        struct sw_flow_key key;
 };
 
-static inline u16
-ovs_sw_flow_mask_actual_size(const struct sw_flow_mask *mask)
-{
-       return ovs_sw_flow_key_range_actual_size(&mask->range);
-}
-
-static inline u16
-ovs_sw_flow_mask_size_roundup(const struct sw_flow_mask *mask)
-{
-       return roundup(ovs_sw_flow_mask_actual_size(mask), sizeof(u32));
-}
-
 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void);
 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *);
 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *, bool deferred);
index fdd2005..6e248c5 100644 (file)
 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
 #endif
 
+#ifndef rounddown
+#define rounddown(x, y) (                              \
+{                                                      \
+       typeof(x) __x = (x);                            \
+       __x - (__x % (y));                              \
+}                                                      \
+)
+#endif
+
 #endif /* linux/kernel.h */
index f2b90db..ef0a3ce 100644 (file)
@@ -295,7 +295,7 @@ struct ofp10_match {
 OFP_ASSERT(sizeof(struct ofp10_match) == 40);
 
 enum ofp10_flow_mod_flags {
-    OFPFF10_EMERG       = 1 << 2   /* Ramark this is for emergency. */
+    OFPFF10_EMERG       = 1 << 2 /* Part of "emergency flow cache". */
 };
 
 /* Flow setup and teardown (controller -> datapath). */
index d07c1e8..d5644a1 100644 (file)
@@ -39,11 +39,39 @@ typedef __be16 ovs_be16;
 typedef __be32 ovs_be32;
 typedef __be64 ovs_be64;
 \f
-/* Netlink and OpenFlow both contain 64-bit values that are only guaranteed to
- * be aligned on 32-bit boundaries.  These types help.
+/* These types help with a few funny situations:
+ *
+ *   - The Ethernet header is 14 bytes long, which misaligns everything after
+ *     that.  One can put 2 "shim" bytes before the Ethernet header, but this
+ *     helps only if there is exactly one Ethernet header.  If there are two,
+ *     as with GRE and VXLAN (and if the inner header doesn't use this
+ *     trick--GRE and VXLAN don't) then you have the choice of aligning the
+ *     inner data or the outer data.  So it seems better to treat 32-bit fields
+ *     in protocol headers as aligned only on 16-bit boundaries.
+ *
+ *   - ARP headers contain misaligned 32-bit fields.
+ *
+ *   - Netlink and OpenFlow contain 64-bit values that are only guaranteed to
+ *     be aligned on 32-bit boundaries.
  *
  * lib/unaligned.h has helper functions for accessing these. */
 
+/* A 32-bit value, in host byte order, that is only aligned on a 16-bit
+ * boundary.  */
+typedef struct {
+#ifdef WORDS_BIGENDIAN
+        uint16_t hi, lo;
+#else
+        uint16_t lo, hi;
+#endif
+} ovs_16aligned_u32;
+
+/* A 32-bit value, in network byte order, that is only aligned on a 16-bit
+ * boundary. */
+typedef struct {
+        ovs_be16 hi, lo;
+} ovs_16aligned_be32;
+
 /* A 64-bit value, in host byte order, that is only aligned on a 32-bit
  * boundary.  */
 typedef struct {
index 6843a6d..fce8423 100644 (file)
@@ -129,6 +129,8 @@ lib_libopenvswitch_a_SOURCES = \
        lib/ofpbuf.c \
        lib/ofpbuf.h \
        lib/ovs-atomic-c11.h \
+       lib/ovs-atomic-clang.h \
+       lib/ovs-atomic-flag-gcc4.7+.h \
        lib/ovs-atomic-gcc4+.c \
        lib/ovs-atomic-gcc4+.h \
        lib/ovs-atomic-gcc4.7+.h \
index 7da6fd9..6c9e920 100644 (file)
--- a/lib/bfd.c
+++ b/lib/bfd.c
@@ -39,6 +39,7 @@
 #include "random.h"
 #include "smap.h"
 #include "timeval.h"
+#include "unaligned.h"
 #include "unixctl.h"
 #include "util.h"
 #include "vlog.h"
@@ -536,8 +537,9 @@ bfd_put_packet(struct bfd *bfd, struct ofpbuf *p,
     ip->ip_ttl = MAXTTL;
     ip->ip_tos = IPTOS_LOWDELAY | IPTOS_THROUGHPUT;
     ip->ip_proto = IPPROTO_UDP;
-    ip->ip_src = htonl(0xA9FE0100); /* 169.254.1.0 Link Local. */
-    ip->ip_dst = htonl(0xA9FE0101); /* 169.254.1.1 Link Local. */
+    /* Use link local addresses: */
+    put_16aligned_be32(&ip->ip_src, htonl(0xA9FE0100)); /* 169.254.1.0. */
+    put_16aligned_be32(&ip->ip_dst, htonl(0xA9FE0101)); /* 169.254.1.1. */
     ip->ip_csum = csum(ip, sizeof *ip);
 
     udp = ofpbuf_put_zeros(p, sizeof *udp);
index 556278f..93ee977 100644 (file)
@@ -89,7 +89,7 @@ cls_rule_init_from_minimatch(struct cls_rule *rule,
 
 /* Initializes 'dst' as a copy of 'src'.
  *
- * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
+ * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
 void
 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
 {
@@ -97,6 +97,16 @@ cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
     dst->priority = src->priority;
 }
 
+/* Initializes 'dst' with the data in 'src', destroying 'src'.
+ *
+ * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
+void
+cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
+{
+    minimatch_move(&dst->match, &src->match);
+    dst->priority = src->priority;
+}
+
 /* Frees memory referenced by 'rule'.  Doesn't free 'rule' itself (it's
  * normally embedded into a larger structure).
  *
index 3f89196..5a45458 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -86,6 +86,7 @@ void cls_rule_init(struct cls_rule *, const struct match *,
 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
                                   unsigned int priority);
 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
+void cls_rule_move(struct cls_rule *dst, struct cls_rule *src);
 void cls_rule_destroy(struct cls_rule *);
 
 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
index fb4d46c..ccc949b 100644 (file)
@@ -20,6 +20,9 @@
 #ifndef __has_feature
   #define __has_feature(x) 0
 #endif
+#ifndef __has_extension
+  #define __has_extension(x) 0
+#endif
 
 #if __GNUC__ && !__CHECKER__
 #define NO_RETURN __attribute__((__noreturn__))
index fb32a53..a9334fe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -116,14 +116,15 @@ recalc_csum32(ovs_be16 old_csum, ovs_be32 old_u32, ovs_be32 new_u32)
  * contained 'old_csum' and in which a field that contained 'old_u32[4]' was
  * changed to contain 'new_u32[4]'. */
 ovs_be16
-recalc_csum128(ovs_be16 old_csum, ovs_be32 old_u32[4],
+recalc_csum128(ovs_be16 old_csum, ovs_16aligned_be32 old_u32[4],
                const ovs_be32 new_u32[4])
 {
     ovs_be16 new_csum = old_csum;
     int i;
 
     for (i = 0; i < 4; ++i) {
-        new_csum = recalc_csum32(new_csum, old_u32[i], new_u32[i]);
+        new_csum = recalc_csum32(new_csum,
+                                 get_16aligned_be32(&old_u32[i]), new_u32[i]);
     }
     return new_csum;
 }
index 6382d29..df4b19d 100644 (file)
@@ -28,7 +28,7 @@ uint32_t csum_continue(uint32_t partial, const void *, size_t);
 ovs_be16 csum_finish(uint32_t partial);
 ovs_be16 recalc_csum16(ovs_be16 old_csum, ovs_be16 old_u16, ovs_be16 new_u16);
 ovs_be16 recalc_csum32(ovs_be16 old_csum, ovs_be32 old_u32, ovs_be32 new_u32);
-ovs_be16 recalc_csum128(ovs_be16 old_csum, ovs_be32 old_u32[4],
+ovs_be16 recalc_csum128(ovs_be16 old_csum, ovs_16aligned_be32 old_u32[4],
                         const ovs_be32 new_u32[4]);
 
 #endif /* csum.h */
index ab49b80..8263672 100644 (file)
@@ -164,7 +164,7 @@ parse_ethertype(struct ofpbuf *b)
 static int
 parse_ipv6(struct ofpbuf *packet, struct flow *flow)
 {
-    const struct ip6_hdr *nh;
+    const struct ovs_16aligned_ip6_hdr *nh;
     ovs_be32 tc_flow;
     int nexthdr;
 
@@ -175,10 +175,10 @@ parse_ipv6(struct ofpbuf *packet, struct flow *flow)
 
     nexthdr = nh->ip6_nxt;
 
-    flow->ipv6_src = nh->ip6_src;
-    flow->ipv6_dst = nh->ip6_dst;
+    memcpy(&flow->ipv6_src, &nh->ip6_src, sizeof flow->ipv6_src);
+    memcpy(&flow->ipv6_dst, &nh->ip6_dst, sizeof flow->ipv6_dst);
 
-    tc_flow = get_unaligned_be32(&nh->ip6_flow);
+    tc_flow = get_16aligned_be32(&nh->ip6_flow);
     flow->nw_tos = ntohl(tc_flow) >> 20;
     flow->ipv6_label = tc_flow & htonl(IPV6_LABEL_MASK);
     flow->nw_ttl = nh->ip6_hlim;
@@ -226,7 +226,7 @@ parse_ipv6(struct ofpbuf *packet, struct flow *flow)
                return EINVAL;
             }
         } else if (nexthdr == IPPROTO_FRAGMENT) {
-            const struct ip6_frag *frag_hdr = packet->data;
+            const struct ovs_16aligned_ip6_frag *frag_hdr = packet->data;
 
             nexthdr = frag_hdr->ip6f_nxt;
             if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
@@ -429,8 +429,8 @@ flow_extract(struct ofpbuf *packet, uint32_t skb_priority, uint32_t pkt_mark,
         if (nh) {
             packet->l4 = b.data;
 
-            flow->nw_src = get_unaligned_be32(&nh->ip_src);
-            flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
+            flow->nw_src = get_16aligned_be32(&nh->ip_src);
+            flow->nw_dst = get_16aligned_be32(&nh->ip_dst);
             flow->nw_proto = nh->ip_proto;
 
             flow->nw_tos = nh->ip_tos;
@@ -488,8 +488,8 @@ flow_extract(struct ofpbuf *packet, uint32_t skb_priority, uint32_t pkt_mark,
                 flow->nw_proto = ntohs(arp->ar_op);
             }
 
-            flow->nw_src = arp->ar_spa;
-            flow->nw_dst = arp->ar_tpa;
+            flow->nw_src = get_16aligned_be32(&arp->ar_spa);
+            flow->nw_dst = get_16aligned_be32(&arp->ar_tpa);
             memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
             memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
         }
@@ -996,8 +996,8 @@ flow_compose(struct ofpbuf *b, const struct flow *flow)
         ip->ip_tos = flow->nw_tos;
         ip->ip_ttl = flow->nw_ttl;
         ip->ip_proto = flow->nw_proto;
-        ip->ip_src = flow->nw_src;
-        ip->ip_dst = flow->nw_dst;
+        put_16aligned_be32(&ip->ip_src, flow->nw_src);
+        put_16aligned_be32(&ip->ip_dst, flow->nw_dst);
 
         if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
             ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
@@ -1055,8 +1055,8 @@ flow_compose(struct ofpbuf *b, const struct flow *flow)
 
         if (flow->nw_proto == ARP_OP_REQUEST ||
             flow->nw_proto == ARP_OP_REPLY) {
-            arp->ar_spa = flow->nw_src;
-            arp->ar_tpa = flow->nw_dst;
+            put_16aligned_be32(&arp->ar_spa, flow->nw_src);
+            put_16aligned_be32(&arp->ar_tpa, flow->nw_dst);
             memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
             memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
         }
@@ -1136,6 +1136,21 @@ miniflow_clone(struct miniflow *dst, const struct miniflow *src)
     memcpy(dst->values, src->values, n * sizeof *dst->values);
 }
 
+/* Initializes 'dst' with the data in 'src', destroying 'src'.
+ * The caller must eventually free 'dst' with miniflow_destroy(). */
+void
+miniflow_move(struct miniflow *dst, struct miniflow *src)
+{
+    int n = miniflow_n_values(src);
+    if (n <= MINI_N_INLINE) {
+        dst->values = dst->inline_values;
+        memcpy(dst->values, src->values, n * sizeof *dst->values);
+    } else {
+        dst->values = src->values;
+    }
+    memcpy(dst->map, src->map, sizeof dst->map);
+}
+
 /* Frees any memory owned by 'flow'.  Does not free the storage in which 'flow'
  * itself resides; the caller is responsible for that. */
 void
@@ -1353,6 +1368,14 @@ minimask_clone(struct minimask *dst, const struct minimask *src)
     miniflow_clone(&dst->masks, &src->masks);
 }
 
+/* Initializes 'dst' with the data in 'src', destroying 'src'.
+ * The caller must eventually free 'dst' with minimask_destroy(). */
+void
+minimask_move(struct minimask *dst, struct minimask *src)
+{
+    miniflow_clone(&dst->masks, &src->masks);
+}
+
 /* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
  *
  * The caller must provide room for FLOW_U32S "uint32_t"s in 'storage', for use
index ecd850a..75d95e8 100644 (file)
@@ -321,6 +321,7 @@ struct miniflow {
 
 void miniflow_init(struct miniflow *, const struct flow *);
 void miniflow_clone(struct miniflow *, const struct miniflow *);
+void miniflow_move(struct miniflow *dst, struct miniflow *);
 void miniflow_destroy(struct miniflow *);
 
 void miniflow_expand(const struct miniflow *, struct flow *);
@@ -350,6 +351,7 @@ struct minimask {
 
 void minimask_init(struct minimask *, const struct flow_wildcards *);
 void minimask_clone(struct minimask *, const struct minimask *);
+void minimask_move(struct minimask *dst, struct minimask *src);
 void minimask_combine(struct minimask *dst,
                       const struct minimask *a, const struct minimask *b,
                       uint32_t storage[FLOW_U32S]);
index 49d9efd..68d95cb 100644 (file)
@@ -97,12 +97,23 @@ learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
     learn->hard_timeout = ntohs(nal->hard_timeout);
     learn->priority = ntohs(nal->priority);
     learn->cookie = ntohll(nal->cookie);
-    learn->flags = ntohs(nal->flags);
     learn->table_id = nal->table_id;
     learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
     learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
 
-    if (learn->flags & ~OFPFF_SEND_FLOW_REM || learn->table_id == 0xff) {
+    /* We only support "send-flow-removed" for now. */
+    switch (ntohs(nal->flags)) {
+    case 0:
+        learn->flags = 0;
+        break;
+    case OFPFF_SEND_FLOW_REM:
+        learn->flags = OFPUTIL_FF_SEND_FLOW_REM;
+        break;
+    default:
+        return OFPERR_OFPBAC_BAD_ARGUMENT;
+    }
+
+    if (learn->table_id == 0xff) {
         return OFPERR_OFPBAC_BAD_ARGUMENT;
     }
 
index 9c1aff7..336257c 100644 (file)
@@ -89,6 +89,7 @@ struct lswitch {
      * to set up the flow table. */
     const struct ofputil_flow_mod *default_flows;
     size_t n_default_flows;
+    enum ofputil_protocol usable_protocols;
 };
 
 /* The log messages here could actually be useful in debugging, so keep the
@@ -161,6 +162,7 @@ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
 
     sw->default_flows = cfg->default_flows;
     sw->n_default_flows = cfg->n_default_flows;
+    sw->usable_protocols = cfg->usable_protocols;
 
     sw->queued = rconn_packet_counter_create();
 
@@ -176,7 +178,6 @@ lswitch_handshake(struct lswitch *sw)
 
     protocol = ofputil_protocol_from_ofp_version(rconn_get_version(sw->rconn));
     if (sw->default_flows) {
-        enum ofputil_protocol usable_protocols;
         struct ofpbuf *msg = NULL;
         int error = 0;
         size_t i;
@@ -188,10 +189,8 @@ lswitch_handshake(struct lswitch *sw)
          * This could be improved by actually negotiating a mutually acceptable
          * flow format with the switch, but that would require an asynchronous
          * state machine.  This version ought to work fine in practice. */
-        usable_protocols = ofputil_flow_mod_usable_protocols(
-            sw->default_flows, sw->n_default_flows);
-        if (!(protocol & usable_protocols)) {
-            enum ofputil_protocol want = rightmost_1bit(usable_protocols);
+        if (!(protocol & sw->usable_protocols)) {
+            enum ofputil_protocol want = rightmost_1bit(sw->usable_protocols);
             while (!error) {
                 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
                 if (!msg) {
@@ -200,7 +199,7 @@ lswitch_handshake(struct lswitch *sw)
                 error = rconn_send(sw->rconn, msg, NULL);
             }
         }
-        if (protocol & usable_protocols) {
+        if (protocol & sw->usable_protocols) {
             for (i = 0; !error && i < sw->n_default_flows; i++) {
                 msg = ofputil_encode_flow_mod(&sw->default_flows[i], protocol);
                 error = rconn_send(sw->rconn, msg, NULL);
index dcfb5a2..b3a5d13 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
+#include "ofp-util.h"
 
 struct ofpbuf;
 struct rconn;
@@ -50,6 +51,7 @@ struct lswitch_config {
      * to set up the flow table. */
     const struct ofputil_flow_mod *default_flows;
     size_t n_default_flows;
+    enum ofputil_protocol usable_protocols;
 
     /* The OpenFlow queue to use by default.  Use UINT32_MAX to avoid
      * specifying a particular queue. */
index c038d48..51ed1b9 100644 (file)
@@ -1104,6 +1104,15 @@ minimatch_clone(struct minimatch *dst, const struct minimatch *src)
     minimask_clone(&dst->mask, &src->mask);
 }
 
+/* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
+ * eventually free 'dst' with minimatch_destroy(). */
+void
+minimatch_move(struct minimatch *dst, struct minimatch *src)
+{
+    miniflow_move(&dst->flow, &src->flow);
+    minimask_move(&dst->mask, &src->mask);
+}
+
 /* Frees any memory owned by 'match'.  Does not free the storage in which
  * 'match' itself resides; the caller is responsible for that. */
 void
index 5788721..7b104ee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -146,6 +146,7 @@ struct minimatch {
 
 void minimatch_init(struct minimatch *, const struct match *);
 void minimatch_clone(struct minimatch *, const struct minimatch *);
+void minimatch_move(struct minimatch *dst, struct minimatch *src);
 void minimatch_destroy(struct minimatch *);
 
 void minimatch_expand(const struct minimatch *, struct match *);
index 902a21d..2f7dfb8 100644 (file)
@@ -55,6 +55,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
         OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_TUN_SRC, "tun_src", NULL,
         MF_FIELD_SIZES(be32),
@@ -64,6 +66,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
         NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_TUN_DST, "tun_dst", NULL,
         MF_FIELD_SIZES(be32),
@@ -73,6 +77,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
         NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_TUN_FLAGS, "tun_flags", NULL,
         MF_FIELD_SIZES(be16),
@@ -82,6 +88,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_NONE,
+        OFPUTIL_P_NONE,
     }, {
         MFF_TUN_TOS, "tun_tos", NULL,
         MF_FIELD_SIZES(u8),
@@ -91,6 +99,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_NONE,
+        OFPUTIL_P_NONE,
     }, {
         MFF_TUN_TTL, "tun_ttl", NULL,
         MF_FIELD_SIZES(u8),
@@ -100,6 +110,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_NONE,
+        OFPUTIL_P_NONE,
     }, {
         MFF_METADATA, "metadata", NULL,
         MF_FIELD_SIZES(be64),
@@ -109,6 +121,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_METADATA, "OXM_OF_METADATA",
         OXM_OF_METADATA, "OXM_OF_METADATA",
+        OFPUTIL_P_NXM_OF11_UP,
+        OFPUTIL_P_NXM_OF11_UP,
     }, {
         MFF_IN_PORT, "in_port", NULL,
         MF_FIELD_SIZES(be16),
@@ -118,6 +132,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
         NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
+        OFPUTIL_P_ANY,   /* OF11+ via mapping to 32 bits. */
+        OFPUTIL_P_NONE,
     }, {
         MFF_IN_PORT_OXM, "in_port_oxm", NULL,
         MF_FIELD_SIZES(be32),
@@ -127,6 +143,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
         OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
+        OFPUTIL_P_OF11_UP,
+        OFPUTIL_P_NONE,
     }, {
         MFF_SKB_PRIORITY, "skb_priority", NULL,
         MF_FIELD_SIZES(be32),
@@ -136,6 +154,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_NONE,
+        OFPUTIL_P_NONE,
     }, {
         MFF_PKT_MARK, "pkt_mark", NULL,
         MF_FIELD_SIZES(be32),
@@ -145,6 +165,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
         NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
 #define REGISTER(IDX)                           \
@@ -157,6 +179,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,                                   \
         NXM_NX_REG(IDX), "NXM_NX_REG" #IDX,     \
         NXM_NX_REG(IDX), "NXM_NX_REG" #IDX,     \
+        OFPUTIL_P_NXM_OXM_ANY,                  \
+        OFPUTIL_P_NXM_OXM_ANY,                  \
     }
 #if FLOW_N_REGS > 0
     REGISTER(0),
@@ -199,6 +223,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
         OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,   /* Bitwise masking only with NXM and OF11+! */
     }, {
         MFF_ETH_DST, "eth_dst", "dl_dst",
         MF_FIELD_SIZES(mac),
@@ -208,6 +234,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
         OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,   /* Bitwise masking only with NXM and OF11+! */
     }, {
         MFF_ETH_TYPE, "eth_type", "dl_type",
         MF_FIELD_SIZES(be16),
@@ -217,6 +245,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
         OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NONE,
     },
 
     {
@@ -228,6 +258,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
         NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_DL_VLAN, "dl_vlan", NULL,
         sizeof(ovs_be16), 12,
@@ -237,6 +269,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_VLAN_VID, "vlan_vid", NULL,
         sizeof(ovs_be16), 12,
@@ -246,6 +280,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
         OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
         1, 3,
@@ -255,6 +291,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         0, NULL,
         0, NULL,
+        OFPUTIL_P_ANY,   /* Will be mapped to NXM and OXM. */
+        OFPUTIL_P_NONE,
     }, {
         MFF_VLAN_PCP, "vlan_pcp", NULL,
         1, 3,
@@ -264,6 +302,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
         OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
+        OFPUTIL_P_ANY,   /* Will be mapped to OF10 and NXM. */
+        OFPUTIL_P_NONE,
     },
 
     /* ## ---- ## */
@@ -278,6 +318,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
         OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
+        OFPUTIL_P_NXM_OF11_UP,
+        OFPUTIL_P_NONE,
     }, {
         MFF_MPLS_TC, "mpls_tc", NULL,
         1, 3,
@@ -287,6 +329,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
         OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
+        OFPUTIL_P_NXM_OF11_UP,
+        OFPUTIL_P_NONE,
     }, {
         MFF_MPLS_BOS, "mpls_bos", NULL,
         1, 1,
@@ -296,6 +340,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
         OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NONE,
     },
 
     /* ## -- ## */
@@ -311,6 +357,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
         OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,
     }, {
         MFF_IPV4_DST, "ip_dst", "nw_dst",
         MF_FIELD_SIZES(be32),
@@ -320,6 +368,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_IP_DST, "NXM_OF_IP_DST",
         OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,
     },
 
     {
@@ -331,6 +381,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
         OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_IPV6_DST, "ipv6_dst", NULL,
         MF_FIELD_SIZES(ipv6),
@@ -340,6 +392,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
         OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
     {
         MFF_IPV6_LABEL, "ipv6_label", NULL,
@@ -350,6 +404,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
         OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     {
@@ -361,6 +417,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
         OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_IP_DSCP, "nw_tos", NULL,
         MF_FIELD_SIZES(u8),
@@ -370,6 +428,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
         NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
+        OFPUTIL_P_ANY,   /* Will be shifted for OXM. */
+        OFPUTIL_P_NONE,
     }, {
         MFF_IP_DSCP_SHIFTED, "nw_tos_shifted", NULL,
         MF_FIELD_SIZES(u8),
@@ -379,6 +439,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
         OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
+        OFPUTIL_P_ANY,   /* Will be shifted for non-OXM. */
+        OFPUTIL_P_NONE,
     }, {
         MFF_IP_ECN, "nw_ecn", NULL,
         1, 2,
@@ -388,6 +450,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
         OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_IP_TTL, "nw_ttl", NULL,
         MF_FIELD_SIZES(u8),
@@ -397,6 +461,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
         NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_IP_FRAG, "ip_frag", NULL,
         1, 2,
@@ -406,6 +472,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
         NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     {
@@ -417,6 +485,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
         OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_ARP_SPA, "arp_spa", NULL,
         MF_FIELD_SIZES(be32),
@@ -426,6 +496,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
         OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,
     }, {
         MFF_ARP_TPA, "arp_tpa", NULL,
         MF_FIELD_SIZES(be32),
@@ -435,6 +507,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
         OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OF11_UP,
     }, {
         MFF_ARP_SHA, "arp_sha", NULL,
         MF_FIELD_SIZES(mac),
@@ -444,6 +518,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
         OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_ARP_THA, "arp_tha", NULL,
         MF_FIELD_SIZES(mac),
@@ -453,6 +529,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
         OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     /* ## -- ## */
@@ -468,6 +546,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
         OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_TCP_DST, "tcp_dst", "tp_dst",
         MF_FIELD_SIZES(be16),
@@ -477,6 +557,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
         OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     {
@@ -488,6 +570,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
         OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_UDP_DST, "udp_dst", NULL,
         MF_FIELD_SIZES(be16),
@@ -497,6 +581,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
         OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     {
@@ -508,6 +594,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
         OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
+        OFPUTIL_P_NXM_OF11_UP,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_SCTP_DST, "sctp_dst", NULL,
         MF_FIELD_SIZES(be16),
@@ -517,6 +605,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         true,
         OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
         OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
+        OFPUTIL_P_NXM_OF11_UP,
+        OFPUTIL_P_NXM_OXM_ANY,
     },
 
     {
@@ -528,6 +618,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
         OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_ICMPV4_CODE, "icmp_code", NULL,
         MF_FIELD_SIZES(u8),
@@ -537,6 +629,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
         OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
+        OFPUTIL_P_ANY,
+        OFPUTIL_P_NONE,
     },
 
     {
@@ -548,6 +642,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
         OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NONE,
     }, {
         MFF_ICMPV6_CODE, "icmpv6_code", NULL,
         MF_FIELD_SIZES(u8),
@@ -557,6 +653,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
         OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NONE,
     },
 
     /* ## ---- ## */
@@ -572,6 +670,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
         OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_ND_SLL, "nd_sll", NULL,
         MF_FIELD_SIZES(mac),
@@ -581,6 +681,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
         OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }, {
         MFF_ND_TLL, "nd_tll", NULL,
         MF_FIELD_SIZES(mac),
@@ -590,6 +692,8 @@ static const struct mf_field mf_fields[MFF_N_IDS] = {
         false,
         NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
         OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
+        OFPUTIL_P_NXM_OXM_ANY,
+        OFPUTIL_P_NXM_OXM_ANY,
     }
 };
 
@@ -1798,17 +1902,17 @@ mf_set_wild(const struct mf_field *mf, struct match *match)
  *
  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
  * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
-void
+enum ofputil_protocol
 mf_set(const struct mf_field *mf,
        const union mf_value *value, const union mf_value *mask,
        struct match *match)
 {
     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
         mf_set_value(mf, value, match);
-        return;
+        return mf->usable_protocols;
     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
         mf_set_wild(mf, match);
-        return;
+        return OFPUTIL_P_ANY;
     }
 
     switch (mf->id) {
@@ -1895,11 +1999,11 @@ mf_set(const struct mf_field *mf,
 
     case MFF_IPV4_SRC:
         match_set_nw_src_masked(match, value->be32, mask->be32);
-        break;
+        goto cidr_check;
 
     case MFF_IPV4_DST:
         match_set_nw_dst_masked(match, value->be32, mask->be32);
-        break;
+        goto cidr_check;
 
     case MFF_IPV6_SRC:
         match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
@@ -1927,11 +2031,11 @@ mf_set(const struct mf_field *mf,
 
     case MFF_ARP_SPA:
         match_set_nw_src_masked(match, value->be32, mask->be32);
-        break;
+        goto cidr_check;
 
     case MFF_ARP_TPA:
         match_set_nw_dst_masked(match, value->be32, mask->be32);
-        break;
+        goto cidr_check;
 
     case MFF_TCP_SRC:
     case MFF_UDP_SRC:
@@ -1949,6 +2053,12 @@ mf_set(const struct mf_field *mf,
     default:
         NOT_REACHED();
     }
+
+    return mf->usable_protocols_bitwise;
+
+cidr_check:
+    return ip_is_cidr(mask->be32) ? mf->usable_protocols :
+            mf->usable_protocols_bitwise;
 }
 
 static enum ofperr
index 969ca9e..dd8b95d 100644 (file)
@@ -22,6 +22,7 @@
 #include <netinet/ip6.h>
 #include "flow.h"
 #include "ofp-errors.h"
+#include "ofp-util.h"
 #include "packets.h"
 
 struct ds;
@@ -280,7 +281,18 @@ struct mf_field {
     uint32_t nxm_header;        /* An NXM_* (or OXM_*) constant. */
     const char *nxm_name;       /* The nxm_header constant's name. */
     uint32_t oxm_header;        /* An OXM_* (or NXM_*) constant. */
-    const char *oxm_name;          /* The oxm_header constant's name */
+    const char *oxm_name;       /* The oxm_header constant's name */
+
+    /* Usable protocols.
+     * NXM and OXM are extensible, allowing later extensions to be sent in
+     * earlier protocol versions, so this does not necessarily correspond to
+     * the OpenFlow protocol version the field was introduced in.
+     * Also, some field types are tranparently mapped to each other via the
+     * struct flow (like vlan and dscp/tos fields), so each variant supports
+     * all protocols. */
+    enum ofputil_protocol usable_protocols; /* If fully/cidr masked. */
+    /* If partially/non-cidr masked. */
+    enum ofputil_protocol usable_protocols_bitwise;
 };
 
 /* The representation of a field's value. */
@@ -344,9 +356,12 @@ bool mf_is_zero(const struct mf_field *, const struct flow *);
 
 void mf_get(const struct mf_field *, const struct match *,
             union mf_value *value, union mf_value *mask);
-void mf_set(const struct mf_field *,
-            const union mf_value *value, const union mf_value *mask,
-            struct match *);
+
+/* Returns the set of usable protocols. */
+enum ofputil_protocol mf_set(const struct mf_field *,
+                             const union mf_value *value,
+                             const union mf_value *mask,
+                             struct match *);
 
 void mf_set_wild(const struct mf_field *, struct match *);
 
index 7c19483..fdea10b 100644 (file)
@@ -1510,7 +1510,9 @@ const struct netdev_class netdev_bsd_class = {
     NULL, /* set_queue */
     NULL, /* delete_queue */
     NULL, /* get_queue_stats */
-    NULL, /* dump_queue */
+    NULL, /* queue_dump_start */
+    NULL, /* queue_dump_next */
+    NULL, /* queue_dump_done */
     NULL, /* dump_queue_stats */
 
     netdev_bsd_get_in4,
@@ -1573,7 +1575,9 @@ const struct netdev_class netdev_tap_class = {
     NULL, /* set_queue */
     NULL, /* delete_queue */
     NULL, /* get_queue_stats */
-    NULL, /* dump_queue */
+    NULL, /* queue_dump_start */
+    NULL, /* queue_dump_next */
+    NULL, /* queue_dump_done */
     NULL, /* dump_queue_stats */
 
     netdev_bsd_get_in4,
index ac26564..16a6b0b 100644 (file)
@@ -708,7 +708,9 @@ static const struct netdev_class dummy_class = {
     NULL,                       /* set_queue */
     NULL,                       /* delete_queue */
     NULL,                       /* get_queue_stats */
-    NULL,                       /* dump_queues */
+    NULL,                       /* queue_dump_start */
+    NULL,                       /* queue_dump_next */
+    NULL,                       /* queue_dump_done */
     NULL,                       /* dump_queue_stats */
 
     NULL,                       /* get_in4 */
index 80fa39b..26fb32d 100644 (file)
@@ -2100,35 +2100,35 @@ start_queue_dump(const struct netdev *netdev, struct nl_dump *dump)
     return true;
 }
 
+struct netdev_linux_queue_state {
+    unsigned int *queues;
+    size_t cur_queue;
+    size_t n_queues;
+};
+
 static int
-netdev_linux_dump_queues(const struct netdev *netdev_,
-                         netdev_dump_queues_cb *cb, void *aux)
+netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep)
 {
-    struct netdev_linux *netdev = netdev_linux_cast(netdev_);
+    const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
     int error;
 
     ovs_mutex_lock(&netdev->mutex);
     error = tc_query_qdisc(netdev_);
     if (!error) {
         if (netdev->tc->ops->class_get) {
-            struct tc_queue *queue, *next_queue;
-            struct smap details;
-
-            smap_init(&details);
-            HMAP_FOR_EACH_SAFE (queue, next_queue, hmap_node,
-                                &netdev->tc->queues) {
-                int retval;
-
-                smap_clear(&details);
-
-                retval = netdev->tc->ops->class_get(netdev_, queue, &details);
-                if (!retval) {
-                    (*cb)(queue->queue_id, &details, aux);
-                } else {
-                    error = retval;
-                }
+            struct netdev_linux_queue_state *state;
+            struct tc_queue *queue;
+            size_t i;
+
+            *statep = state = xmalloc(sizeof *state);
+            state->n_queues = hmap_count(&netdev->tc->queues);
+            state->cur_queue = 0;
+            state->queues = xmalloc(state->n_queues * sizeof *state->queues);
+
+            i = 0;
+            HMAP_FOR_EACH (queue, hmap_node, &netdev->tc->queues) {
+                state->queues[i++] = queue->queue_id;
             }
-            smap_destroy(&details);
         } else {
             error = EOPNOTSUPP;
         }
@@ -2138,6 +2138,41 @@ netdev_linux_dump_queues(const struct netdev *netdev_,
     return error;
 }
 
+static int
+netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_,
+                             unsigned int *queue_idp, struct smap *details)
+{
+    const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
+    struct netdev_linux_queue_state *state = state_;
+    int error = EOF;
+
+    ovs_mutex_lock(&netdev->mutex);
+    while (state->cur_queue < state->n_queues) {
+        unsigned int queue_id = state->queues[state->cur_queue++];
+        struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
+
+        if (queue) {
+            *queue_idp = queue_id;
+            error = netdev->tc->ops->class_get(netdev_, queue, details);
+            break;
+        }
+    }
+    ovs_mutex_unlock(&netdev->mutex);
+
+    return error;
+}
+
+static int
+netdev_linux_queue_dump_done(const struct netdev *netdev OVS_UNUSED,
+                             void *state_)
+{
+    struct netdev_linux_queue_state *state = state_;
+
+    free(state->queues);
+    free(state);
+    return 0;
+}
+
 static int
 netdev_linux_dump_queue_stats(const struct netdev *netdev_,
                               netdev_dump_queue_stats_cb *cb, void *aux)
@@ -2580,7 +2615,9 @@ netdev_linux_change_seq(const struct netdev *netdev_)
     netdev_linux_set_queue,                                     \
     netdev_linux_delete_queue,                                  \
     netdev_linux_get_queue_stats,                               \
-    netdev_linux_dump_queues,                                   \
+    netdev_linux_queue_dump_start,                              \
+    netdev_linux_queue_dump_next,                               \
+    netdev_linux_queue_dump_done,                               \
     netdev_linux_dump_queue_stats,                              \
                                                                 \
     netdev_linux_get_in4,                                       \
index 23905d4..2442b77 100644 (file)
@@ -478,22 +478,39 @@ struct netdev_class {
     int (*get_queue_stats)(const struct netdev *netdev, unsigned int queue_id,
                            struct netdev_queue_stats *stats);
 
-    /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's
-     * ID, its configuration, and the 'aux' specified by the caller.  The order
-     * of iteration is unspecified, but (when successful) each queue is visited
-     * exactly once.
-     *
-     * 'cb' will not modify or free the 'details' argument passed in.  It may
-     * delete or modify the queue passed in as its 'queue_id' argument.  It may
-     * modify but will not delete any other queue within 'netdev'.  If 'cb'
-     * adds new queues, then ->dump_queues is allowed to visit some queues
-     * twice or not at all.
-     */
-    int (*dump_queues)(const struct netdev *netdev,
-                       void (*cb)(unsigned int queue_id,
-                                  const struct smap *details,
-                                  void *aux),
-                       void *aux);
+    /* Attempts to begin dumping the queues in 'netdev'.  On success, returns 0
+     * and initializes '*statep' with any data needed for iteration.  On
+     * failure, returns a positive errno value.
+     *
+     * May be NULL if 'netdev' does not support QoS at all. */
+    int (*queue_dump_start)(const struct netdev *netdev, void **statep);
+
+    /* Attempts to retrieve another queue from 'netdev' for 'state', which was
+     * initialized by a successful call to the 'queue_dump_start' function for
+     * 'netdev'.  On success, stores a queue ID into '*queue_id' and fills
+     * 'details' with the configuration of the queue with that ID.  Returns EOF
+     * if the last queue has been dumped, or a positive errno value on error.
+     * This function will not be called again once it returns nonzero once for
+     * a given iteration (but the 'queue_dump_done' function will be called
+     * afterward).
+     *
+     * The caller initializes and clears 'details' before calling this
+     * function.  The caller takes ownership of the string key-values pairs
+     * added to 'details'.
+     *
+     * The returned contents of 'details' should be documented as valid for the
+     * given 'type' in the "other_config" column in the "Queue" table in
+     * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
+     *
+     * May be NULL if 'netdev' does not support QoS at all. */
+    int (*queue_dump_next)(const struct netdev *netdev, void *state,
+                           unsigned int *queue_id, struct smap *details);
+
+    /* Releases resources from 'netdev' for 'state', which was initialized by a
+     * successful call to the 'queue_dump_start' function for 'netdev'.
+     *
+     * May be NULL if 'netdev' does not support QoS at all. */
+    int (*queue_dump_done)(const struct netdev *netdev, void *state);
 
     /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's
      * ID, its statistics, and the 'aux' specified by the caller.  The order of
index 0f5dd7a..af50597 100644 (file)
@@ -727,7 +727,9 @@ get_stats(const struct netdev *netdev, struct netdev_stats *stats)
     NULL,                       /* set_queue */             \
     NULL,                       /* delete_queue */          \
     NULL,                       /* get_queue_stats */       \
-    NULL,                       /* dump_queues */           \
+    NULL,                       /* queue_dump_start */      \
+    NULL,                       /* queue_dump_next */       \
+    NULL,                       /* queue_dump_done */       \
     NULL,                       /* dump_queue_stats */      \
                                                             \
     NULL,                       /* get_in4 */               \
index b1dfc08..74f5f53 100644 (file)
@@ -1400,30 +1400,78 @@ netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
     return retval;
 }
 
-/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
- * its configuration, and the 'aux' specified by the caller.  The order of
- * iteration is unspecified, but (when successful) each queue is visited
- * exactly once.
+/* Initializes 'dump' to begin dumping the queues in a netdev.
+ *
+ * This function provides no status indication.  An error status for the entire
+ * dump operation is provided when it is completed by calling
+ * netdev_queue_dump_done().
+ */
+void
+netdev_queue_dump_start(struct netdev_queue_dump *dump,
+                        const struct netdev *netdev)
+{
+    dump->netdev = netdev_ref(netdev);
+    if (netdev->netdev_class->queue_dump_start) {
+        dump->error = netdev->netdev_class->queue_dump_start(netdev,
+                                                             &dump->state);
+    } else {
+        dump->error = EOPNOTSUPP;
+    }
+}
+
+/* Attempts to retrieve another queue from 'dump', which must have been
+ * initialized with netdev_queue_dump_start().  On success, stores a new queue
+ * ID into '*queue_id', fills 'details' with configuration details for the
+ * queue, and returns true.  On failure, returns false.
  *
- * Calling this function may be more efficient than calling netdev_get_queue()
- * for every queue.
+ * Queues are not necessarily dumped in increasing order of queue ID (or any
+ * other predictable order).
  *
- * 'cb' must not modify or free the 'details' argument passed in.  It may
- * delete or modify the queue passed in as its 'queue_id' argument.  It may
- * modify but must not delete any other queue within 'netdev'.  'cb' should not
- * add new queues because this may cause some queues to be visited twice or not
- * at all.
+ * Failure might indicate an actual error or merely that the last queue has
+ * been dumped.  An error status for the entire dump operation is provided when
+ * it is completed by calling netdev_queue_dump_done().
  *
- * Returns 0 if successful, otherwise a positive errno value.  On error, some
- * configured queues may not have been included in the iteration. */
+ * The returned contents of 'details' should be documented as valid for the
+ * given 'type' in the "other_config" column in the "Queue" table in
+ * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
+ *
+ * The caller must initialize 'details' (e.g. with smap_init()) before calling
+ * this function.  This function will clear and replace its contents.  The
+ * caller must free 'details' when it is no longer needed (e.g. with
+ * smap_destroy()). */
+bool
+netdev_queue_dump_next(struct netdev_queue_dump *dump,
+                       unsigned int *queue_id, struct smap *details)
+{
+    const struct netdev *netdev = dump->netdev;
+
+    if (dump->error) {
+        return false;
+    }
+
+    dump->error = netdev->netdev_class->queue_dump_next(netdev, dump->state,
+                                                        queue_id, details);
+
+    if (dump->error) {
+        netdev->netdev_class->queue_dump_done(netdev, dump->state);
+        return false;
+    }
+    return true;
+}
+
+/* Completes queue table dump operation 'dump', which must have been
+ * initialized with netdev_queue_dump_start().  Returns 0 if the dump operation
+ * was error-free, otherwise a positive errno value describing the problem. */
 int
-netdev_dump_queues(const struct netdev *netdev,
-                   netdev_dump_queues_cb *cb, void *aux)
+netdev_queue_dump_done(struct netdev_queue_dump *dump)
 {
-    const struct netdev_class *class = netdev->netdev_class;
-    return (class->dump_queues
-            ? class->dump_queues(netdev, cb, aux)
-            : EOPNOTSUPP);
+    const struct netdev *netdev = dump->netdev;
+    if (!dump->error && netdev->netdev_class->queue_dump_done) {
+        dump->error = netdev->netdev_class->queue_dump_done(netdev,
+                                                            dump->state);
+    }
+    netdev_close(dump->netdev);
+    return dump->error == EOF ? 0 : dump->error;
 }
 
 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
index 287f6cc..bafa50e 100644 (file)
@@ -47,7 +47,17 @@ extern "C" {
  *      These functions are conditionally thread-safe: they may be called from
  *      different threads only on different netdev_rx objects.  (The client may
  *      create multiple netdev_rx objects for a single netdev and access each
- *      of those from a different thread.) */
+ *      of those from a different thread.)
+ *
+ *    NETDEV_FOR_EACH_QUEUE
+ *    netdev_queue_dump_next()
+ *    netdev_queue_dump_done()
+ *
+ *      These functions are conditionally thread-safe: they may be called from
+ *      different threads only on different netdev_queue_dump objects.  (The
+ *      client may create multiple netdev_queue_dump objects for a single
+ *      netdev and access each of those from a different thread.)
+ */
 
 struct netdev;
 struct netdev_class;
@@ -271,10 +281,31 @@ int netdev_delete_queue(struct netdev *, unsigned int queue_id);
 int netdev_get_queue_stats(const struct netdev *, unsigned int queue_id,
                            struct netdev_queue_stats *);
 
-typedef void netdev_dump_queues_cb(unsigned int queue_id,
-                                   const struct smap *details, void *aux);
-int netdev_dump_queues(const struct netdev *,
-                       netdev_dump_queues_cb *, void *aux);
+struct netdev_queue_dump {
+    struct netdev *netdev;
+    int error;
+    void *state;
+};
+void netdev_queue_dump_start(struct netdev_queue_dump *,
+                             const struct netdev *);
+bool netdev_queue_dump_next(struct netdev_queue_dump *,
+                            unsigned int *queue_id, struct smap *details);
+int netdev_queue_dump_done(struct netdev_queue_dump *);
+
+/* Iterates through each queue in NETDEV, using DUMP as state.  Fills QUEUE_ID
+ * and DETAILS with information about queues.  The client must initialize and
+ * destroy DETAILS.
+ *
+ * Arguments all have pointer type.
+ *
+ * If you break out of the loop, then you need to free the dump structure by
+ * hand using netdev_queue_dump_done(). */
+#define NETDEV_QUEUE_FOR_EACH(QUEUE_ID, DETAILS, DUMP, NETDEV)  \
+    for (netdev_queue_dump_start(DUMP, NETDEV);                 \
+         (netdev_queue_dump_next(DUMP, QUEUE_ID, DETAILS)       \
+          ? true                                                \
+          : (netdev_queue_dump_done(DUMP), false));             \
+        )
 
 typedef void netdev_dump_queue_stats_cb(unsigned int queue_id,
                                         struct netdev_queue_stats *,
index ca33ca8..101c33d 100644 (file)
@@ -414,7 +414,7 @@ struct ofpact_learn {
     uint16_t hard_timeout;      /* Max time before discarding (seconds). */
     uint16_t priority;          /* Priority level of flow entry. */
     uint64_t cookie;            /* Cookie for new flow. */
-    uint16_t flags;             /* Either 0 or OFPFF_SEND_FLOW_REM. */
+    enum ofputil_flow_mod_flags flags;
     uint8_t table_id;           /* Table to insert flow entry. */
     uint16_t fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
     uint16_t fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
index 79acd30..c80a75e 100644 (file)
@@ -349,7 +349,8 @@ enum ofperr {
     /* OF1.0(3,4), OF1.1+(5,6).  Unsupported or unknown command. */
     OFPERR_OFPFMFC_BAD_COMMAND,
 
-    /* OF1.2+(5,7).  Unsupported or unknown flags. */
+    /* NX1.0(3,258), NX1.1(5,258), OF1.2+(5,7).  Unsupported or unknown
+     * flags. */
     OFPERR_OFPFMFC_BAD_FLAGS,
 
     /* OF1.0(3,5).  Unsupported action list - cannot process in the order
index dd0738c..176f61f 100644 (file)
@@ -464,7 +464,8 @@ parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
-set_field_parse__(char *arg, struct ofpbuf *ofpacts)
+set_field_parse__(char *arg, struct ofpbuf *ofpacts,
+                  enum ofputil_protocol *usable_protocols)
 {
     struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
     char *value;
@@ -502,6 +503,7 @@ set_field_parse__(char *arg, struct ofpbuf *ofpacts)
     }
     ofpact_set_field_init(load, mf, &mf_value);
 
+    *usable_protocols &= mf->usable_protocols;
     return NULL;
 }
 
@@ -511,10 +513,11 @@ set_field_parse__(char *arg, struct ofpbuf *ofpacts)
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
-set_field_parse(const char *arg, struct ofpbuf *ofpacts)
+set_field_parse(const char *arg, struct ofpbuf *ofpacts,
+                enum ofputil_protocol *usable_protocols)
 {
     char *copy = xstrdup(arg);
-    char *error = set_field_parse__(copy, ofpacts);
+    char *error = set_field_parse__(copy, ofpacts, usable_protocols);
     free(copy);
     return error;
 }
@@ -593,7 +596,8 @@ parse_sample(struct ofpbuf *b, char *arg)
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
 parse_named_action(enum ofputil_action_code code,
-                   char *arg, struct ofpbuf *ofpacts)
+                   char *arg, struct ofpbuf *ofpacts,
+                   enum ofputil_protocol *usable_protocols)
 {
     size_t orig_size = ofpacts->size;
     struct ofpact_tunnel *tunnel;
@@ -639,7 +643,7 @@ parse_named_action(enum ofputil_action_code code,
         break;
 
     case OFPUTIL_OFPAT12_SET_FIELD:
-        return set_field_parse(arg, ofpacts);
+        return set_field_parse(arg, ofpacts, usable_protocols);
 
     case OFPUTIL_OFPAT10_STRIP_VLAN:
     case OFPUTIL_OFPAT11_POP_VLAN:
@@ -647,6 +651,7 @@ parse_named_action(enum ofputil_action_code code,
         break;
 
     case OFPUTIL_OFPAT11_PUSH_VLAN:
+        *usable_protocols &= OFPUTIL_P_OF11_UP;
         error = str_to_u16(arg, "ethertype", &ethertype);
         if (error) {
             return error;
@@ -842,11 +847,12 @@ parse_named_action(enum ofputil_action_code code,
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
 str_to_ofpact__(char *pos, char *act, char *arg,
-                struct ofpbuf *ofpacts, int n_actions)
+                struct ofpbuf *ofpacts, int n_actions,
+                enum ofputil_protocol *usable_protocols)
 {
     int code = ofputil_action_code_from_name(act);
     if (code >= 0) {
-        return parse_named_action(code, arg, ofpacts);
+        return parse_named_action(code, arg, ofpacts, usable_protocols);
     } else if (!strcasecmp(act, "drop")) {
         if (n_actions) {
             return xstrdup("Drop actions must not be preceded by other "
@@ -872,7 +878,8 @@ str_to_ofpact__(char *pos, char *act, char *arg,
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
-str_to_ofpacts(char *str, struct ofpbuf *ofpacts)
+str_to_ofpacts(char *str, struct ofpbuf *ofpacts,
+               enum ofputil_protocol *usable_protocols)
 {
     size_t orig_size = ofpacts->size;
     char *pos, *act, *arg;
@@ -882,7 +889,8 @@ str_to_ofpacts(char *str, struct ofpbuf *ofpacts)
     pos = str;
     n_actions = 0;
     while (ofputil_parse_key_value(&pos, &act, &arg)) {
-        char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions);
+        char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions,
+                                      usable_protocols);
         if (error) {
             ofpacts->size = orig_size;
             return error;
@@ -907,11 +915,14 @@ str_to_ofpacts(char *str, struct ofpbuf *ofpacts)
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
 parse_named_instruction(enum ovs_instruction_type type,
-                        char *arg, struct ofpbuf *ofpacts)
+                        char *arg, struct ofpbuf *ofpacts,
+                        enum ofputil_protocol *usable_protocols)
 {
     char *error_s = NULL;
     enum ofperr error;
 
+    *usable_protocols &= OFPUTIL_P_OF11_UP;
+
     switch (type) {
     case OVSINST_OFPIT11_APPLY_ACTIONS:
         NOT_REACHED();  /* This case is handled by str_to_inst_ofpacts() */
@@ -927,10 +938,12 @@ parse_named_instruction(enum ovs_instruction_type type,
         break;
 
     case OVSINST_OFPIT13_METER:
+        *usable_protocols &= OFPUTIL_P_OF13_UP;
         error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
         break;
 
     case OVSINST_OFPIT11_WRITE_METADATA:
+        *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
         error_s = parse_metadata(ofpacts, arg);
         break;
 
@@ -963,7 +976,8 @@ parse_named_instruction(enum ovs_instruction_type type,
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
-str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts)
+str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts,
+                    enum ofputil_protocol *usable_protocols)
 {
     size_t orig_size = ofpacts->size;
     char *pos, *inst, *arg;
@@ -976,7 +990,8 @@ str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts)
     while (ofputil_parse_key_value(&pos, &inst, &arg)) {
         type = ovs_instruction_type_from_name(inst);
         if (type < 0) {
-            char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions);
+            char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions,
+                                          usable_protocols);
             if (error) {
                 ofpacts->size = orig_size;
                 return error;
@@ -992,7 +1007,8 @@ str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts)
             return xasprintf("%s isn't supported. Just write actions then "
                              "it is interpreted as apply_actions", inst);
         } else {
-            char *error = parse_named_instruction(type, arg, ofpacts);
+            char *error = parse_named_instruction(type, arg, ofpacts,
+                                                  usable_protocols);
             if (error) {
                 ofpacts->size = orig_size;
                 return error;
@@ -1058,25 +1074,28 @@ parse_protocol(const char *name, const struct protocol **p_out)
 }
 
 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
- * 'match' appropriately.
+ * 'match' appropriately.  Restricts the set of usable protocols to ones
+ * supporting the parsed field.
  *
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 static char * WARN_UNUSED_RESULT
-parse_field(const struct mf_field *mf, const char *s, struct match *match)
+parse_field(const struct mf_field *mf, const char *s, struct match *match,
+            enum ofputil_protocol *usable_protocols)
 {
     union mf_value value, mask;
     char *error;
 
     error = mf_parse(mf, s, &value, &mask);
     if (!error) {
-        mf_set(mf, &value, &mask, match);
+        *usable_protocols &= mf_set(mf, &value, &mask, match);
     }
     return error;
 }
 
 static char * WARN_UNUSED_RESULT
-parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
+parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
+                enum ofputil_protocol *usable_protocols)
 {
     enum {
         F_OUT_PORT = 1 << 0,
@@ -1089,6 +1108,8 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
     char *act_str = NULL;
     char *name;
 
+    *usable_protocols = OFPUTIL_P_ANY;
+
     switch (command) {
     case -1:
         fields = F_OUT_PORT;
@@ -1161,15 +1182,18 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
                 match_set_nw_proto(&fm->match, p->nw_proto);
             }
         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
-            fm->flags |= OFPFF_SEND_FLOW_REM;
+            fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
-            fm->flags |= OFPFF_CHECK_OVERLAP;
+            fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
         } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
-            fm->flags |= OFPFF12_RESET_COUNTS;
+            fm->flags |= OFPUTIL_FF_RESET_COUNTS;
+            *usable_protocols &= OFPUTIL_P_OF12_UP;
         } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
-            fm->flags |= OFPFF13_NO_PKT_COUNTS;
+            fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
+            *usable_protocols &= OFPUTIL_P_OF13_UP;
         } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
-            fm->flags |= OFPFF13_NO_BYT_COUNTS;
+            fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
+            *usable_protocols &= OFPUTIL_P_OF13_UP;
         } else {
             char *value;
 
@@ -1180,6 +1204,9 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
 
             if (!strcmp(name, "table")) {
                 error = str_to_u8(value, "table", &fm->table_id);
+                if (fm->table_id != 0xff) {
+                    *usable_protocols &= OFPUTIL_P_TID;
+                }
             } else if (!strcmp(name, "out_port")) {
                 if (!ofputil_port_from_string(value, &fm->out_port)) {
                     error = xasprintf("%s is not a valid OpenFlow port",
@@ -1209,6 +1236,12 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
                         return error;
                     }
                     error = str_to_be64(mask + 1, &fm->cookie_mask);
+
+                    /* Matching of the cookie is only supported through NXM or
+                     * OF1.1+. */
+                    if (fm->cookie_mask != htonll(0)) {
+                        *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
+                    }
                 } else {
                     /* No mask means that the cookie is being set. */
                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
@@ -1219,7 +1252,8 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
                     fm->modify_cookie = true;
                 }
             } else if (mf_from_name(name)) {
-                error = parse_field(mf_from_name(name), value, &fm->match);
+                error = parse_field(mf_from_name(name), value, &fm->match,
+                                    usable_protocols);
             } else if (!strcmp(name, "duration")
                        || !strcmp(name, "n_packets")
                        || !strcmp(name, "n_bytes")
@@ -1237,6 +1271,20 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
             }
         }
     }
+    /* Check for usable protocol interdependencies between match fields. */
+    if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
+        const struct flow_wildcards *wc = &fm->match.wc;
+        /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
+         *
+         * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
+         *  nw_ttl are covered elsewhere so they don't need to be included in
+         *  this test too.)
+         */
+        if (wc->masks.nw_proto || wc->masks.nw_tos
+            || wc->masks.tp_src || wc->masks.tp_dst) {
+            *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
+        }
+    }
     if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
         && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
         /* On modifies without a mask, we are supposed to add a flow if
@@ -1249,7 +1297,7 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
         char *error;
 
         ofpbuf_init(&ofpacts, 32);
-        error = str_to_inst_ofpacts(act_str, &ofpacts);
+        error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols);
         if (!error) {
             enum ofperr err;
 
@@ -1277,6 +1325,7 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
 
 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
+ * Returns the set of usable protocols in '*usable_protocols'.
  *
  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
@@ -1285,12 +1334,13 @@ parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
-parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_)
+parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
+              enum ofputil_protocol *usable_protocols)
 {
     char *string = xstrdup(str_);
     char *error;
 
-    error = parse_ofp_str__(fm, command, string);
+    error = parse_ofp_str__(fm, command, string, usable_protocols);
     if (error) {
         fm->ofpacts = NULL;
         fm->ofpacts_len = 0;
@@ -1302,7 +1352,8 @@ parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_)
 
 static char * WARN_UNUSED_RESULT
 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
-                          struct ofpbuf *bands, int command)
+                          struct ofpbuf *bands, int command,
+                          enum ofputil_protocol *usable_protocols)
 {
     enum {
         F_METER = 1 << 0,
@@ -1313,6 +1364,9 @@ parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
     char *band_str = NULL;
     char *name;
 
+    /* Meters require at least OF 1.3. */
+    *usable_protocols &= OFPUTIL_P_OF13_UP;
+
     switch (command) {
     case -1:
         fields = F_METER;
@@ -1501,7 +1555,7 @@ parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
-                        int command)
+                        int command, enum ofputil_protocol *usable_protocols)
 {
     struct ofpbuf bands;
     char *string;
@@ -1510,7 +1564,8 @@ parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
     ofpbuf_init(&bands, 64);
     string = xstrdup(str_);
 
-    error = parse_ofp_meter_mod_str__(mm, string, &bands, command);
+    error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
+                                      usable_protocols);
 
     free(string);
     ofpbuf_uninit(&bands);
@@ -1520,7 +1575,8 @@ parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
 
 static char * WARN_UNUSED_RESULT
 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
-                             const char *str_, char *string)
+                             const char *str_, char *string,
+                             enum ofputil_protocol *usable_protocols)
 {
     static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
     char *save_ptr = NULL;
@@ -1573,7 +1629,8 @@ parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
             } else if (mf_from_name(name)) {
                 char *error;
 
-                error = parse_field(mf_from_name(name), value, &fmr->match);
+                error = parse_field(mf_from_name(name), value, &fmr->match,
+                                    usable_protocols);
                 if (error) {
                     return error;
                 }
@@ -1592,10 +1649,12 @@ parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
-                           const char *str_)
+                           const char *str_,
+                           enum ofputil_protocol *usable_protocols)
 {
     char *string = xstrdup(str_);
-    char *error = parse_flow_monitor_request__(fmr, str_, string);
+    char *error = parse_flow_monitor_request__(fmr, str_, string,
+                                               usable_protocols);
     free(string);
     return error;
 }
@@ -1606,10 +1665,15 @@ parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
  * Returns NULL if successful, otherwise a malloc()'d string describing the
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
-parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
+parse_ofpacts(const char *s_, struct ofpbuf *ofpacts,
+              enum ofputil_protocol *usable_protocols)
 {
     char *s = xstrdup(s_);
-    char *error = str_to_ofpacts(s, ofpacts);
+    char *error;
+
+    *usable_protocols = OFPUTIL_P_ANY;
+
+    error = str_to_ofpacts(s, ofpacts, usable_protocols);
     free(s);
 
     return error;
@@ -1622,9 +1686,10 @@ parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
-                       uint16_t command)
+                       uint16_t command,
+                       enum ofputil_protocol *usable_protocols)
 {
-    char *error = parse_ofp_str(fm, command, string);
+    char *error = parse_ofp_str(fm, command, string, usable_protocols);
     if (!error) {
         /* Normalize a copy of the match.  This ensures that non-normalized
          * flows get logged but doesn't affect what gets sent to the switch, so
@@ -1644,13 +1709,16 @@ parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
  * error.  The caller is responsible for freeing the returned string. */
 char * WARN_UNUSED_RESULT
 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
-                        struct ofputil_flow_mod **fms, size_t *n_fms)
+                        struct ofputil_flow_mod **fms, size_t *n_fms,
+                        enum ofputil_protocol *usable_protocols)
 {
     size_t allocated_fms;
     int line_number;
     FILE *stream;
     struct ds s;
 
+    *usable_protocols = OFPUTIL_P_ANY;
+
     *fms = NULL;
     *n_fms = 0;
 
@@ -1665,11 +1733,13 @@ parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
     line_number = 0;
     while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
         char *error;
+        enum ofputil_protocol usable;
 
         if (*n_fms >= allocated_fms) {
             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
         }
-        error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command);
+        error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
+                                       &usable);
         if (error) {
             size_t i;
 
@@ -1687,6 +1757,7 @@ parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
 
             return xasprintf("%s:%d: %s", file_name, line_number, error);
         }
+        *usable_protocols &= usable; /* Each line can narrow the set. */
         *n_fms += 1;
     }
 
@@ -1699,16 +1770,25 @@ parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
 
 char * WARN_UNUSED_RESULT
 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
-                                 bool aggregate, const char *string)
+                                 bool aggregate, const char *string,
+                                 enum ofputil_protocol *usable_protocols)
 {
     struct ofputil_flow_mod fm;
     char *error;
 
-    error = parse_ofp_str(&fm, -1, string);
+    error = parse_ofp_str(&fm, -1, string, usable_protocols);
     if (error) {
         return error;
     }
 
+    /* Special table ID support not required for stats requests. */
+    if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
+        *usable_protocols |= OFPUTIL_P_OF10_STD;
+    }
+    if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
+        *usable_protocols |= OFPUTIL_P_OF10_NXM;
+    }
+
     fsr->aggregate = aggregate;
     fsr->cookie = fm.cookie;
     fsr->cookie_mask = fm.cookie_mask;
index 707f5d8..cdb6831 100644 (file)
@@ -30,32 +30,40 @@ struct ofputil_flow_mod;
 struct ofputil_flow_monitor_request;
 struct ofputil_flow_stats_request;
 struct ofputil_meter_mod;
+enum ofputil_protocol;
 
-char *parse_ofp_str(struct ofputil_flow_mod *, int command, const char *str_)
+char *parse_ofp_str(struct ofputil_flow_mod *, int command, const char *str_,
+                    enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
 char *parse_ofp_flow_mod_str(struct ofputil_flow_mod *, const char *string,
-                            uint16_t command)
+                             uint16_t command,
+                             enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 char *parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
-                              struct ofputil_flow_mod **fms, size_t *n_fms)
+                              struct ofputil_flow_mod **fms, size_t *n_fms,
+                              enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
 char *parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *,
-                                       bool aggregate, const char *string)
+                                       bool aggregate, const char *string,
+                                       enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
-char *parse_ofpacts(const char *, struct ofpbuf *ofpacts)
+char *parse_ofpacts(const char *, struct ofpbuf *ofpacts,
+                    enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
 char *parse_ofp_exact_flow(struct flow *, const char *);
 
 char *parse_ofp_meter_mod_str(struct ofputil_meter_mod *, const char *string,
-                             int command)
+                              int command,
+                              enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
 char *parse_flow_monitor_request(struct ofputil_flow_monitor_request *,
-                                const char *)
+                                 const char *,
+                                 enum ofputil_protocol *usable_protocols)
     WARN_UNUSED_RESULT;
 
 #endif /* ofp-parse.h */
index 23ba9d4..560762b 100644 (file)
@@ -724,30 +724,23 @@ ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
 }
 
 static void
-ofp_print_flow_flags(struct ds *s, uint16_t flags)
+ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
 {
-    if (flags & OFPFF_SEND_FLOW_REM) {
+    if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
         ds_put_cstr(s, "send_flow_rem ");
     }
-    if (flags & OFPFF_CHECK_OVERLAP) {
+    if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
         ds_put_cstr(s, "check_overlap ");
     }
-    if (flags & OFPFF12_RESET_COUNTS) {
+    if (flags & OFPUTIL_FF_RESET_COUNTS) {
         ds_put_cstr(s, "reset_counts ");
     }
-    if (flags & OFPFF13_NO_PKT_COUNTS) {
+    if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
         ds_put_cstr(s, "no_packet_counts ");
     }
-    if (flags & OFPFF13_NO_BYT_COUNTS) {
+    if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
         ds_put_cstr(s, "no_byte_counts ");
     }
-
-    flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
-               | OFPFF12_RESET_COUNTS
-               | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
-    if (flags) {
-        ds_put_format(s, "flags:0x%"PRIx16" ", flags);
-    }
 }
 
 static void
@@ -848,9 +841,14 @@ ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
         ofputil_format_port(fm.out_port, s);
         ds_put_char(s, ' ');
     }
-    if (fm.flags != 0) {
-        ofp_print_flow_flags(s, fm.flags);
+
+    if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
+        /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
+         * versions don't really have such a flag and printing one is likely to
+         * confuse people. */
+        fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
     }
+    ofp_print_flow_flags(s, fm.flags);
 
     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
     ofpbuf_uninit(&ofpacts);
index 62cfd2e..61fb74f 100644 (file)
@@ -1100,158 +1100,6 @@ ofputil_packet_in_format_from_string(const char *s)
             : -1);
 }
 
-static bool
-regs_fully_wildcarded(const struct flow_wildcards *wc)
-{
-    int i;
-
-    for (i = 0; i < FLOW_N_REGS; i++) {
-        if (wc->masks.regs[i] != 0) {
-            return false;
-        }
-    }
-    return true;
-}
-
-/* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
- * to a switch (e.g. to add or remove a flow).  Only NXM can handle tunnel IDs,
- * registers, or fixing the Ethernet multicast bit.  Otherwise, it's better to
- * use OpenFlow 1.0 protocol for backward compatibility. */
-enum ofputil_protocol
-ofputil_usable_protocols(const struct match *match)
-{
-    const struct flow_wildcards *wc = &match->wc;
-
-    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
-
-    /* These tunnel params can't be sent in a flow_mod */
-    if (wc->masks.tunnel.ip_ttl
-        || wc->masks.tunnel.ip_tos || wc->masks.tunnel.flags) {
-        return OFPUTIL_P_NONE;
-    }
-
-    /* skb_priority can't be sent in a flow_mod */
-    if (wc->masks.skb_priority) {
-        return OFPUTIL_P_NONE;
-    }
-
-    /* NXM and OXM support pkt_mark */
-    if (wc->masks.pkt_mark) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM, OXM, and OF1.1 support bitwise matching on ethernet addresses. */
-    if (!eth_mask_is_exact(wc->masks.dl_src)
-        && !eth_addr_is_zero(wc->masks.dl_src)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-    if (!eth_mask_is_exact(wc->masks.dl_dst)
-        && !eth_addr_is_zero(wc->masks.dl_dst)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM, OXM, and OF1.1+ support matching metadata. */
-    if (wc->masks.metadata != htonll(0)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching ARP hardware addresses. */
-    if (!eth_addr_is_zero(wc->masks.arp_sha) ||
-        !eth_addr_is_zero(wc->masks.arp_tha)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching L3 and L4 fields within IPv6.
-     *
-     * (arp_sha, arp_tha, nw_frag, and nw_ttl are covered elsewhere so they
-     * don't need to be included in this test too.) */
-    if (match->flow.dl_type == htons(ETH_TYPE_IPV6)
-        && (!ipv6_mask_is_any(&wc->masks.ipv6_src)
-            || !ipv6_mask_is_any(&wc->masks.ipv6_dst)
-            || !ipv6_mask_is_any(&wc->masks.nd_target)
-            || wc->masks.ipv6_label
-            || wc->masks.tp_src
-            || wc->masks.tp_dst
-            || wc->masks.nw_proto
-            || wc->masks.nw_tos)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching registers. */
-    if (!regs_fully_wildcarded(wc)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching tun_id, tun_src, and tun_dst. */
-    if (wc->masks.tunnel.tun_id != htonll(0)
-        || wc->masks.tunnel.ip_src != htonl(0)
-        || wc->masks.tunnel.ip_dst != htonl(0)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching fragments. */
-    if (wc->masks.nw_frag) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching IP ECN bits. */
-    if (wc->masks.nw_tos & IP_ECN_MASK) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support matching IP TTL/hop limit. */
-    if (wc->masks.nw_ttl) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support non-CIDR IPv4 address masks. */
-    if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OXM support bitwise matching on transport port. */
-    if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
-        (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OF1.1+ support matching MPLS label */
-    if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OF1.1+ support matching MPLS TC */
-    if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* NXM and OF1.3+ support matching MPLS stack flag */
-    /* Allow for OF1.2 as there doesn't seem to be a
-     * particularly good reason not to */
-    if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
-        return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-
-    /* Other formats can express this rule. */
-    return OFPUTIL_P_ANY;
-}
-
 void
 ofputil_format_version(struct ds *msg, enum ofp_version version)
 {
@@ -1554,6 +1402,74 @@ ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
     return msg;
 }
 
+struct ofputil_flow_mod_flag {
+    uint16_t raw_flag;
+    enum ofp_version min_version, max_version;
+    enum ofputil_flow_mod_flags flag;
+};
+
+static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
+    { OFPFF_SEND_FLOW_REM,   OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
+    { OFPFF_CHECK_OVERLAP,   OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
+    { OFPFF10_EMERG,         OFP10_VERSION, OFP10_VERSION,
+      OFPUTIL_FF_EMERG },
+    { OFPFF12_RESET_COUNTS,  OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
+    { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
+    { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
+    { 0, 0, 0, 0 },
+};
+
+static enum ofperr
+ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
+                              enum ofp_flow_mod_command command,
+                              enum ofp_version version,
+                              enum ofputil_flow_mod_flags *flagsp)
+{
+    uint16_t raw_flags = ntohs(raw_flags_);
+    const struct ofputil_flow_mod_flag *f;
+
+    *flagsp = 0;
+    for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
+        if (raw_flags & f->raw_flag
+            && version >= f->min_version
+            && (!f->max_version || version <= f->max_version)) {
+            raw_flags &= ~f->raw_flag;
+            *flagsp |= f->flag;
+        }
+    }
+
+    /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
+     * never do.
+     *
+     * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
+     * resets counters. */
+    if ((version == OFP10_VERSION || version == OFP11_VERSION)
+        && command == OFPFC_ADD) {
+        *flagsp |= OFPUTIL_FF_RESET_COUNTS;
+    }
+
+    return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
+}
+
+static ovs_be16
+ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
+                              enum ofp_version version)
+{
+    const struct ofputil_flow_mod_flag *f;
+    uint16_t raw_flags;
+
+    raw_flags = 0;
+    for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
+        if (f->flag & flags
+            && version >= f->min_version
+            && (!f->max_version || version <= f->max_version)) {
+            raw_flags |= f->raw_flag;
+        }
+    }
+
+    return htons(raw_flags);
+}
+
 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
  * flow_mod in 'fm'.  Returns 0 if successful, otherwise an OpenFlow error
  * code.
@@ -1570,16 +1486,16 @@ ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
                         enum ofputil_protocol protocol,
                         struct ofpbuf *ofpacts)
 {
-    uint16_t command;
+    ovs_be16 raw_flags;
+    enum ofperr error;
     struct ofpbuf b;
     enum ofpraw raw;
 
     ofpbuf_use_const(&b, oh, ntohs(oh->length));
     raw = ofpraw_pull_assert(&b);
     if (raw == OFPRAW_OFPT11_FLOW_MOD) {
-        /* Standard OpenFlow 1.1 flow_mod. */
+        /* Standard OpenFlow 1.1+ flow_mod. */
         const struct ofp11_flow_mod *ofm;
-        enum ofperr error;
 
         ofm = ofpbuf_pull(&b, sizeof *ofm);
 
@@ -1626,12 +1542,13 @@ ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
             && ofm->out_group != htonl(OFPG_ANY)) {
             return OFPERR_OFPFMFC_UNKNOWN;
         }
-        fm->flags = ntohs(ofm->flags);
+        raw_flags = ofm->flags;
     } else {
+        uint16_t command;
+
         if (raw == OFPRAW_OFPT10_FLOW_MOD) {
             /* Standard OpenFlow 1.0 flow_mod. */
             const struct ofp10_flow_mod *ofm;
-            enum ofperr error;
 
             /* Get the ofp10_flow_mod. */
             ofm = ofpbuf_pull(&b, sizeof *ofm);
@@ -1661,11 +1578,10 @@ ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
             fm->hard_timeout = ntohs(ofm->hard_timeout);
             fm->buffer_id = ntohl(ofm->buffer_id);
             fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
-            fm->flags = ntohs(ofm->flags);
+            raw_flags = ofm->flags;
         } else if (raw == OFPRAW_NXT_FLOW_MOD) {
             /* Nicira extended flow_mod. */
             const struct nx_flow_mod *nfm;
-            enum ofperr error;
 
             /* Dissect the message. */
             nfm = ofpbuf_pull(&b, sizeof *nfm);
@@ -1692,23 +1608,11 @@ ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
             fm->hard_timeout = ntohs(nfm->hard_timeout);
             fm->buffer_id = ntohl(nfm->buffer_id);
             fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
-            fm->flags = ntohs(nfm->flags);
+            raw_flags = nfm->flags;
         } else {
             NOT_REACHED();
         }
 
-        if (fm->flags & OFPFF10_EMERG) {
-            /* We do not support the OpenFlow 1.0 emergency flow cache, which
-             * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
-             *
-             * OpenFlow 1.0 specifies the error code to use when idle_timeout
-             * or hard_timeout is nonzero.  Otherwise, there is no good error
-             * code, so just state that the flow table is full. */
-            return (fm->hard_timeout || fm->idle_timeout
-                    ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
-                    : OFPERR_OFPFMFC_TABLE_FULL);
-        }
-
         fm->modify_cookie = fm->new_cookie != htonll(UINT64_MAX);
         if (protocol & OFPUTIL_P_TID) {
             fm->command = command & 0xff;
@@ -1722,6 +1626,24 @@ ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
     fm->ofpacts = ofpacts->data;
     fm->ofpacts_len = ofpacts->size;
 
+    error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
+                                          oh->version, &fm->flags);
+    if (error) {
+        return error;
+    }
+
+    if (fm->flags & OFPUTIL_FF_EMERG) {
+        /* We do not support the OpenFlow 1.0 emergency flow cache, which
+         * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
+         *
+         * OpenFlow 1.0 specifies the error code to use when idle_timeout
+         * or hard_timeout is nonzero.  Otherwise, there is no good error
+         * code, so just state that the flow table is full. */
+        return (fm->hard_timeout || fm->idle_timeout
+                ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
+                : OFPERR_OFPFMFC_TABLE_FULL);
+    }
+
     return 0;
 }
 
@@ -2105,6 +2027,8 @@ struct ofpbuf *
 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
                         enum ofputil_protocol protocol)
 {
+    enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
+    ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
     struct ofpbuf *msg;
 
     switch (protocol) {
@@ -2115,9 +2039,7 @@ ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
         int tailroom;
 
         tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
-        msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD,
-                           ofputil_protocol_to_ofp_version(protocol),
-                           tailroom);
+        msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
         ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
         if ((protocol == OFPUTIL_P_OF11_STD
              && (fm->command == OFPFC_MODIFY ||
@@ -2137,7 +2059,7 @@ ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
         ofm->buffer_id = htonl(fm->buffer_id);
         ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
         ofm->out_group = htonl(OFPG11_ANY);
-        ofm->flags = htons(fm->flags);
+        ofm->flags = raw_flags;
         ofputil_put_ofp11_match(msg, &fm->match, protocol);
         ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
         break;
@@ -2158,7 +2080,7 @@ ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
         ofm->priority = htons(fm->priority);
         ofm->buffer_id = htonl(fm->buffer_id);
         ofm->out_port = htons(ofp_to_u16(fm->out_port));
-        ofm->flags = htons(fm->flags);
+        ofm->flags = raw_flags;
         ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
         break;
     }
@@ -2180,7 +2102,7 @@ ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
         nfm->priority = htons(fm->priority);
         nfm->buffer_id = htonl(fm->buffer_id);
         nfm->out_port = htons(ofp_to_u16(fm->out_port));
-        nfm->flags = htons(fm->flags);
+        nfm->flags = raw_flags;
         nfm->match_len = htons(match_len);
         ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
         break;
@@ -2194,39 +2116,6 @@ ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
     return msg;
 }
 
-/* Returns a bitmask with a 1-bit for each protocol that could be used to
- * send all of the 'n_fm's flow table modification requests in 'fms', and a
- * 0-bit for each protocol that is inadequate.
- *
- * (The return value will have at least one 1-bit.) */
-enum ofputil_protocol
-ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
-                                  size_t n_fms)
-{
-    enum ofputil_protocol usable_protocols;
-    size_t i;
-
-    usable_protocols = OFPUTIL_P_ANY;
-    for (i = 0; i < n_fms; i++) {
-        const struct ofputil_flow_mod *fm = &fms[i];
-
-        usable_protocols &= ofputil_usable_protocols(&fm->match);
-        if (fm->table_id != 0xff) {
-            usable_protocols &= OFPUTIL_P_TID;
-        }
-
-        /* Matching of the cookie is only supported through NXM or OF1.1+. */
-        if (fm->cookie_mask != htonll(0)) {
-            usable_protocols &= (OFPUTIL_P_OF10_NXM_ANY
-                                 | OFPUTIL_P_OF11_STD
-                                 | OFPUTIL_P_OF12_OXM
-                                 | OFPUTIL_P_OF13_OXM);
-        }
-    }
-
-    return usable_protocols;
-}
-
 static enum ofperr
 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
                                     const struct ofp10_flow_stats_request *ofsr,
@@ -2402,24 +2291,6 @@ ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
     return msg;
 }
 
-/* Returns a bitmask with a 1-bit for each protocol that could be used to
- * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
- *
- * (The return value will have at least one 1-bit.) */
-enum ofputil_protocol
-ofputil_flow_stats_request_usable_protocols(
-    const struct ofputil_flow_stats_request *fsr)
-{
-    enum ofputil_protocol usable_protocols;
-
-    usable_protocols = ofputil_usable_protocols(&fsr->match);
-    if (fsr->cookie_mask != htonll(0)) {
-        usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
-            | OFPUTIL_P_OF13_OXM;
-    }
-    return usable_protocols;
-}
-
 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
  * ofputil_flow_stats in 'fs'.
  *
@@ -2446,6 +2317,7 @@ ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
                                 bool flow_age_extension,
                                 struct ofpbuf *ofpacts)
 {
+    const struct ofp_header *oh;
     enum ofperr error;
     enum ofpraw raw;
 
@@ -2455,6 +2327,7 @@ ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
     if (error) {
         return error;
     }
+    oh = msg->l2;
 
     if (!msg->size) {
         return EOF;
@@ -2495,7 +2368,15 @@ ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
         fs->duration_nsec = ntohl(ofs->duration_nsec);
         fs->idle_timeout = ntohs(ofs->idle_timeout);
         fs->hard_timeout = ntohs(ofs->hard_timeout);
-        fs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? ntohs(ofs->flags) : 0;
+        if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
+            error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
+                                                  &fs->flags);
+            if (error) {
+                return error;
+            }
+        } else {
+            fs->flags = 0;
+        }
         fs->idle_age = -1;
         fs->hard_age = -1;
         fs->cookie = ofs->cookie;
@@ -2616,6 +2497,7 @@ ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
 
     ofpraw_decode_partial(&raw, reply->data, reply->size);
     if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
+        const struct ofp_header *oh = reply->data;
         struct ofp11_flow_stats *ofs;
 
         ofpbuf_put_uninit(reply, sizeof *ofs);
@@ -2632,7 +2514,11 @@ ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
         ofs->priority = htons(fs->priority);
         ofs->idle_timeout = htons(fs->idle_timeout);
         ofs->hard_timeout = htons(fs->hard_timeout);
-        ofs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? htons(fs->flags) : 0;
+        if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
+            ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, oh->version);
+        } else {
+            ofs->flags = 0;
+        }
         memset(ofs->pad2, 0, sizeof ofs->pad2);
         ofs->cookie = fs->cookie;
         ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
index f3348c0..7e50db2 100644 (file)
@@ -101,6 +101,17 @@ enum ofputil_protocol {
     OFPUTIL_P_OF13_OXM      = 1 << 6,
 #define OFPUTIL_P_ANY_OXM (OFPUTIL_P_OF12_OXM | OFPUTIL_P_OF13_OXM)
 
+#define OFPUTIL_P_NXM_OF11_UP (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF11_STD | \
+                               OFPUTIL_P_ANY_OXM)
+
+#define OFPUTIL_P_NXM_OXM_ANY (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_ANY_OXM)
+
+#define OFPUTIL_P_OF11_UP (OFPUTIL_P_OF11_STD | OFPUTIL_P_ANY_OXM)
+
+#define OFPUTIL_P_OF12_UP (OFPUTIL_P_ANY_OXM)
+
+#define OFPUTIL_P_OF13_UP (OFPUTIL_P_OF13_OXM)
+
     /* All protocols. */
 #define OFPUTIL_P_ANY ((1 << 7) - 1)
 
@@ -129,7 +140,6 @@ enum ofputil_protocol ofputil_protocol_set_base(
 const char *ofputil_protocol_to_string(enum ofputil_protocol);
 char *ofputil_protocols_to_string(enum ofputil_protocol);
 enum ofputil_protocol ofputil_protocols_from_string(const char *);
-enum ofputil_protocol ofputil_usable_protocols(const struct match *);
 
 void ofputil_format_version(struct ds *, enum ofp_version);
 void ofputil_format_version_name(struct ds *, enum ofp_version);
@@ -207,6 +217,16 @@ struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
 /* NXT_FLOW_MOD_TABLE_ID extension. */
 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
 
+/* Protocol-independent flow_mod flags. */
+enum ofputil_flow_mod_flags {
+    OFPUTIL_FF_SEND_FLOW_REM = 1 << 0, /* All versions. */
+    OFPUTIL_FF_CHECK_OVERLAP = 1 << 1, /* All versions. */
+    OFPUTIL_FF_EMERG         = 1 << 2, /* OpenFlow 1.0 only. */
+    OFPUTIL_FF_RESET_COUNTS  = 1 << 3, /* OpenFlow 1.2+. */
+    OFPUTIL_FF_NO_PKT_COUNTS = 1 << 4, /* OpenFlow 1.3+. */
+    OFPUTIL_FF_NO_BYT_COUNTS = 1 << 5  /* OpenFlow 1.3+. */
+};
+
 /* Protocol-independent flow_mod.
  *
  * The handling of cookies across multiple versions of OpenFlow is a bit
@@ -248,7 +268,7 @@ struct ofputil_flow_mod {
     uint16_t hard_timeout;
     uint32_t buffer_id;
     ofp_port_t out_port;
-    uint16_t flags;
+    enum ofputil_flow_mod_flags flags;
     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
 };
@@ -260,9 +280,6 @@ enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
                                        enum ofputil_protocol);
 
-enum ofputil_protocol ofputil_flow_mod_usable_protocols(
-    const struct ofputil_flow_mod *fms, size_t n_fms);
-
 /* Flow stats or aggregate stats request, independent of protocol. */
 struct ofputil_flow_stats_request {
     bool aggregate;             /* Aggregate results? */
@@ -277,8 +294,6 @@ enum ofperr ofputil_decode_flow_stats_request(
     struct ofputil_flow_stats_request *, const struct ofp_header *);
 struct ofpbuf *ofputil_encode_flow_stats_request(
     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
-enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
-    const struct ofputil_flow_stats_request *);
 
 /* Flow stats reply, independent of protocol. */
 struct ofputil_flow_stats {
@@ -296,7 +311,7 @@ struct ofputil_flow_stats {
     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
     struct ofpact *ofpacts;
     size_t ofpacts_len;
-    uint16_t flags;             /* Added for OF 1.3 */
+    enum ofputil_flow_mod_flags flags;
 };
 
 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
diff --git a/lib/ovs-atomic-clang.h b/lib/ovs-atomic-clang.h
new file mode 100644 (file)
index 0000000..dbec956
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2013 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This header implements atomic operation primitives on Clang. */
+#ifndef IN_OVS_ATOMIC_H
+#error "This header should only be included indirectly via ovs-atomic.h."
+#endif
+
+#define OVS_ATOMIC_CLANG_IMPL 1
+
+/* Standard atomic types. */
+typedef _Atomic(_Bool) atomic_bool;
+
+typedef _Atomic(char) atomic_char;
+typedef _Atomic(signed char) atomic_schar;
+typedef _Atomic(unsigned char) atomic_uchar;
+
+typedef _Atomic(short) atomic_short;
+typedef _Atomic(unsigned short) atomic_ushort;
+
+typedef _Atomic(int) atomic_int;
+typedef _Atomic(unsigned int) atomic_uint;
+
+typedef _Atomic(long) atomic_long;
+typedef _Atomic(unsigned long) atomic_ulong;
+
+typedef _Atomic(long long) atomic_llong;
+typedef _Atomic(unsigned long long) atomic_ullong;
+
+typedef _Atomic(size_t) atomic_size_t;
+typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
+
+typedef _Atomic(intmax_t) atomic_intmax_t;
+typedef _Atomic(uintmax_t) atomic_uintmax_t;
+
+typedef _Atomic(intptr_t) atomic_intptr_t;
+typedef _Atomic(uintptr_t) atomic_uintptr_t;
+
+/* Nonstandard atomic types. */
+typedef _Atomic(uint8_t)   atomic_uint8_t;
+typedef _Atomic(uint16_t)  atomic_uint16_t;
+typedef _Atomic(uint32_t)  atomic_uint32_t;
+typedef _Atomic(uint64_t)  atomic_uint64_t;
+
+typedef _Atomic(int8_t)    atomic_int8_t;
+typedef _Atomic(int16_t)   atomic_int16_t;
+typedef _Atomic(int32_t)   atomic_int32_t;
+typedef _Atomic(int64_t)   atomic_int64_t;
+
+#define ATOMIC_VAR_INIT(VALUE) (VALUE)
+
+#define atomic_init(OBJECT, VALUE) __c11_atomic_init(OBJECT, VALUE)
+
+/* Clang hard-codes these exact values internally but does not appear to
+ * export any names for them. */
+typedef enum {
+    memory_order_relaxed = 0,
+    memory_order_consume = 1,
+    memory_order_acquire = 2,
+    memory_order_release = 3,
+    memory_order_acq_rel = 4,
+    memory_order_seq_cst = 5
+} memory_order;
+
+#define atomic_thread_fence(ORDER) __c11_atomic_thread_fence(ORDER)
+#define atomic_signal_fence(ORDER) __c11_atomic_signal_fence(ORDER)
+
+#define atomic_store(DST, SRC) \
+    atomic_store_explicit(DST, SRC, memory_order_seq_cst)
+#define atomic_store_explicit(DST, SRC, ORDER) \
+    __c11_atomic_store(DST, SRC, ORDER)
+
+
+#define atomic_read(SRC, DST) \
+    atomic_read_explicit(SRC, DST, memory_order_seq_cst)
+#define atomic_read_explicit(SRC, DST, ORDER)   \
+    (*(DST) = __c11_atomic_load(SRC, ORDER), \
+     (void) 0)
+
+#define atomic_add(RMW, ARG, ORIG) \
+    atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
+#define atomic_sub(RMW, ARG, ORIG) \
+    atomic_sub_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
+#define atomic_or(RMW, ARG, ORIG) \
+    atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
+#define atomic_xor(RMW, ARG, ORIG) \
+    atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
+#define atomic_and(RMW, ARG, ORIG) \
+    atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
+
+#define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
+    (*(ORIG) = __c11_atomic_fetch_add(RMW, ARG, ORDER), (void) 0)
+#define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
+    (*(ORIG) = __c11_atomic_fetch_sub(RMW, ARG, ORDER), (void) 0)
+#define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
+    (*(ORIG) = __c11_atomic_fetch_or(RMW, ARG, ORDER), (void) 0)
+#define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
+    (*(ORIG) = __c11_atomic_fetch_xor(RMW, ARG, ORDER), (void) 0)
+#define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
+    (*(ORIG) = __c11_atomic_fetch_and(RMW, ARG, ORDER), (void) 0)
+
+#include "ovs-atomic-flag-gcc4.7+.h"
diff --git a/lib/ovs-atomic-flag-gcc4.7+.h b/lib/ovs-atomic-flag-gcc4.7+.h
new file mode 100644 (file)
index 0000000..c42c7ca
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2013 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This header implements atomic_flag on Clang and on GCC 4.7 and later. */
+#ifndef IN_OVS_ATOMIC_H
+#error "This header should only be included indirectly via ovs-atomic.h."
+#endif
+
+/* atomic_flag */
+
+typedef struct {
+    unsigned char b;
+} atomic_flag;
+#define ATOMIC_FLAG_INIT { .b = false }
+
+static inline bool
+atomic_flag_test_and_set_explicit(volatile atomic_flag *object,
+                                  memory_order order)
+{
+    return __atomic_test_and_set(&object->b, order);
+}
+
+static inline bool
+atomic_flag_test_and_set(volatile atomic_flag *object)
+{
+    return atomic_flag_test_and_set_explicit(object, memory_order_seq_cst);
+}
+
+static inline void
+atomic_flag_clear_explicit(volatile atomic_flag *object, memory_order order)
+{
+    __atomic_clear(object, order);
+}
+
+static inline void
+atomic_flag_clear(volatile atomic_flag *object)
+{
+    atomic_flag_clear_explicit(object, memory_order_seq_cst);
+}
index 07bef2a..52e167f 100644 (file)
@@ -107,35 +107,5 @@ typedef enum {
     (*(ORIG) = __atomic_fetch_xor(RMW, OPERAND, ORDER), (void) 0)
 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER)  \
     (*(ORIG) = __atomic_fetch_and(RMW, OPERAND, ORDER), (void) 0)
-\f
-/* atomic_flag */
-
-typedef struct {
-    unsigned char b;
-} atomic_flag;
-#define ATOMIC_FLAG_INIT { .b = false }
-
-static inline bool
-atomic_flag_test_and_set_explicit(volatile atomic_flag *object,
-                                  memory_order order)
-{
-    return __atomic_test_and_set(&object->b, order);
-}
-
-static inline bool
-atomic_flag_test_and_set(volatile atomic_flag *object)
-{
-    return atomic_flag_test_and_set_explicit(object, memory_order_seq_cst);
-}
-
-static inline void
-atomic_flag_clear_explicit(volatile atomic_flag *object, memory_order order)
-{
-    __atomic_clear(object, order);
-}
-
-static inline void
-atomic_flag_clear(volatile atomic_flag *object)
-{
-    atomic_flag_clear_explicit(object, memory_order_seq_cst);
-}
+
+#include "ovs-atomic-flag-gcc4.7+.h"
index 3467f11..a9ae4ae 100644 (file)
         #include "ovs-atomic-pthreads.h"
     #elif HAVE_STDATOMIC_H
         #include "ovs-atomic-c11.h"
+    #elif __has_extension(c_atomic)
+        #include "ovs-atomic-clang.h"
     #elif __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
         #include "ovs-atomic-gcc4.7+.h"
     #elif HAVE_GCC4_ATOMICS
index 253184f..d15c402 100644 (file)
@@ -29,6 +29,7 @@
 #include "dynamic-string.h"
 #include "ofpbuf.h"
 #include "ovs-thread.h"
+#include "unaligned.h"
 
 const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT;
 
@@ -151,9 +152,9 @@ compose_rarp(struct ofpbuf *b, const uint8_t eth_src[ETH_ADDR_LEN])
     struct arp_eth_header *arp;
 
     ofpbuf_clear(b);
-    ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
+    ofpbuf_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN
                              + ARP_ETH_HEADER_LEN);
-    ofpbuf_reserve(b, VLAN_HEADER_LEN);
+    ofpbuf_reserve(b, 2 + VLAN_HEADER_LEN);
     eth = ofpbuf_put_uninit(b, sizeof *eth);
     memcpy(eth->eth_dst, eth_addr_broadcast, ETH_ADDR_LEN);
     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
@@ -166,9 +167,9 @@ compose_rarp(struct ofpbuf *b, const uint8_t eth_src[ETH_ADDR_LEN])
     arp->ar_pln = sizeof arp->ar_spa;
     arp->ar_op = htons(ARP_OP_RARP);
     memcpy(arp->ar_sha, eth_src, ETH_ADDR_LEN);
-    arp->ar_spa = htonl(0);
+    put_16aligned_be32(&arp->ar_spa, htonl(0));
     memcpy(arp->ar_tha, eth_src, ETH_ADDR_LEN);
-    arp->ar_tpa = htonl(0);
+    put_16aligned_be32(&arp->ar_tpa, htonl(0));
 }
 
 /* Insert VLAN header according to given TCI. Packet passed must be Ethernet
@@ -395,13 +396,16 @@ pop_mpls(struct ofpbuf *packet, ovs_be16 ethtype)
 
 /* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'.  The
  * caller must free '*packetp'.  On success, returns NULL.  On failure, returns
- * an error message and stores NULL in '*packetp'. */
+ * an error message and stores NULL in '*packetp'.
+ *
+ * Aligns the L3 header of '*packetp' on a 32-bit boundary. */
 const char *
 eth_from_hex(const char *hex, struct ofpbuf **packetp)
 {
     struct ofpbuf *packet;
 
-    packet = *packetp = ofpbuf_new(strlen(hex) / 2);
+    /* Use 2 bytes of headroom to 32-bit align the L3 header. */
+    packet = *packetp = ofpbuf_new_with_headroom(strlen(hex) / 2, 2);
 
     if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
         ofpbuf_delete(packet);
@@ -602,7 +606,8 @@ ipv6_is_cidr(const struct in6_addr *netmask)
  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
  * in 'b' and returned.  This payload may be populated with appropriate
  * information by the caller.  Sets 'b''s 'l2' and 'l3' pointers to the
- * Ethernet header and payload respectively.
+ * Ethernet header and payload respectively.  Aligns b->l3 on a 32-bit
+ * boundary.
  *
  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
  * desired. */
@@ -616,8 +621,10 @@ eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
 
     ofpbuf_clear(b);
 
-    ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
-    ofpbuf_reserve(b, VLAN_HEADER_LEN);
+    /* The magic 2 here ensures that the L3 header (when it is added later)
+     * will be 32-bit aligned. */
+    ofpbuf_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
+    ofpbuf_reserve(b, 2 + VLAN_HEADER_LEN);
     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
     data = ofpbuf_put_uninit(b, size);
 
@@ -632,26 +639,28 @@ eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
 }
 
 static void
-packet_set_ipv4_addr(struct ofpbuf *packet, ovs_be32 *addr, ovs_be32 new_addr)
+packet_set_ipv4_addr(struct ofpbuf *packet,
+                     ovs_16aligned_be32 *addr, ovs_be32 new_addr)
 {
     struct ip_header *nh = packet->l3;
+    ovs_be32 old_addr = get_16aligned_be32(addr);
 
     if (nh->ip_proto == IPPROTO_TCP && packet->l7) {
         struct tcp_header *th = packet->l4;
 
-        th->tcp_csum = recalc_csum32(th->tcp_csum, *addr, new_addr);
+        th->tcp_csum = recalc_csum32(th->tcp_csum, old_addr, new_addr);
     } else if (nh->ip_proto == IPPROTO_UDP && packet->l7) {
         struct udp_header *uh = packet->l4;
 
         if (uh->udp_csum) {
-            uh->udp_csum = recalc_csum32(uh->udp_csum, *addr, new_addr);
+            uh->udp_csum = recalc_csum32(uh->udp_csum, old_addr, new_addr);
             if (!uh->udp_csum) {
                 uh->udp_csum = htons(0xffff);
             }
         }
     }
-    nh->ip_csum = recalc_csum32(nh->ip_csum, *addr, new_addr);
-    *addr = new_addr;
+    nh->ip_csum = recalc_csum32(nh->ip_csum, old_addr, new_addr);
+    put_16aligned_be32(addr, new_addr);
 }
 
 /* Returns true, if packet contains at least one routing header where
@@ -661,7 +670,7 @@ packet_set_ipv4_addr(struct ofpbuf *packet, ovs_be32 *addr, ovs_be32 new_addr)
 static bool
 packet_rh_present(struct ofpbuf *packet)
 {
-    const struct ip6_hdr *nh;
+    const struct ovs_16aligned_ip6_hdr *nh;
     int nexthdr;
     size_t len;
     size_t remaining;
@@ -672,7 +681,7 @@ packet_rh_present(struct ofpbuf *packet)
     if (remaining < sizeof *nh) {
         return false;
     }
-    nh = ALIGNED_CAST(struct ip6_hdr *, data);
+    nh = ALIGNED_CAST(struct ovs_16aligned_ip6_hdr *, data);
     data += sizeof *nh;
     remaining -= sizeof *nh;
     nexthdr = nh->ip6_nxt;
@@ -708,8 +717,8 @@ packet_rh_present(struct ofpbuf *packet)
             nexthdr = ext_hdr->ip6e_nxt;
             len = (ext_hdr->ip6e_len + 2) * 4;
         } else if (nexthdr == IPPROTO_FRAGMENT) {
-            const struct ip6_frag *frag_hdr = ALIGNED_CAST(struct ip6_frag *,
-                                                           data);
+            const struct ovs_16aligned_ip6_frag *frag_hdr
+                = ALIGNED_CAST(struct ovs_16aligned_ip6_frag *, data);
 
             nexthdr = frag_hdr->ip6f_nxt;
             len = sizeof *frag_hdr;
@@ -741,7 +750,7 @@ packet_rh_present(struct ofpbuf *packet)
 
 static void
 packet_update_csum128(struct ofpbuf *packet, uint8_t proto,
-                     ovs_be32 addr[4], const ovs_be32 new_addr[4])
+                     ovs_16aligned_be32 addr[4], const ovs_be32 new_addr[4])
 {
     if (proto == IPPROTO_TCP && packet->l7) {
         struct tcp_header *th = packet->l4;
@@ -761,25 +770,29 @@ packet_update_csum128(struct ofpbuf *packet, uint8_t proto,
 
 static void
 packet_set_ipv6_addr(struct ofpbuf *packet, uint8_t proto,
-                     struct in6_addr *addr, const ovs_be32 new_addr[4],
+                     ovs_16aligned_be32 *addr, const ovs_be32 new_addr[4],
                      bool recalculate_csum)
 {
     if (recalculate_csum) {
-        packet_update_csum128(packet, proto, (ovs_be32 *)addr, new_addr);
+        packet_update_csum128(packet, proto, addr, new_addr);
     }
     memcpy(addr, new_addr, sizeof(*addr));
 }
 
 static void
-packet_set_ipv6_flow_label(ovs_be32 *flow_label, ovs_be32 flow_key)
+packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key)
 {
-    *flow_label = (*flow_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
+    ovs_be32 old_label = get_16aligned_be32(flow_label);
+    ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
+    put_16aligned_be32(flow_label, new_label);
 }
 
 static void
-packet_set_ipv6_tc(ovs_be32 *flow_label, uint8_t tc)
+packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc)
 {
-    *flow_label = (*flow_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
+    ovs_be32 old_label = get_16aligned_be32(flow_label);
+    ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
+    put_16aligned_be32(flow_label, new_label);
 }
 
 /* Modifies the IPv4 header fields of 'packet' to be consistent with 'src',
@@ -792,11 +805,11 @@ packet_set_ipv4(struct ofpbuf *packet, ovs_be32 src, ovs_be32 dst,
 {
     struct ip_header *nh = packet->l3;
 
-    if (nh->ip_src != src) {
+    if (get_16aligned_be32(&nh->ip_src) != src) {
         packet_set_ipv4_addr(packet, &nh->ip_src, src);
     }
 
-    if (nh->ip_dst != dst) {
+    if (get_16aligned_be32(&nh->ip_dst) != dst) {
         packet_set_ipv4_addr(packet, &nh->ip_dst, dst);
     }
 
@@ -826,14 +839,14 @@ packet_set_ipv6(struct ofpbuf *packet, uint8_t proto, const ovs_be32 src[4],
                 const ovs_be32 dst[4], uint8_t key_tc, ovs_be32 key_fl,
                 uint8_t key_hl)
 {
-    struct ip6_hdr *nh = packet->l3;
+    struct ovs_16aligned_ip6_hdr *nh = packet->l3;
 
     if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) {
-        packet_set_ipv6_addr(packet, proto, &nh->ip6_src, src, true);
+        packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32, src, true);
     }
 
     if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) {
-        packet_set_ipv6_addr(packet, proto, &nh->ip6_dst, dst,
+        packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst,
                              !packet_rh_present(packet));
     }
 
index 2108bef..b776914 100644 (file)
@@ -446,8 +446,8 @@ struct ip_header {
     uint8_t ip_ttl;
     uint8_t ip_proto;
     ovs_be16 ip_csum;
-    ovs_be32 ip_src;
-    ovs_be32 ip_dst;
+    ovs_16aligned_be32 ip_src;
+    ovs_16aligned_be32 ip_dst;
 };
 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
 
@@ -465,7 +465,7 @@ struct icmp_header {
             ovs_be16 empty;
             ovs_be16 mtu;
         } frag;
-        ovs_be32 gateway;
+        ovs_16aligned_be32 gateway;
     } icmp_fields;
     uint8_t icmp_data[0];
 };
@@ -504,8 +504,8 @@ BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
 struct tcp_header {
     ovs_be16 tcp_src;
     ovs_be16 tcp_dst;
-    ovs_be32 tcp_seq;
-    ovs_be32 tcp_ack;
+    ovs_16aligned_be32 tcp_seq;
+    ovs_16aligned_be32 tcp_ack;
     ovs_be16 tcp_ctl;
     ovs_be16 tcp_winsz;
     ovs_be16 tcp_csum;
@@ -520,7 +520,6 @@ BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
 #define ARP_OP_RARP 3
 
 #define ARP_ETH_HEADER_LEN 28
-OVS_PACKED(
 struct arp_eth_header {
     /* Generic members. */
     ovs_be16 ar_hrd;           /* Hardware type. */
@@ -531,12 +530,44 @@ struct arp_eth_header {
 
     /* Ethernet+IPv4 specific members. */
     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
-    ovs_be32 ar_spa;           /* Sender protocol address. */
+    ovs_16aligned_be32 ar_spa;           /* Sender protocol address. */
     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
-    ovs_be32 ar_tpa;           /* Target protocol address. */
-});
+    ovs_16aligned_be32 ar_tpa;           /* Target protocol address. */
+};
 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
 
+/* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
+ * most implementations, this one only requires 16-bit alignment. */
+union ovs_16aligned_in6_addr {
+    ovs_be16 be16[8];
+    ovs_16aligned_be32 be32[4];
+};
+
+/* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
+ * one only requires 16-bit alignment. */
+struct ovs_16aligned_ip6_hdr {
+    union {
+        struct ovs_16aligned_ip6_hdrctl {
+            ovs_16aligned_be32 ip6_un1_flow;
+            ovs_be16 ip6_un1_plen;
+            uint8_t ip6_un1_nxt;
+            uint8_t ip6_un1_hlim;
+        } ip6_un1;
+        uint8_t ip6_un2_vfc;
+    } ip6_ctlun;
+    union ovs_16aligned_in6_addr ip6_src;
+    union ovs_16aligned_in6_addr ip6_dst;
+};
+
+/* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
+ * this one only requires 16-bit alignment. */
+struct ovs_16aligned_ip6_frag {
+    uint8_t ip6f_nxt;
+    uint8_t ip6f_reserved;
+    ovs_be16 ip6f_offlg;
+    ovs_16aligned_be32 ip6f_ident;
+};
+
 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
 #define IPV6_LABEL_MASK 0x000fffff
 
index 2654a27..154eb13 100644 (file)
@@ -174,6 +174,21 @@ put_unaligned_u64(uint64_t *p, uint64_t x)
     put_unaligned_u64__(p, x);
 }
 \f
+/* Returns the value in 'x'. */
+static inline uint32_t
+get_16aligned_u32(const ovs_16aligned_u32 *x)
+{
+    return ((uint32_t) x->hi << 16) | x->lo;
+}
+
+/* Stores 'value' in 'x'. */
+static inline void
+put_16aligned_u32(ovs_16aligned_u32 *x, uint32_t value)
+{
+    x->hi = value >> 16;
+    x->lo = value;
+}
+
 /* Returns the value in 'x'. */
 static inline uint64_t
 get_32aligned_u64(const ovs_32aligned_u64 *x)
@@ -190,6 +205,30 @@ put_32aligned_u64(ovs_32aligned_u64 *x, uint64_t value)
 }
 
 #ifndef __CHECKER__
+/* Returns the value of 'x'. */
+static inline ovs_be32
+get_16aligned_be32(const ovs_16aligned_be32 *x)
+{
+#ifdef WORDS_BIGENDIAN
+    return ((ovs_be32) x->hi << 16) | x->lo;
+#else
+    return ((ovs_be32) x->lo << 16) | x->hi;
+#endif
+}
+
+/* Stores network byte order 'value' into 'x'. */
+static inline void
+put_16aligned_be32(ovs_16aligned_be32 *x, ovs_be32 value)
+{
+#if WORDS_BIGENDIAN
+    x->hi = value >> 16;
+    x->lo = value;
+#else
+    x->hi = value;
+    x->lo = value >> 16;
+#endif
+}
+
 /* Returns the value of 'x'. */
 static inline ovs_be64
 get_32aligned_be64(const ovs_32aligned_be64 *x)
@@ -216,6 +255,8 @@ put_32aligned_be64(ovs_32aligned_be64 *x, ovs_be64 value)
 #else  /* __CHECKER__ */
 /* Making sparse happy with these functions also makes them unreadable, so
  * don't bother to show it their implementations. */
+ovs_be32 get_16aligned_be32(const ovs_16aligned_be32 *);
+void put_16aligned_be32(ovs_16aligned_be32 *, ovs_be32);
 ovs_be64 get_32aligned_be64(const ovs_32aligned_be64 *);
 void put_32aligned_be64(ovs_32aligned_be64 *, ovs_be64);
 #endif
index 54fabfe..eeccbc5 100644 (file)
@@ -2111,33 +2111,13 @@ xlate_learn_action(struct xlate_ctx *ctx,
     ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
 }
 
-/* Reduces '*timeout' to no more than 'max'.  A value of zero in either case
- * means "infinite". */
-static void
-reduce_timeout(uint16_t max, uint16_t *timeout)
-{
-    if (max && (!*timeout || *timeout > max)) {
-        *timeout = max;
-    }
-}
-
 static void
 xlate_fin_timeout(struct xlate_ctx *ctx,
                   const struct ofpact_fin_timeout *oft)
 {
     if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
-        struct rule_dpif *rule = ctx->rule;
-
-        ovs_mutex_lock(&rule->up.ofproto->expirable_mutex);
-        if (list_is_empty(&rule->up.expirable)) {
-            list_insert(&rule->up.ofproto->expirable, &rule->up.expirable);
-        }
-        ovs_mutex_unlock(&rule->up.ofproto->expirable_mutex);
-
-        ovs_mutex_lock(&rule->up.timeout_mutex);
-        reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
-        reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
-        ovs_mutex_unlock(&rule->up.timeout_mutex);
+        ofproto_rule_reduce_timeouts(&ctx->rule->up, oft->fin_idle_timeout,
+                                     oft->fin_hard_timeout);
     }
 }
 
index 80c7c4c..d4c8f73 100644 (file)
@@ -1437,10 +1437,11 @@ destruct(struct ofproto *ofproto_)
         ovs_rwlock_wrlock(&table->cls.rwlock);
         cls_cursor_init(&cursor, &table->cls, NULL);
         CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
-            ofproto_rule_destroy(&ofproto->up, &table->cls, &rule->up);
+            ofproto_rule_delete(&ofproto->up, &table->cls, &rule->up);
         }
         ovs_rwlock_unlock(&table->cls.rwlock);
     }
+    complete_operations(ofproto);
 
     ovs_mutex_lock(&ofproto->flow_mod_mutex);
     LIST_FOR_EACH_SAFE (fm, next_fm, list_node, &ofproto->flow_mods) {
@@ -3975,12 +3976,8 @@ rule_expire(struct rule_dpif *rule)
         return;
     }
 
-    if (!ovs_rwlock_trywrlock(&rule->up.evict)) {
-        COVERAGE_INC(ofproto_dpif_expired);
-
-        /* Get rid of the rule. */
-        ofproto_rule_expire(&rule->up, reason);
-    }
+    COVERAGE_INC(ofproto_dpif_expired);
+    ofproto_rule_expire(&rule->up, reason);
 }
 \f
 /* Facets. */
@@ -4892,15 +4889,27 @@ rule_construct(struct rule *rule_)
     rule->packet_count = 0;
     rule->byte_count = 0;
     ovs_mutex_unlock(&rule->stats_mutex);
-    complete_operation(rule);
     return 0;
 }
 
 static void
-rule_destruct(struct rule *rule_)
+rule_insert(struct rule *rule_)
 {
     struct rule_dpif *rule = rule_dpif_cast(rule_);
     complete_operation(rule);
+}
+
+static void
+rule_delete(struct rule *rule_)
+{
+    struct rule_dpif *rule = rule_dpif_cast(rule_);
+    complete_operation(rule);
+}
+
+static void
+rule_destruct(struct rule *rule_)
+{
+    struct rule_dpif *rule = rule_dpif_cast(rule_);
     ovs_mutex_destroy(&rule->stats_mutex);
 }
 
@@ -4955,10 +4964,17 @@ rule_execute(struct rule *rule, const struct flow *flow,
 }
 
 static void
-rule_modify_actions(struct rule *rule_)
+rule_modify_actions(struct rule *rule_, bool reset_counters)
 {
     struct rule_dpif *rule = rule_dpif_cast(rule_);
 
+    if (reset_counters) {
+        ovs_mutex_lock(&rule->stats_mutex);
+        rule->packet_count = 0;
+        rule->byte_count = 0;
+        ovs_mutex_unlock(&rule->stats_mutex);
+    }
+
     complete_operation(rule);
 }
 \f
@@ -6343,6 +6359,8 @@ const struct ofproto_class ofproto_dpif_class = {
     NULL,                       /* rule_choose_table */
     rule_alloc,
     rule_construct,
+    rule_insert,
+    rule_delete,
     rule_destruct,
     rule_dealloc,
     rule_get_stats,
index d8b6a79..d6a8a0b 100644 (file)
@@ -277,15 +277,16 @@ rule_from_cls_rule(const struct cls_rule *cls_rule)
 }
 
 void ofproto_rule_update_used(struct rule *, long long int used);
-void ofproto_rule_expire(struct rule *rule, uint8_t reason)
-    OVS_RELEASES(rule->evict);
-void ofproto_rule_destroy(struct ofproto *, struct classifier *cls,
-                          struct rule *) OVS_REQ_WRLOCK(cls->rwlock);
+void ofproto_rule_expire(struct rule *rule, uint8_t reason);
+void ofproto_rule_delete(struct ofproto *, struct classifier *cls,
+                         struct rule *) OVS_REQ_WRLOCK(cls->rwlock);
+void ofproto_rule_reduce_timeouts(struct rule *rule, uint16_t idle_timeout,
+                                  uint16_t hard_timeout)
+    OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex);
 
 bool ofproto_rule_has_out_port(const struct rule *, ofp_port_t out_port);
 
 void ofoperation_complete(struct ofoperation *, enum ofperr);
-struct rule *ofoperation_get_victim(struct ofoperation *);
 
 bool ofoperation_has_out_port(const struct ofoperation *, ofp_port_t out_port);
 
@@ -328,6 +329,10 @@ bool ofproto_rule_is_hidden(const struct rule *);
  *   ofport   ->port_alloc  ->port_construct  ->port_destruct  ->port_dealloc
  *   rule     ->rule_alloc  ->rule_construct  ->rule_destruct  ->rule_dealloc
  *
+ * "ofproto" and "ofport" have this exact life cycle.  The "rule" data
+ * structure also follow this life cycle with some additional elaborations
+ * described under "Rule Life Cycle" below.
+ *
  * Any instance of a given data structure goes through the following life
  * cycle:
  *
@@ -516,8 +521,16 @@ struct ofproto_class {
      * must complete all of them by calling ofoperation_complete().
      *
      * ->destruct() must also destroy all remaining rules in the ofproto's
-     * tables, by passing each remaining rule to ofproto_rule_destroy().  The
-     * client will destroy the flow tables themselves after ->destruct()
+     * tables, by passing each remaining rule to ofproto_rule_delete(), and
+     * then complete each of those deletions in turn by calling
+     * ofoperation_complete().
+     *
+     * (Thus, there is a multi-step process for any rule currently being
+     * inserted or modified at the beginning of destruction: first
+     * ofoperation_complete() that operation, then ofproto_rule_delete() the
+     * rule, then ofoperation_complete() the deletion operation.)
+     *
+     * The client will destroy the flow tables themselves after ->destruct()
      * returns.
      */
     struct ofproto *(*alloc)(void);
@@ -872,30 +885,62 @@ struct ofproto_class {
                                      const struct match *match,
                                      uint8_t *table_idp);
 
-    /* Life-cycle functions for a "struct rule" (see "Life Cycle" above).
+    /* Life-cycle functions for a "struct rule".
+     *
+     *
+     * Rule Life Cycle
+     * ===============
+     *
+     * The life cycle of a struct rule is an elaboration of the basic life
+     * cycle described above under "Life Cycle".
+     *
+     * After a rule is successfully constructed, it is then inserted.  If
+     * insertion completes successfully, then before it is later destructed, it
+     * is deleted.
+     *
+     * You can think of a rule as having the following extra steps inserted
+     * between "Life Cycle" steps 4 and 5:
+     *
+     *   4.1. The client inserts the rule into the flow table, making it
+     *        visible in flow table lookups.
+     *
+     *   4.2. The client calls "rule_insert".  Immediately or eventually, the
+     *        implementation calls ofoperation_complete() to indicate that the
+     *        insertion completed.  If the operation failed, skip to step 5.
+     *
+     *   4.3. The rule is now installed in the flow table.  Eventually it will
+     *        be deleted.
+     *
+     *   4.4. The client removes the rule from the flow table.  It is no longer
+     *        visible in flow table lookups.
+     *
+     *   4.5. The client calls "rule_delete".  Immediately or eventually, the
+     *        implementation calls ofoperation_complete() to indicate that the
+     *        deletion completed.  Deletion is not allowed to fail, so it must
+     *        be successful.
      *
      *
      * Asynchronous Operation Support
      * ==============================
      *
-     * The life-cycle operations on rules can operate asynchronously, meaning
-     * that ->rule_construct() and ->rule_destruct() only need to initiate
-     * their respective operations and do not need to wait for them to complete
-     * before they return.  ->rule_modify_actions() also operates
-     * asynchronously.
+     * The "insert" and "delete" life-cycle operations on rules can operate
+     * asynchronously, meaning that ->rule_insert() and ->rule_delete() only
+     * need to initiate their respective operations and do not need to wait for
+     * them to complete before they return.  ->rule_modify_actions() also
+     * operates asynchronously.
      *
      * An ofproto implementation reports the success or failure of an
      * asynchronous operation on a rule using the rule's 'pending' member,
      * which points to a opaque "struct ofoperation" that represents the
-     * ongoing opreation.  When the operation completes, the ofproto
+     * ongoing operation.  When the operation completes, the ofproto
      * implementation calls ofoperation_complete(), passing the ofoperation and
      * an error indication.
      *
      * Only the following contexts may call ofoperation_complete():
      *
-     *   - The function called to initiate the operation,
-     *     e.g. ->rule_construct() or ->rule_destruct().  This is the best
-     *     choice if the operation completes quickly.
+     *   - The function called to initiate the operation, e.g. ->rule_insert()
+     *     or ->rule_delete().  This is the best choice if the operation
+     *     completes quickly.
      *
      *   - The implementation's ->run() function.
      *
@@ -904,22 +949,22 @@ struct ofproto_class {
      * The ofproto base code updates the flow table optimistically, assuming
      * that the operation will probably succeed:
      *
-     *   - ofproto adds or replaces the rule in the flow table before calling
-     *     ->rule_construct().
+     *   - ofproto adds the rule in the flow table before calling
+     *     ->rule_insert().
      *
-     *   - ofproto updates the rule's actions before calling
-     *     ->rule_modify_actions().
+     *   - ofproto updates the rule's actions and other properties before
+     *     calling ->rule_modify_actions().
      *
-     *   - ofproto removes the rule before calling ->rule_destruct().
+     *   - ofproto removes the rule before calling ->rule_delete().
      *
      * With one exception, when an asynchronous operation completes with an
      * error, ofoperation_complete() backs out the already applied changes:
      *
-     *   - If adding or replacing a rule in the flow table fails, ofproto
-     *     removes the new rule or restores the original rule.
+     *   - If adding a rule in the flow table fails, ofproto removes the new
+     *     rule.
      *
-     *   - If modifying a rule's actions fails, ofproto restores the original
-     *     actions.
+     *   - If modifying a rule fails, ofproto restores the original actions
+     *     (and other properties).
      *
      *   - Removing a rule is not allowed to fail.  It must always succeed.
      *
@@ -935,73 +980,77 @@ struct ofproto_class {
      * Construction
      * ============
      *
-     * When ->rule_construct() is called, the caller has already inserted
-     * 'rule' into 'rule->ofproto''s flow table numbered 'rule->table_id'.
-     * There are two cases:
+     * When ->rule_construct() is called, 'rule' is a new rule that is not yet
+     * inserted into a flow table.  ->rule_construct() should initialize enough
+     * of the rule's derived state for 'rule' to be suitable for inserting into
+     * a flow table.  ->rule_construct() should not modify any base members of
+     * struct rule.
      *
-     *   - 'rule' is a new rule in its flow table.  In this case,
-     *     ofoperation_get_victim(rule) returns NULL.
+     * If ->rule_construct() fails (as indicated by returning a nonzero
+     * OpenFlow error code), the ofproto base code will uninitialize and
+     * deallocate 'rule'.  See "Rule Life Cycle" above for more details.
      *
-     *   - 'rule' is replacing an existing rule in its flow table that had the
-     *     same matching criteria and priority.  In this case,
-     *     ofoperation_get_victim(rule) returns the rule being replaced (the
-     *     "victim" rule).
+     * ->rule_construct() may also:
      *
-     * ->rule_construct() should set the following in motion:
-     *
-     *   - Validate that the matching rule in 'rule->cr' is supported by the
+     *   - Validate that the datapath supports the matching rule in 'rule->cr'
      *     datapath.  For example, if the rule's table does not support
      *     registers, then it is an error if 'rule->cr' does not wildcard all
      *     registers.
      *
      *   - Validate that the datapath can correctly implement 'rule->ofpacts'.
      *
-     *   - If the rule is valid, update the datapath flow table, adding the new
-     *     rule or replacing the existing one.
+     * Some implementations might need to defer these tasks to ->rule_insert(),
+     * which is also acceptable.
+     *
      *
-     *   - If 'rule' is replacing an existing rule, uninitialize any derived
-     *     state for the victim rule, as in step 5 in the "Life Cycle"
-     *     described above.
+     * Insertion
+     * =========
      *
-     * (On failure, the ofproto code will roll back the insertion from the flow
-     * table, either removing 'rule' or replacing it by the victim rule if
-     * there is one.)
+     * Following successful construction, the ofproto base case inserts 'rule'
+     * into its flow table, then it calls ->rule_insert().  ->rule_insert()
+     * should set in motion adding the new rule to the datapath flow table.  It
+     * must act as follows:
      *
-     * ->rule_construct() must act in one of the following ways:
+     *   - If it completes insertion, either by succeeding or failing, it must
+     *     call ofoperation_complete()
      *
-     *   - If it succeeds, it must call ofoperation_complete() and return 0.
+     *   - If insertion is only partially complete, then it must return without
+     *     calling ofoperation_complete().  Later, when the insertion is
+     *     complete, the ->run() or ->destruct() function must call
+     *     ofoperation_complete() to report success or failure.
      *
-     *   - If it fails, it must act in one of the following ways:
+     * If ->rule_insert() fails, the ofproto base code will remove 'rule' from
+     * the flow table, destruct, uninitialize, and deallocate 'rule'.  See
+     * "Rule Life Cycle" above for more details.
      *
-     *       * Call ofoperation_complete() and return 0.
      *
-     *       * Return an OpenFlow error code.  (Do not call
-     *         ofoperation_complete() in this case.)
+     * Deletion
+     * ========
      *
-     *     Either way, ->rule_destruct() will not be called for 'rule', but
-     *     ->rule_dealloc() will be.
+     * The ofproto base code removes 'rule' from its flow table before it calls
+     * ->rule_delete().  ->rule_delete() should set in motion removing 'rule'
+     * from the datapath flow table.  It must act as follows:
      *
-     *   - If the operation is only partially complete, then it must return 0.
-     *     Later, when the operation is complete, the ->run() or ->destruct()
-     *     function must call ofoperation_complete() to report success or
-     *     failure.
+     *   - If it completes deletion, it must call ofoperation_complete().
      *
-     * ->rule_construct() should not modify any base members of struct rule.
+     *   - If deletion is only partially complete, then it must return without
+     *     calling ofoperation_complete().  Later, when the deletion is
+     *     complete, the ->run() or ->destruct() function must call
+     *     ofoperation_complete().
+     *
+     * Rule deletion must not fail.
      *
      *
      * Destruction
      * ===========
      *
-     * When ->rule_destruct() is called, the caller has already removed 'rule'
-     * from 'rule->ofproto''s flow table.  ->rule_destruct() should set in
-     * motion removing 'rule' from the datapath flow table.  If removal
-     * completes synchronously, it should call ofoperation_complete().
-     * Otherwise, the ->run() or ->destruct() function must later call
-     * ofoperation_complete() after the operation completes.
+     * ->rule_destruct() must uninitialize derived state.
      *
      * Rule destruction must not fail. */
     struct rule *(*rule_alloc)(void);
     enum ofperr (*rule_construct)(struct rule *rule);
+    void (*rule_insert)(struct rule *rule);
+    void (*rule_delete)(struct rule *rule);
     void (*rule_destruct)(struct rule *rule);
     void (*rule_dealloc)(struct rule *rule);
 
@@ -1023,6 +1072,7 @@ struct ofproto_class {
      * flow->tunnel and flow->in_port, which are assigned the correct values
      * for the incoming packet.  The register values are zeroed.  'packet''s
      * header pointers (e.g. packet->l3) are appropriately initialized.
+     * packet->l3 is aligned on a 32-bit boundary.
      *
      * The implementation should add the statistics for 'packet' into 'rule'.
      *
@@ -1041,6 +1091,10 @@ struct ofproto_class {
      *
      *   - Update the datapath flow table with the new actions.
      *
+     *   - Only if 'reset_counters' is true, reset any packet or byte counters
+     *     associated with the rule to zero, so that rule_get_stats() will not
+     *     longer count those packets or bytes.
+     *
      * If the operation synchronously completes, ->rule_modify_actions() may
      * call ofoperation_complete() before it returns.  Otherwise, ->run()
      * should call ofoperation_complete() later, after the operation does
@@ -1051,7 +1105,7 @@ struct ofproto_class {
      *
      * ->rule_modify_actions() should not modify any base members of struct
      * rule. */
-    void (*rule_modify_actions)(struct rule *rule);
+    void (*rule_modify_actions)(struct rule *rule, bool reset_counters);
 
     /* Changes the OpenFlow IP fragment handling policy to 'frag_handling',
      * which takes one of the following values, with the corresponding
index 709fd07..1173936 100644 (file)
@@ -76,7 +76,8 @@ enum ofproto_state {
 enum ofoperation_type {
     OFOPERATION_ADD,
     OFOPERATION_DELETE,
-    OFOPERATION_MODIFY
+    OFOPERATION_MODIFY,
+    OFOPERATION_REPLACE
 };
 
 /* A single OpenFlow request can execute any number of operations.  The
@@ -121,10 +122,8 @@ struct ofoperation {
     struct rule *rule;          /* Rule being operated upon. */
     enum ofoperation_type type; /* Type of operation. */
 
-    /* OFOPERATION_ADD. */
-    struct rule *victim;        /* Rule being replaced, if any.. */
-
-    /* OFOPERATION_MODIFY: The old actions, if the actions are changing. */
+    /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
+     * are changing. */
     struct ofpact *ofpacts;
     size_t ofpacts_len;
     uint32_t meter_id;
@@ -133,6 +132,9 @@ struct ofoperation {
     enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
 
     ovs_be64 flow_cookie;       /* Rule's old flow cookie. */
+    uint16_t idle_timeout;      /* Rule's old idle timeout. */
+    uint16_t hard_timeout;      /* Rule's old hard timeout. */
+    bool send_flow_removed;     /* Rule's old 'send_flow_removed'. */
     enum ofperr error;          /* 0 if no error. */
 };
 
@@ -157,8 +159,7 @@ static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->evict);
 static void oftable_remove_rule__(struct ofproto *ofproto,
                                   struct classifier *cls, struct rule *rule)
     OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->evict);
-static struct rule *oftable_replace_rule(struct rule *);
-static void oftable_substitute_rule(struct rule *old, struct rule *new);
+static void oftable_insert_rule(struct rule *);
 
 /* A set of rules within a single OpenFlow table (oftable) that have the same
  * values for the oftable's eviction_fields.  A rule to be evicted, when one is
@@ -186,6 +187,8 @@ static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
     OVS_TRY_WRLOCK(true, (*rulep)->evict);
 static void ofproto_evict(struct ofproto *);
 static uint32_t rule_eviction_priority(struct rule *);
+static void eviction_group_add_rule(struct rule *);
+static void eviction_group_remove_rule(struct rule *);
 
 /* ofport. */
 static void ofport_destroy__(struct ofport *);
@@ -204,6 +207,9 @@ static bool rule_is_modifiable(const struct rule *);
 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
                             struct ofputil_flow_mod *,
                             const struct ofp_header *);
+static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
+                                  struct ofputil_flow_mod *,
+                                  const struct ofp_header *, struct list *);
 static void delete_flow__(struct rule *rule, struct ofopgroup *,
                           enum ofp_flow_removed_reason)
     OVS_RELEASES(rule->evict);
@@ -1068,17 +1074,37 @@ ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
     connmgr_get_snoops(ofproto->connmgr, snoops);
 }
 
+/* Deletes 'rule' from 'cls' within 'ofproto'.
+ *
+ * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls)
+ * but it allows Clang to do better checking. */
 static void
-ofproto_flush__(struct ofproto *ofproto)
+ofproto_delete_rule(struct ofproto *ofproto, struct classifier *cls,
+                    struct rule *rule)
+    OVS_REQ_WRLOCK(cls->rwlock)
 {
     struct ofopgroup *group;
+
+    ovs_assert(!rule->pending);
+    ovs_assert(cls == &ofproto->tables[rule->table_id].cls);
+
+    group = ofopgroup_create_unattached(ofproto);
+    ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
+    ovs_rwlock_wrlock(&rule->evict);
+    oftable_remove_rule__(ofproto, cls, rule);
+    ofproto->ofproto_class->rule_delete(rule);
+    ofopgroup_submit(group);
+}
+
+static void
+ofproto_flush__(struct ofproto *ofproto)
+{
     struct oftable *table;
 
     if (ofproto->ofproto_class->flush) {
         ofproto->ofproto_class->flush(ofproto);
     }
 
-    group = ofopgroup_create_unattached(ofproto);
     OFPROTO_FOR_EACH_TABLE (table, ofproto) {
         struct rule *rule, *next_rule;
         struct cls_cursor cursor;
@@ -1091,16 +1117,11 @@ ofproto_flush__(struct ofproto *ofproto)
         cls_cursor_init(&cursor, &table->cls, NULL);
         CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
             if (!rule->pending) {
-                ofoperation_create(group, rule, OFOPERATION_DELETE,
-                                   OFPRR_DELETE);
-                ovs_rwlock_wrlock(&rule->evict);
-                oftable_remove_rule__(ofproto, &table->cls, rule);
-                ofproto->ofproto_class->rule_destruct(rule);
+                ofproto_delete_rule(ofproto, &table->cls, rule);
             }
         }
         ovs_rwlock_unlock(&table->cls.rwlock);
     }
-    ofopgroup_submit(group);
 }
 
 static void
@@ -1678,12 +1699,13 @@ bool
 ofproto_delete_flow(struct ofproto *ofproto,
                     const struct match *target, unsigned int priority)
 {
+    struct classifier *cls = &ofproto->tables[0].cls;
     struct rule *rule;
 
-    ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
-    rule = rule_from_cls_rule(classifier_find_match_exactly(
-                                  &ofproto->tables[0].cls, target, priority));
-    ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
+    ovs_rwlock_rdlock(&cls->rwlock);
+    rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
+                                                            priority));
+    ovs_rwlock_unlock(&cls->rwlock);
     if (!rule) {
         /* No such rule -> success. */
         return true;
@@ -1693,12 +1715,10 @@ ofproto_delete_flow(struct ofproto *ofproto,
         return false;
     } else {
         /* Initiate deletion -> success. */
-        struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
-        ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
-        ovs_rwlock_wrlock(&rule->evict);
-        oftable_remove_rule(rule);
-        ofproto->ofproto_class->rule_destruct(rule);
-        ofopgroup_submit(group);
+        ovs_rwlock_wrlock(&cls->rwlock);
+        ofproto_delete_rule(ofproto, cls, rule);
+        ovs_rwlock_unlock(&cls->rwlock);
+
         return true;
     }
 
@@ -2196,6 +2216,7 @@ static void
 ofproto_rule_destroy__(struct rule *rule)
 {
     if (rule) {
+        rule->ofproto->ofproto_class->rule_destruct(rule);
         cls_rule_destroy(&rule->cr);
         free(rule->ofpacts);
         ovs_mutex_destroy(&rule->timeout_mutex);
@@ -2205,24 +2226,18 @@ ofproto_rule_destroy__(struct rule *rule)
 }
 
 /* This function allows an ofproto implementation to destroy any rules that
- * remain when its ->destruct() function is called.  The caller must have
- * already uninitialized any derived members of 'rule' (step 5 described in the
- * large comment in ofproto/ofproto-provider.h titled "Life Cycle").
- * This function implements steps 6 and 7.
+ * remain when its ->destruct() function is called..  This function implements
+ * steps 4.4 and 4.5 in the section titled "Rule Life Cycle" in
+ * ofproto-provider.h.
  *
  * This function should only be called from an ofproto implementation's
  * ->destruct() function.  It is not suitable elsewhere. */
 void
-ofproto_rule_destroy(struct ofproto *ofproto, struct classifier *cls,
-                     struct rule *rule) OVS_REQ_WRLOCK(cls->rwlock)
+ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls,
+                    struct rule *rule)
+    OVS_REQ_WRLOCK(cls->rwlock)
 {
-    ovs_assert(!rule->pending);
-    if (!ovs_rwlock_trywrlock(&rule->evict)) {
-        oftable_remove_rule__(ofproto, cls, rule);
-    } else {
-        NOT_REACHED();
-    }
-    ofproto_rule_destroy__(rule);
+    ofproto_delete_rule(ofproto, cls, rule);
 }
 
 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
@@ -2245,12 +2260,11 @@ ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
 
     switch (op->type) {
     case OFOPERATION_ADD:
-        return op->victim && ofproto_rule_has_out_port(op->victim, out_port);
-
     case OFOPERATION_DELETE:
         return false;
 
     case OFOPERATION_MODIFY:
+    case OFOPERATION_REPLACE:
         return ofpacts_output_to_port(op->ofpacts, op->ofpacts_len, out_port);
     }
 
@@ -2258,8 +2272,7 @@ ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
 }
 
 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
- * statistics appropriately.  'packet' must have at least sizeof(struct
- * ofp10_packet_in) bytes of headroom.
+ * statistics appropriately.
  *
  * 'packet' doesn't necessarily have to match 'rule'.  'rule' will be credited
  * with statistics for 'packet' either way.
@@ -2271,8 +2284,6 @@ rule_execute(struct rule *rule, ofp_port_t in_port, struct ofpbuf *packet)
     struct flow flow;
     union flow_in_port in_port_;
 
-    ovs_assert(ofpbuf_headroom(packet) >= sizeof(struct ofp10_packet_in));
-
     in_port_.ofp_port = in_port;
     flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
     return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
@@ -2514,8 +2525,8 @@ handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
             goto exit_free_ofpacts;
         }
     } else {
-        payload = xmalloc(sizeof *payload);
-        ofpbuf_use_const(payload, po.packet, po.packet_len);
+        /* Ensure that the L3 header is 32-bit aligned. */
+        payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
     }
 
     /* Verify actions against packet, then send packet if successful. */
@@ -3072,17 +3083,17 @@ handle_flow_stats_request(struct ofconn *ofconn,
                                                &fs.byte_count);
         fs.ofpacts = rule->ofpacts;
         fs.ofpacts_len = rule->ofpacts_len;
-        fs.flags = 0;
 
         ovs_mutex_lock(&rule->timeout_mutex);
         fs.idle_timeout = rule->idle_timeout;
         fs.hard_timeout = rule->hard_timeout;
         ovs_mutex_unlock(&rule->timeout_mutex);
 
+        fs.flags = 0;
         if (rule->send_flow_removed) {
-            fs.flags |= OFPFF_SEND_FLOW_REM;
-            /* FIXME: Implement OF 1.3 flags OFPFF13_NO_PKT_COUNTS
-               and OFPFF13_NO_BYT_COUNTS */
+            fs.flags |= OFPUTIL_FF_SEND_FLOW_REM;
+            /* FIXME: Implement OFPUTIL_FF_NO_PKT_COUNTS and
+               OFPUTIL_FF_NO_BYT_COUNTS. */
         }
         ofputil_append_flow_stats_reply(&fs, &replies);
     }
@@ -3337,6 +3348,34 @@ is_flow_deletion_pending(const struct ofproto *ofproto,
     return false;
 }
 
+static enum ofperr
+evict_rule_from_table(struct ofproto *ofproto, struct oftable *table)
+{
+    struct rule *rule;
+    size_t n_rules;
+
+    ovs_rwlock_rdlock(&table->cls.rwlock);
+    n_rules = classifier_count(&table->cls);
+    ovs_rwlock_unlock(&table->cls.rwlock);
+
+    if (n_rules < table->max_flows) {
+        return 0;
+    } else if (!choose_rule_to_evict(table, &rule)) {
+        return OFPERR_OFPFMFC_TABLE_FULL;
+    } else if (rule->pending) {
+        ovs_rwlock_unlock(&rule->evict);
+        return OFPROTO_POSTPONE;
+    } else {
+        struct ofopgroup *group;
+
+        group = ofopgroup_create_unattached(ofproto);
+        delete_flow__(rule, group, OFPRR_EVICTION);
+        ofopgroup_submit(group);
+
+        return 0;
+    }
+}
+
 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
  * in which no matching flow already exists in the flow table.
  *
@@ -3356,7 +3395,7 @@ add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
 {
     struct oftable *table;
     struct ofopgroup *group;
-    struct rule *victim;
+    struct cls_rule cr;
     struct rule *rule;
     uint8_t table_id;
     int error;
@@ -3391,6 +3430,28 @@ add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
         return OFPERR_OFPBRC_EPERM;
     }
 
+    cls_rule_init(&cr, &fm->match, fm->priority);
+
+    /* Transform "add" into "modify" if there's an existing identical flow. */
+    ovs_rwlock_rdlock(&table->cls.rwlock);
+    rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
+    ovs_rwlock_unlock(&table->cls.rwlock);
+    if (rule) {
+        cls_rule_destroy(&cr);
+        if (!rule_is_modifiable(rule)) {
+            return OFPERR_OFPBRC_EPERM;
+        } else if (rule->pending) {
+            return OFPROTO_POSTPONE;
+        } else {
+            struct list rules;
+
+            list_init(&rules);
+            list_push_back(&rules, &rule->ofproto_node);
+            fm->modify_cookie = true;
+            return modify_flows__(ofproto, ofconn, fm, request, &rules);
+        }
+    }
+
     /* Verify actions. */
     error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len,
                                   &fm->match.flow, table_id);
@@ -3398,40 +3459,45 @@ add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
         return error;
     }
 
-    /* Allocate new rule and initialize classifier rule. */
-    rule = ofproto->ofproto_class->rule_alloc();
-    if (!rule) {
-        VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
-                     ofproto->name, ovs_strerror(error));
-        return ENOMEM;
-    }
-    cls_rule_init(&rule->cr, &fm->match, fm->priority);
-
     /* Serialize against pending deletion. */
-    if (is_flow_deletion_pending(ofproto, &rule->cr, table_id)) {
-        cls_rule_destroy(&rule->cr);
-        ofproto->ofproto_class->rule_dealloc(rule);
+    if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
+        cls_rule_destroy(&cr);
         return OFPROTO_POSTPONE;
     }
 
     /* Check for overlap, if requested. */
-    if (fm->flags & OFPFF_CHECK_OVERLAP) {
+    if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
         bool overlaps;
 
         ovs_rwlock_rdlock(&table->cls.rwlock);
-        overlaps = classifier_rule_overlaps(&table->cls, &rule->cr);
+        overlaps = classifier_rule_overlaps(&table->cls, &cr);
         ovs_rwlock_unlock(&table->cls.rwlock);
 
         if (overlaps) {
-            cls_rule_destroy(&rule->cr);
-            ofproto->ofproto_class->rule_dealloc(rule);
+            cls_rule_destroy(&cr);
             return OFPERR_OFPFMFC_OVERLAP;
         }
     }
 
-    /* FIXME: Implement OFPFF12_RESET_COUNTS */
+    /* If necessary, evict an existing rule to clear out space. */
+    error = evict_rule_from_table(ofproto, table);
+    if (error) {
+        cls_rule_destroy(&cr);
+        return error;
+    }
 
+    /* Allocate new rule. */
+    rule = ofproto->ofproto_class->rule_alloc();
+    if (!rule) {
+        cls_rule_destroy(&cr);
+        VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
+                     ofproto->name, ovs_strerror(error));
+        return ENOMEM;
+    }
+
+    /* Initialize base state. */
     rule->ofproto = ofproto;
+    cls_rule_move(&rule->cr, &cr);
     rule->pending = NULL;
     rule->flow_cookie = fm->new_cookie;
     rule->created = rule->modified = rule->used = time_msec();
@@ -3443,9 +3509,7 @@ add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
     ovs_mutex_unlock(&rule->timeout_mutex);
 
     rule->table_id = table - ofproto->tables;
-    rule->send_flow_removed = (fm->flags & OFPFF_SEND_FLOW_REM) != 0;
-    /* FIXME: Implement OF 1.3 flags OFPFF13_NO_PKT_COUNTS
-       and OFPFF13_NO_BYT_COUNTS */
+    rule->send_flow_removed = (fm->flags & OFPUTIL_FF_SEND_FLOW_REM) != 0;
     rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
     rule->ofpacts_len = fm->ofpacts_len;
     rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
@@ -3457,65 +3521,21 @@ add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
     rule->modify_seqno = 0;
     ovs_rwlock_init(&rule->evict);
 
-    /* Insert new rule. */
-    victim = oftable_replace_rule(rule);
-    if (victim && !rule_is_modifiable(victim)) {
-        error = OFPERR_OFPBRC_EPERM;
-    } else if (victim && victim->pending) {
-        error = OFPROTO_POSTPONE;
-    } else {
-        struct ofoperation *op;
-        struct rule *evict;
-        size_t n_rules;
-
-        ovs_rwlock_rdlock(&table->cls.rwlock);
-        n_rules = classifier_count(&table->cls);
-        ovs_rwlock_unlock(&table->cls.rwlock);
-        if (n_rules > table->max_flows) {
-            ovs_rwlock_rdlock(&rule->evict);
-            if (choose_rule_to_evict(table, &evict)) {
-                ovs_rwlock_unlock(&rule->evict);
-                ovs_rwlock_unlock(&evict->evict);
-                if (evict->pending) {
-                    error = OFPROTO_POSTPONE;
-                    goto exit;
-                }
-            } else {
-                ovs_rwlock_unlock(&rule->evict);
-                error = OFPERR_OFPFMFC_TABLE_FULL;
-                goto exit;
-            }
-        } else {
-            evict = NULL;
-        }
-
-        group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
-        op = ofoperation_create(group, rule, OFOPERATION_ADD, 0);
-        op->victim = victim;
-
-        error = ofproto->ofproto_class->rule_construct(rule);
-        if (error) {
-            op->group->n_running--;
-            ofoperation_destroy(rule->pending);
-        } else if (evict) {
-            /* It would be better if we maintained the lock we took in
-             * choose_rule_to_evict() earlier, but that confuses the thread
-             * safety analysis, and this code is fragile enough that we really
-             * need it.  In the worst case, we'll have to block a little while
-             * before we perform the eviction, which doesn't seem like a big
-             * problem. */
-            ovs_rwlock_wrlock(&evict->evict);
-            delete_flow__(evict, group, OFPRR_EVICTION);
-        }
-        ofopgroup_submit(group);
-    }
-
-exit:
-    /* Back out if an error occurred. */
+    /* Construct rule, initializing derived state. */
+    error = ofproto->ofproto_class->rule_construct(rule);
     if (error) {
-        oftable_substitute_rule(rule, victim);
         ofproto_rule_destroy__(rule);
+        return error;
     }
+
+    /* Insert rule. */
+    oftable_insert_rule(rule);
+
+    group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
+    ofoperation_create(group, rule, OFOPERATION_ADD, 0);
+    ofproto->ofproto_class->rule_insert(rule);
+    ofopgroup_submit(group);
+
     return error;
 }
 \f
@@ -3533,17 +3553,20 @@ modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
                struct ofputil_flow_mod *fm, const struct ofp_header *request,
                struct list *rules)
 {
+    enum ofoperation_type type;
     struct ofopgroup *group;
     struct rule *rule;
     enum ofperr error;
 
+    type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
     group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
     error = OFPERR_OFPBRC_EPERM;
     LIST_FOR_EACH (rule, ofproto_node, rules) {
         struct ofoperation *op;
         bool actions_changed;
+        bool reset_counters;
 
-        /* FIXME: Implement OFPFF12_RESET_COUNTS */
+        /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
 
         if (rule_is_modifiable(rule)) {
             /* At least one rule is modifiable, don't report EPERM error. */
@@ -3562,19 +3585,39 @@ modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
         actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
                                          rule->ofpacts, rule->ofpacts_len);
 
-        op = ofoperation_create(group, rule, OFOPERATION_MODIFY, 0);
+        op = ofoperation_create(group, rule, type, 0);
 
         if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) {
             ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
         }
-        if (actions_changed) {
+        if (type == OFOPERATION_REPLACE) {
+            ovs_mutex_lock(&rule->timeout_mutex);
+            rule->idle_timeout = fm->idle_timeout;
+            rule->hard_timeout = fm->hard_timeout;
+            ovs_mutex_unlock(&rule->timeout_mutex);
+
+            rule->send_flow_removed = (fm->flags
+                                       & OFPUTIL_FF_SEND_FLOW_REM) != 0;
+
+            if (fm->idle_timeout || fm->hard_timeout) {
+                if (!rule->eviction_group) {
+                    eviction_group_add_rule(rule);
+                }
+            } else {
+                eviction_group_remove_rule(rule);
+            }
+        }
+
+        reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
+        if (actions_changed || reset_counters) {
             op->ofpacts = rule->ofpacts;
             op->ofpacts_len = rule->ofpacts_len;
             op->meter_id = rule->meter_id;
             rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
             rule->ofpacts_len = fm->ofpacts_len;
             rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
-            rule->ofproto->ofproto_class->rule_modify_actions(rule);
+            rule->ofproto->ofproto_class->rule_modify_actions(rule,
+                                                              reset_counters);
         } else {
             ofoperation_complete(op, 0);
         }
@@ -3659,7 +3702,7 @@ delete_flow__(struct rule *rule, struct ofopgroup *group,
 
     ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
     oftable_remove_rule(rule);
-    ofproto->ofproto_class->rule_destruct(rule);
+    ofproto->ofproto_class->rule_delete(rule);
 }
 
 /* Deletes the rules listed in 'rules'.
@@ -3773,17 +3816,54 @@ void
 ofproto_rule_expire(struct rule *rule, uint8_t reason)
 {
     struct ofproto *ofproto = rule->ofproto;
-    struct ofopgroup *group;
+    struct classifier *cls = &ofproto->tables[rule->table_id].cls;
 
     ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT);
-
     ofproto_rule_send_removed(rule, reason);
 
-    group = ofopgroup_create_unattached(ofproto);
-    ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
-    oftable_remove_rule(rule);
-    ofproto->ofproto_class->rule_destruct(rule);
-    ofopgroup_submit(group);
+    ovs_rwlock_wrlock(&cls->rwlock);
+    ofproto_delete_rule(ofproto, cls, rule);
+    ovs_rwlock_unlock(&cls->rwlock);
+}
+
+/* Reduces '*timeout' to no more than 'max'.  A value of zero in either case
+ * means "infinite". */
+static void
+reduce_timeout(uint16_t max, uint16_t *timeout)
+{
+    if (max && (!*timeout || *timeout > max)) {
+        *timeout = max;
+    }
+}
+
+/* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
+ * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
+ * 'idle_timeout' seconds.  Similarly for 'hard_timeout'.
+ *
+ * Suitable for implementing OFPACT_FIN_TIMEOUT. */
+void
+ofproto_rule_reduce_timeouts(struct rule *rule,
+                             uint16_t idle_timeout, uint16_t hard_timeout)
+    OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex)
+{
+    if (!idle_timeout && !hard_timeout) {
+        return;
+    }
+
+    ovs_mutex_lock(&rule->ofproto->expirable_mutex);
+    if (list_is_empty(&rule->expirable)) {
+        list_insert(&rule->ofproto->expirable, &rule->expirable);
+    }
+    ovs_mutex_unlock(&rule->ofproto->expirable_mutex);
+
+    ovs_mutex_lock(&rule->timeout_mutex);
+    reduce_timeout(idle_timeout, &rule->idle_timeout);
+    reduce_timeout(hard_timeout, &rule->hard_timeout);
+    ovs_mutex_unlock(&rule->timeout_mutex);
+
+    if (!rule->eviction_group) {
+        eviction_group_add_rule(rule);
+    }
 }
 \f
 static enum ofperr
@@ -4036,7 +4116,7 @@ ofproto_compose_flow_refresh_update(const struct rule *rule,
     struct ofputil_flow_update fu;
     struct match match;
 
-    if (op && op->type == OFOPERATION_ADD && !op->victim) {
+    if (op && op->type == OFOPERATION_ADD) {
         /* We'll report the final flow when the operation completes.  Reporting
          * it now would cause a duplicate report later. */
         return;
@@ -4065,12 +4145,10 @@ ofproto_compose_flow_refresh_update(const struct rule *rule,
          * actions, so that when the operation commits we report the change. */
         switch (op->type) {
         case OFOPERATION_ADD:
-            /* We already verified that there was a victim. */
-            fu.ofpacts = op->victim->ofpacts;
-            fu.ofpacts_len = op->victim->ofpacts_len;
-            break;
+            NOT_REACHED();
 
         case OFOPERATION_MODIFY:
+        case OFOPERATION_REPLACE:
             if (op->ofpacts) {
                 fu.ofpacts = op->ofpacts;
                 fu.ofpacts_len = op->ofpacts_len;
@@ -4881,15 +4959,27 @@ ofopgroup_complete(struct ofopgroup *group)
                   && rule->flow_cookie == op->flow_cookie))) {
             /* Check that we can just cast from ofoperation_type to
              * nx_flow_update_event. */
-            BUILD_ASSERT_DECL((enum nx_flow_update_event) OFOPERATION_ADD
-                              == NXFME_ADDED);
-            BUILD_ASSERT_DECL((enum nx_flow_update_event) OFOPERATION_DELETE
-                              == NXFME_DELETED);
-            BUILD_ASSERT_DECL((enum nx_flow_update_event) OFOPERATION_MODIFY
-                              == NXFME_MODIFIED);
-
-            ofmonitor_report(ofproto->connmgr, rule,
-                             (enum nx_flow_update_event) op->type,
+            enum nx_flow_update_event event_type;
+
+            switch (op->type) {
+            case OFOPERATION_ADD:
+            case OFOPERATION_REPLACE:
+                event_type = NXFME_ADDED;
+                break;
+
+            case OFOPERATION_DELETE:
+                event_type = NXFME_DELETED;
+                break;
+
+            case OFOPERATION_MODIFY:
+                event_type = NXFME_MODIFIED;
+                break;
+
+            default:
+                NOT_REACHED();
+            }
+
+            ofmonitor_report(ofproto->connmgr, rule, event_type,
                              op->reason, abbrev_ofconn, abbrev_xid);
         }
 
@@ -4900,7 +4990,6 @@ ofopgroup_complete(struct ofopgroup *group)
             if (!op->error) {
                 uint16_t vid_mask;
 
-                ofproto_rule_destroy__(op->victim);
                 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
                 if (vid_mask == VLAN_VID_MASK) {
                     if (ofproto->vlan_bitmap) {
@@ -4914,7 +5003,8 @@ ofopgroup_complete(struct ofopgroup *group)
                     }
                 }
             } else {
-                oftable_substitute_rule(rule, op->victim);
+                ovs_rwlock_wrlock(&rule->evict);
+                oftable_remove_rule(rule);
                 ofproto_rule_destroy__(rule);
             }
             break;
@@ -4926,10 +5016,20 @@ ofopgroup_complete(struct ofopgroup *group)
             break;
 
         case OFOPERATION_MODIFY:
+        case OFOPERATION_REPLACE:
             if (!op->error) {
-                rule->modified = time_msec();
+                long long int now = time_msec();
+
+                rule->modified = now;
+                if (op->type == OFOPERATION_REPLACE) {
+                    rule->created = rule->used = now;
+                }
             } else {
                 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
+                ovs_mutex_lock(&rule->timeout_mutex);
+                rule->idle_timeout = op->idle_timeout;
+                rule->hard_timeout = op->hard_timeout;
+                ovs_mutex_unlock(&rule->timeout_mutex);
                 if (op->ofpacts) {
                     free(rule->ofpacts);
                     rule->ofpacts = op->ofpacts;
@@ -4937,6 +5037,7 @@ ofopgroup_complete(struct ofopgroup *group)
                     op->ofpacts = NULL;
                     op->ofpacts_len = 0;
                 }
+                rule->send_flow_removed = op->send_flow_removed;
             }
             break;
 
@@ -4990,6 +5091,11 @@ ofoperation_create(struct ofopgroup *group, struct rule *rule,
     op->type = type;
     op->reason = reason;
     op->flow_cookie = rule->flow_cookie;
+    ovs_mutex_lock(&rule->timeout_mutex);
+    op->idle_timeout = rule->idle_timeout;
+    op->hard_timeout = rule->hard_timeout;
+    ovs_mutex_unlock(&rule->timeout_mutex);
+    op->send_flow_removed = rule->send_flow_removed;
 
     group->n_running++;
 
@@ -5021,14 +5127,7 @@ ofoperation_destroy(struct ofoperation *op)
  * indicate success or an OpenFlow error code on failure.
  *
  * If 'error' is 0, indicating success, the operation will be committed
- * permanently to the flow table.  There is one interesting subcase:
- *
- *   - If 'op' is an "add flow" operation that is replacing an existing rule in
- *     the flow table (the "victim" rule) by a new one, then the caller must
- *     have uninitialized any derived state in the victim rule, as in step 5 in
- *     the "Life Cycle" in ofproto/ofproto-provider.h.  ofoperation_complete()
- *     performs steps 6 and 7 for the victim rule, most notably by calling its
- *     ->rule_dealloc() function.
+ * permanently to the flow table.
  *
  * If 'error' is nonzero, then generally the operation will be rolled back:
  *
@@ -5060,13 +5159,6 @@ ofoperation_complete(struct ofoperation *op, enum ofperr error)
         ofopgroup_complete(group);
     }
 }
-
-struct rule *
-ofoperation_get_victim(struct ofoperation *op)
-{
-    ovs_assert(op->type == OFOPERATION_ADD);
-    return op->victim;
-}
 \f
 static uint64_t
 pick_datapath_id(const struct ofproto *ofproto)
@@ -5177,7 +5269,7 @@ ofproto_evict(struct ofproto *ofproto)
             ofoperation_create(group, rule,
                                OFOPERATION_DELETE, OFPRR_EVICTION);
             oftable_remove_rule(rule);
-            ofproto->ofproto_class->rule_destruct(rule);
+            ofproto->ofproto_class->rule_delete(rule);
         }
     }
     ofopgroup_submit(group);
@@ -5508,15 +5600,13 @@ oftable_remove_rule(struct rule *rule)
     ovs_rwlock_unlock(&table->cls.rwlock);
 }
 
-/* Inserts 'rule' into its oftable.  Removes any existing rule from 'rule''s
- * oftable that has an identical cls_rule.  Returns the rule that was removed,
- * if any, and otherwise NULL. */
-static struct rule *
-oftable_replace_rule(struct rule *rule)
+/* Inserts 'rule' into its oftable, which must not already contain any rule for
+ * the same cls_rule. */
+static void
+oftable_insert_rule(struct rule *rule)
 {
     struct ofproto *ofproto = rule->ofproto;
     struct oftable *table = &ofproto->tables[rule->table_id];
-    struct rule *victim;
     bool may_expire;
 
     ovs_mutex_lock(&rule->timeout_mutex);
@@ -5534,35 +5624,9 @@ oftable_replace_rule(struct rule *rule)
         list_insert(&meter->rules, &rule->meter_list_node);
     }
     ovs_rwlock_wrlock(&table->cls.rwlock);
-    victim = rule_from_cls_rule(classifier_replace(&table->cls, &rule->cr));
+    classifier_insert(&table->cls, &rule->cr);
     ovs_rwlock_unlock(&table->cls.rwlock);
-    if (victim) {
-        if (victim->meter_id) {
-            list_remove(&victim->meter_list_node);
-        }
-        cookies_remove(ofproto, victim);
-
-        ovs_mutex_lock(&ofproto->expirable_mutex);
-        if (!list_is_empty(&victim->expirable)) {
-            list_remove(&victim->expirable);
-        }
-        ovs_mutex_unlock(&ofproto->expirable_mutex);
-        eviction_group_remove_rule(victim);
-    }
     eviction_group_add_rule(rule);
-    return victim;
-}
-
-/* Removes 'old' from its oftable then, if 'new' is nonnull, inserts 'new'. */
-static void
-oftable_substitute_rule(struct rule *old, struct rule *new)
-{
-    if (new) {
-        oftable_replace_rule(new);
-    } else {
-        ovs_rwlock_wrlock(&old->evict);
-        oftable_remove_rule(old);
-    }
 }
 \f
 /* unixctl commands. */
index 65fcef6..38ec348 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -119,9 +119,9 @@ pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
     if (++p->cookie >= COOKIE_MAX) {
         p->cookie = 0;
     }
-    p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size,
-                                                sizeof(struct ofp10_packet_in));
 
+    /* Use 2 bytes of headroom to 32-bit align the L3 header. */
+    p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size, 2);
 
     p->timeout = time_msec() + OVERWRITE_MSECS;
     p->in_port = in_port;
@@ -165,8 +165,7 @@ pktbuf_get_null(void)
  *
  * 'in_port' may be NULL if the input port is not of interest.
  *
- * A returned packet will have at least sizeof(struct ofp10_packet_in) bytes of
- * headroom.
+ * The L3 header of a returned packet will be 32-bit aligned.
  *
  * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
 enum ofperr
index 63356b4..62fc9eb 100644 (file)
@@ -29,11 +29,11 @@ AT_DATA([flows.txt], [[
 actions=learn(output:OXM_OF_IN_PORT[])
 actions=learn(table=1, in_port=1, load:OXM_OF_IN_PORT[]->NXM_NX_REG1[], load:0xfffffffe->OXM_OF_IN_PORT[])
 ]])
-AT_CHECK([ovs-ofctl parse-flows flows.txt], [0],
+AT_CHECK([ovs-ofctl -O OpenFlow12 parse-flows flows.txt], [0],
 [[usable protocols: any
-chosen protocol: OpenFlow10-table_id
-OFPT_FLOW_MOD (xid=0x1): ADD actions=learn(table=1,output:OXM_OF_IN_PORT[])
-OFPT_FLOW_MOD (xid=0x2): ADD actions=learn(table=1,in_port=1,load:OXM_OF_IN_PORT[]->NXM_NX_REG1[],load:0xfffffffe->OXM_OF_IN_PORT[])
+chosen protocol: OXM-OpenFlow12
+OFPT_FLOW_MOD (OF1.2) (xid=0x1): ADD table:255 actions=learn(table=1,output:OXM_OF_IN_PORT[])
+OFPT_FLOW_MOD (OF1.2) (xid=0x2): ADD table:255 actions=learn(table=1,in_port=1,load:OXM_OF_IN_PORT[]->NXM_NX_REG1[],load:0xfffffffe->OXM_OF_IN_PORT[])
 ]])
 AT_CLEANUP
 
index af19672..7f01a2d 100644 (file)
@@ -266,7 +266,7 @@ cookie=0x6 table=4 in_port=83 actions=load:4->NXM_NX_REG3[[]],mod_nw_src:83.83.8
 cookie=0x7 table=5 in_port=84 actions=load:5->NXM_NX_REG4[[]],load:6->NXM_NX_TUN_ID[[]],mod_nw_dst:84.84.84.84,controller,resubmit(85,6)
 cookie=0x8 table=6 in_port=85 actions=mod_tp_src:85,controller,resubmit(86,7)
 cookie=0x9 table=7 in_port=86 actions=mod_tp_dst:86,controller,controller
-cookie=0xa dl_src=40:44:44:44:44:41 actions=push_vlan:0x8100,mod_vlan_vid:99,mod_vlan_pcp:1,controller
+cookie=0xa dl_src=40:44:44:44:44:41 actions=mod_vlan_vid:99,mod_vlan_pcp:1,controller
 cookie=0xa dl_src=40:44:44:44:44:42 actions=push_mpls:0x8847,load:10->OXM_OF_MPLS_LABEL[[]],load:3->OXM_OF_MPLS_TC[[]],controller
 cookie=0xa dl_src=40:44:44:44:44:43 actions=push_mpls:0x8847,load:10->OXM_OF_MPLS_LABEL[[]],load:3->OXM_OF_MPLS_TC[[]],controller
 cookie=0xa dl_src=40:44:44:44:44:44 actions=push_mpls:0x8847,load:10->OXM_OF_MPLS_LABEL[[]],load:3->OXM_OF_MPLS_TC[[]],controller
index a0148a6..cbd6aec 100644 (file)
@@ -9,7 +9,7 @@ for test_case in \
     'tun_flags=0                                 none' \
     'tun_tos=0                                   none' \
     'tun_ttl=0                                   none' \
-    'metadata=0                                  NXM,OXM' \
+    'metadata=0                                  NXM,OXM,OpenFlow11' \
     'in_port=1                                   any' \
     'skb_priority=0                              none' \
     'pkt_mark=1                                  NXM,OXM' \
@@ -22,9 +22,9 @@ for test_case in \
     'reg6=6                                      NXM,OXM' \
     'reg7=7                                      NXM,OXM' \
     'dl_src=00:11:22:33:44:55                    any' \
-    'dl_src=00:11:22:33:44:55/00:ff:ff:ff:ff:ff  NXM,OXM' \
+    'dl_src=00:11:22:33:44:55/00:ff:ff:ff:ff:ff  NXM,OXM,OpenFlow11' \
     'dl_dst=00:11:22:33:44:55                    any' \
-    'dl_dst=00:11:22:33:44:55/00:ff:ff:ff:ff:ff  NXM,OXM' \
+    'dl_dst=00:11:22:33:44:55/00:ff:ff:ff:ff:ff  NXM,OXM,OpenFlow11' \
     'dl_type=0x1234                              any' \
     'dl_type=0x0800                              any' \
     'dl_type=0x0806                              any' \
@@ -35,15 +35,15 @@ for test_case in \
     'vlan_vid=11                                 any' \
     'dl_vlan_pcp=6                               any' \
     'vlan_pcp=5                                  any' \
-    'mpls,mpls_label=5                           NXM,OXM' \
-    'mpls,mpls_tc=1                              NXM,OXM' \
+    'mpls,mpls_label=5                           NXM,OXM,OpenFlow11' \
+    'mpls,mpls_tc=1                              NXM,OXM,OpenFlow11' \
     'mpls,mpls_bos=0                             NXM,OXM' \
     'ip,ip_src=1.2.3.4                           any' \
     'ip,ip_src=192.168.0.0/24                    any' \
-    'ip,ip_src=192.0.168.0/255.0.255.0           NXM,OXM' \
+    'ip,ip_src=192.0.168.0/255.0.255.0           NXM,OXM,OpenFlow11' \
     'ip,ip_dst=1.2.3.4                           any' \
     'ip,ip_dst=192.168.0.0/24                    any' \
-    'ip,ip_dst=192.0.168.0/255.0.255.0           NXM,OXM' \
+    'ip,ip_dst=192.0.168.0/255.0.255.0           NXM,OXM,OpenFlow11' \
     'ipv6,ipv6_src=::1                           NXM,OXM' \
     'ipv6,ipv6_dst=::1                           NXM,OXM' \
     'ipv6,ipv6_label=5                           NXM,OXM' \
@@ -113,8 +113,8 @@ udp,nw_src=192.168.0.3,tp_dst=53 actions=pop_queue,output:1
 cookie=0x123456789abcdef hard_timeout=10 priority=60000 actions=controller
 actions=note:41.42.43,note:00.01.02.03.04.05.06.07,note
 ip,actions=set_field:10.4.3.77->ip_src
-sctp actions=set_field:3334->sctp_src
-sctp actions=set_field:4445->sctp_dst
+sctp actions=drop
+sctp actions=drop
 in_port=0 actions=resubmit:0
 actions=sample(probability=12345,collector_set_id=23456,obs_domain_id=34567,obs_point_id=45678)
 ]])
@@ -132,8 +132,8 @@ OFPT_FLOW_MOD: ADD udp,nw_src=192.168.0.3,tp_dst=53 actions=pop_queue,output:1
 OFPT_FLOW_MOD: ADD priority=60000 cookie:0x123456789abcdef hard:10 actions=CONTROLLER:65535
 OFPT_FLOW_MOD: ADD actions=note:41.42.43.00.00.00,note:00.01.02.03.04.05.06.07.00.00.00.00.00.00,note:00.00.00.00.00.00
 OFPT_FLOW_MOD: ADD ip actions=load:0xa04034d->NXM_OF_IP_SRC[]
-OFPT_FLOW_MOD: ADD sctp actions=load:0xd06->OXM_OF_SCTP_SRC[]
-OFPT_FLOW_MOD: ADD sctp actions=load:0x115d->OXM_OF_SCTP_DST[]
+OFPT_FLOW_MOD: ADD sctp actions=drop
+OFPT_FLOW_MOD: ADD sctp actions=drop
 OFPT_FLOW_MOD: ADD in_port=0 actions=resubmit:0
 OFPT_FLOW_MOD: ADD actions=sample(probability=12345,collector_set_id=23456,obs_domain_id=34567,obs_point_id=45678)
 ]])
@@ -159,7 +159,7 @@ actions=sample(probability=12345,collector_set_id=23456,obs_domain_id=34567,obs_
 AT_CHECK([ovs-ofctl --protocols OpenFlow12 parse-flows flows.txt
 ], [0], [stdout])
 AT_CHECK([[sed 's/ (xid=0x[0-9a-fA-F]*)//' stdout]], [0],
-[[usable protocols: any
+[[usable protocols: NXM,OXM
 chosen protocol: OXM-OpenFlow12
 OFPT_FLOW_MOD (OF1.2): ADD table:255 tcp,tp_src=123 actions=FLOOD
 OFPT_FLOW_MOD (OF1.2): ADD table:255 in_port=LOCAL,dl_vlan=9,dl_src=00:0a:e4:25:6b:b0 actions=drop
@@ -2108,7 +2108,7 @@ AT_CHECK([ovs-ofctl -F openflow10 add-flow dummy tun_id=123,actions=drop],
   [1], [], [ovs-ofctl: none of the usable flow formats (NXM,OXM) is among the allowed flow formats (OpenFlow10)
 ])
 AT_CHECK([ovs-ofctl -F openflow10 add-flow dummy metadata=123,actions=drop],
-  [1], [], [ovs-ofctl: none of the usable flow formats (NXM,OXM) is among the allowed flow formats (OpenFlow10)
+  [1], [], [ovs-ofctl: none of the usable flow formats (NXM,OXM,OpenFlow11) is among the allowed flow formats (OpenFlow10)
 ])
 AT_CLEANUP
 
index d9ae983..f487d8c 100644 (file)
@@ -83,6 +83,7 @@ static struct simap port_queues = SIMAP_INITIALIZER(&port_queues);
 /* --with-flows: Flows to send to switch. */
 static struct ofputil_flow_mod *default_flows;
 static size_t n_default_flows;
+static enum ofputil_protocol usable_protocols;
 
 /* --unixctl: Name of unixctl socket, or null to use the default. */
 static char *unixctl_path = NULL;
@@ -216,6 +217,7 @@ new_switch(struct switch_ *sw, struct vconn *vconn)
     cfg.max_idle = set_up_flows ? max_idle : -1;
     cfg.default_flows = default_flows;
     cfg.n_default_flows = n_default_flows;
+    cfg.usable_protocols = usable_protocols;
     cfg.default_queue = default_queue;
     cfg.port_queues = &port_queues;
     cfg.mute = mute;
@@ -329,7 +331,8 @@ parse_options(int argc, char *argv[])
 
         case OPT_WITH_FLOWS:
             error = parse_ofp_flow_mod_file(optarg, OFPFC_ADD, &default_flows,
-                                            &n_default_flows);
+                                            &n_default_flows,
+                                            &usable_protocols);
             if (error) {
                 ovs_fatal(0, "%s", error);
             }
index 899ce4e..547d50c 100644 (file)
@@ -871,13 +871,12 @@ prepare_dump_flows(int argc, char *argv[], bool aggregate,
     char *error;
 
     error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
-                                             argc > 2 ? argv[2] : "");
+                                             argc > 2 ? argv[2] : "",
+                                             &usable_protocols);
     if (error) {
         ovs_fatal(0, "%s", error);
     }
 
-    usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
-
     protocol = open_vconn(argv[1], &vconn);
     protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
     *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
@@ -1031,17 +1030,13 @@ ofctl_queue_stats(int argc, char *argv[])
 }
 
 static enum ofputil_protocol
-open_vconn_for_flow_mod(const char *remote,
-                        const struct ofputil_flow_mod *fms, size_t n_fms,
-                        struct vconn **vconnp)
+open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
+                        enum ofputil_protocol usable_protocols)
 {
-    enum ofputil_protocol usable_protocols;
     enum ofputil_protocol cur_protocol;
     char *usable_s;
     int i;
 
-    /* Figure out what flow formats will work. */
-    usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
     if (!(usable_protocols & allowed_protocols)) {
         char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
         usable_s = ofputil_protocols_to_string(usable_protocols);
@@ -1073,13 +1068,13 @@ open_vconn_for_flow_mod(const char *remote,
 
 static void
 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
-                 size_t n_fms)
+                 size_t n_fms, enum ofputil_protocol usable_protocols)
 {
     enum ofputil_protocol protocol;
     struct vconn *vconn;
     size_t i;
 
-    protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
+    protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
 
     for (i = 0; i < n_fms; i++) {
         struct ofputil_flow_mod *fm = &fms[i];
@@ -1093,32 +1088,37 @@ ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
 static void
 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
 {
+    enum ofputil_protocol usable_protocols;
     struct ofputil_flow_mod *fms = NULL;
     size_t n_fms = 0;
     char *error;
 
-    error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
+    error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
+                                    &usable_protocols);
     if (error) {
         ovs_fatal(0, "%s", error);
     }
-    ofctl_flow_mod__(argv[1], fms, n_fms);
+    ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
     free(fms);
 }
 
 static void
 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
 {
+    enum ofputil_protocol usable_protocols;
+
     if (argc > 2 && !strcmp(argv[2], "-")) {
         ofctl_flow_mod_file(argc, argv, command);
     } else {
         struct ofputil_flow_mod fm;
         char *error;
 
-        error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command);
+        error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
+                                       &usable_protocols);
         if (error) {
             ovs_fatal(0, "%s", error);
         }
-        ofctl_flow_mod__(argv[1], &fm, 1);
+        ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
     }
 }
 
@@ -1440,6 +1440,7 @@ ofctl_monitor(int argc, char *argv[])
 {
     struct vconn *vconn;
     int i;
+    enum ofputil_protocol usable_protocols;
 
     open_vconn(argv[1], &vconn);
     for (i = 2; i < argc; i++) {
@@ -1458,7 +1459,8 @@ ofctl_monitor(int argc, char *argv[])
             struct ofpbuf *msg;
             char *error;
 
-            error = parse_flow_monitor_request(&fmr, arg + 6);
+            error = parse_flow_monitor_request(&fmr, arg + 6,
+                                               &usable_protocols);
             if (error) {
                 ovs_fatal(0, "%s", error);
             }
@@ -1561,9 +1563,10 @@ ofctl_packet_out(int argc, char *argv[])
     struct vconn *vconn;
     char *error;
     int i;
+    enum ofputil_protocol usable_protocols; /* TODO: Use in proto selection */
 
     ofpbuf_init(&ofpacts, 64);
-    error = parse_ofpacts(argv[3], &ofpacts);
+    error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
     if (error) {
         ovs_fatal(0, "%s", error);
     }
@@ -2000,22 +2003,23 @@ read_flows_from_file(const char *filename, struct classifier *cls, int index)
         struct fte_version *version;
         struct ofputil_flow_mod fm;
         char *error;
+        enum ofputil_protocol usable;
 
-        error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s));
+        error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
         if (error) {
             ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
         }
+        usable_protocols &= usable;
 
         version = xmalloc(sizeof *version);
         version->cookie = fm.new_cookie;
         version->idle_timeout = fm.idle_timeout;
         version->hard_timeout = fm.hard_timeout;
-        version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG);
+        version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
+                                     | OFPUTIL_FF_EMERG);
         version->ofpacts = fm.ofpacts;
         version->ofpacts_len = fm.ofpacts_len;
 
-        usable_protocols &= ofputil_usable_protocols(&fm.match);
-
         fte_insert(cls, &fm.match, fm.priority, version, index);
     }
     ds_destroy(&s);
@@ -2279,14 +2283,13 @@ ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
 /* Undocumented commands for unit testing. */
 
 static void
-ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
+ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
+                    enum ofputil_protocol usable_protocols)
 {
-    enum ofputil_protocol usable_protocols;
     enum ofputil_protocol protocol = 0;
     char *usable_s;
     size_t i;
 
-    usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
     usable_s = ofputil_protocols_to_string(usable_protocols);
     printf("usable protocols: %s\n", usable_s);
     free(usable_s);
@@ -2321,14 +2324,15 @@ ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
 static void
 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
 {
+    enum ofputil_protocol usable_protocols;
     struct ofputil_flow_mod fm;
     char *error;
 
-    error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD);
+    error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
     if (error) {
         ovs_fatal(0, "%s", error);
     }
-    ofctl_parse_flows__(&fm, 1);
+    ofctl_parse_flows__(&fm, 1, usable_protocols);
 }
 
 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
@@ -2336,15 +2340,17 @@ ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
 static void
 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
 {
+    enum ofputil_protocol usable_protocols;
     struct ofputil_flow_mod *fms = NULL;
     size_t n_fms = 0;
     char *error;
 
-    error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
+    error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
+                                    &usable_protocols);
     if (error) {
         ovs_fatal(0, "%s", error);
     }
-    ofctl_parse_flows__(fms, n_fms);
+    ofctl_parse_flows__(fms, n_fms, usable_protocols);
     free(fms);
 }
 
@@ -2799,6 +2805,8 @@ ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
     enum ofperr error;
     char *error_s;
 
+    enum ofputil_protocol usable_protocols; /* Unused for now. */
+
     match_init_catchall(&match);
     match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
     match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
@@ -2807,7 +2815,7 @@ ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
     string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
     printf("%s -> ", string_s);
     fflush(stdout);
-    error_s = parse_ofp_str(&fm, -1, string_s);
+    error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
     if (error_s) {
         ovs_fatal(0, "%s", error_s);
     }
index b46f98d..73895f3 100755 (executable)
@@ -33,23 +33,8 @@ This script is meant as a helper for the Open vSwitch init script commands.
 EOF
 }
 
-PATH=/sbin:/bin:/usr/sbin:/usr/bin
-
-missing_program () {
-    save_IFS=$IFS
-    IFS=:
-    for dir in $PATH; do
-        IFS=$save_IFS
-        if test -x $dir/$1; then
-            return 1
-        fi
-    done
-    IFS=$save_IFS
-    return 0
-}
-
 save_interfaces () {
-    if missing_program ip; then
+    if (ip -V) > /dev/null 2>&1; then :; else
         echo "$0: ip not found in $PATH" >&2
         exit 1
     fi
@@ -152,18 +137,18 @@ save_interfaces () {
         echo
     done
 
-    if missing_program iptables-save; then
-        echo "# iptables-save not found in $PATH, not saving iptables state"
-    else
+    if (iptables-save) > /dev/null 2>&1; then
         echo "# global"
         echo "iptables-restore <<'EOF'"
         iptables-save
         echo "EOF"
+    else
+        echo "# iptables-save not found in $PATH, not saving iptables state"
     fi
 }
 
 save_flows () {
-    if missing_program ovs-ofctl; then
+    if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
         echo "$0: ovs-ofctl not found in $PATH" >&2
         exit 1
     fi
@@ -182,7 +167,7 @@ ovs_vsctl () {
 
 save_ofports ()
 {
-    if missing_program ovs-vsctl; then
+    if (ovs-vsctl --version) > /dev/null 2>&1; then :; else
         echo "$0: ovs-vsctl not found in $PATH" >&2
         exit 1
     fi
index a11e646..f993ba1 100644 (file)
@@ -2557,13 +2557,11 @@ struct qos_unixctl_show_cbdata {
 };
 
 static void
-qos_unixctl_show_cb(unsigned int queue_id,
-                    const struct smap *details,
-                    void *aux)
+qos_unixctl_show_queue(unsigned int queue_id,
+                       const struct smap *details,
+                       struct iface *iface,
+                       struct ds *ds)
 {
-    struct qos_unixctl_show_cbdata *data = aux;
-    struct ds *ds = data->ds;
-    struct iface *iface = data->iface;
     struct netdev_queue_stats stats;
     struct smap_node *node;
     int error;
@@ -2607,8 +2605,6 @@ qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
     struct iface *iface;
     const char *type;
     struct smap_node *node;
-    struct qos_unixctl_show_cbdata data;
-    int error;
 
     iface = iface_find(argv[1]);
     if (!iface) {
@@ -2619,20 +2615,22 @@ qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
     netdev_get_qos(iface->netdev, &type, &smap);
 
     if (*type != '\0') {
+        struct netdev_queue_dump dump;
+        struct smap details;
+        unsigned int queue_id;
+
         ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
 
         SMAP_FOR_EACH (node, &smap) {
             ds_put_format(&ds, "%s: %s\n", node->key, node->value);
         }
 
-        data.ds = &ds;
-        data.iface = iface;
-        error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
-
-        if (error) {
-            ds_put_format(&ds, "failed to dump queues: %s",
-                          ovs_strerror(error));
+        smap_init(&details);
+        NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
+            qos_unixctl_show_queue(queue_id, &details, iface, &ds);
         }
+        smap_destroy(&details);
+
         unixctl_command_reply(conn, ds_cstr(&ds));
     } else {
         ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
@@ -3605,11 +3603,6 @@ iface_clear_db_record(const struct ovsrec_interface *if_cfg)
     }
 }
 
-struct iface_delete_queues_cbdata {
-    struct netdev *netdev;
-    const struct ovsdb_datum *queues;
-};
-
 static bool
 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
 {
@@ -3619,17 +3612,6 @@ queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
     return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
 }
 
-static void
-iface_delete_queues(unsigned int queue_id,
-                    const struct smap *details OVS_UNUSED, void *cbdata_)
-{
-    struct iface_delete_queues_cbdata *cbdata = cbdata_;
-
-    if (!queue_ids_include(cbdata->queues, queue_id)) {
-        netdev_delete_queue(cbdata->netdev, queue_id);
-    }
-}
-
 static void
 iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
 {
@@ -3640,7 +3622,10 @@ iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
     if (!qos || qos->type[0] == '\0' || qos->n_queues < 1) {
         netdev_set_qos(iface->netdev, NULL, NULL);
     } else {
-        struct iface_delete_queues_cbdata cbdata;
+        const struct ovsdb_datum *queues;
+        struct netdev_queue_dump dump;
+        unsigned int queue_id;
+        struct smap details;
         bool queue_zero;
         size_t i;
 
@@ -3648,10 +3633,15 @@ iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
         netdev_set_qos(iface->netdev, qos->type, &qos->other_config);
 
         /* Deconfigure queues that were deleted. */
-        cbdata.netdev = iface->netdev;
-        cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
-                                              OVSDB_TYPE_UUID);
-        netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
+        queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
+                                       OVSDB_TYPE_UUID);
+        smap_init(&details);
+        NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
+            if (!queue_ids_include(queues, queue_id)) {
+                netdev_delete_queue(iface->netdev, queue_id);
+            }
+        }
+        smap_destroy(&details);
 
         /* Configure queues for 'iface'. */
         queue_zero = false;