Merge into master from pull request #247:
openflow_input: add bsn_pktin_flag extension (https://github.com/floodlight/loxigen/pull/247)
diff --git a/c_gen/build_of_g.py b/c_gen/build_of_g.py
index 41d8ab2..4d37b36 100755
--- a/c_gen/build_of_g.py
+++ b/c_gen/build_of_g.py
@@ -158,6 +158,11 @@
             # but is variable length
             bytes = -1
             len_update = 4
+        elif base_type == "of_bsn_vport_header_t":
+            # This is a special case: it has non-zero min length
+            # but is variable length
+            bytes = -1
+            len_update = 4
         elif base_type in of_g.of_base_types:
             bytes = of_g.of_base_types[base_type]["bytes"]
         else:
@@ -332,6 +337,9 @@
                     # HACK the C backend does not yet support of_oxm_t
                     if m.oftype == 'of_oxm_t':
                         m_type = 'of_oxm_header_t'
+                    # HACK the C backend does not yet support of_bsn_vport_t
+                    elif m.oftype == 'of_bsn_vport_t':
+                        m_type = 'of_bsn_vport_header_t'
                     else:
                         enum = find(lambda e: e.name == m.oftype, protocol.enums)
                         if enum and "wire_type" in enum.params:
diff --git a/c_gen/c_code_gen.py b/c_gen/c_code_gen.py
index 4f5ee7f..3fa4f3a 100644
--- a/c_gen/c_code_gen.py
+++ b/c_gen/c_code_gen.py
@@ -1585,6 +1585,17 @@
     %(m_name)s->length = cur_len;
     of_object_wire_init(%(m_name)s, OF_OXM, 0);
 """ % dict(m_type=m_type[:-2], m_name=m_name))
+    elif m_type == "of_bsn_vport_header_t":
+        out.write("""
+    /* Initialize child */
+    %(m_type)s_init(%(m_name)s, obj->version, 0, 1);
+    /* Attach to parent */
+    %(m_name)s->parent = (of_object_t *)obj;
+    %(m_name)s->wbuf = obj->wbuf;
+    %(m_name)s->obj_offset = abs_offset;
+    %(m_name)s->length = cur_len;
+    of_object_wire_init(%(m_name)s, OF_BSN_VPORT, 0);
+""" % dict(m_type=m_type[:-2], m_name=m_name))
     else:
         out.write("""
     /* Initialize child */
diff --git a/c_gen/c_test_gen.py b/c_gen/c_test_gen.py
index 3eaaf82..be1b0da 100644
--- a/c_gen/c_test_gen.py
+++ b/c_gen/c_test_gen.py
@@ -100,6 +100,7 @@
         of_meter_features_t="features",
         of_match_t="match",
         of_oxm_header_t="oxm",
+        of_bsn_vport_header_t="bsn_vport",
         # BSN extensions
         of_bsn_vport_q_in_q_t="vport",
         of_bitmap_128_t="bitmap_128",
@@ -1363,6 +1364,19 @@
         FREE(octets.data);
     }
 """ % dict(var_name=var_name_map(m_type), cls=cls, m_name=m_name))
+        elif m_type == "of_bsn_vport_t": # FIXME: tests only q_in_q
+            out.write("""\
+    %(var_name)s = %(sub_cls)s_new(%(v_name)s);
+    TEST_ASSERT(%(var_name)s != NULL);
+    value = %(sub_cls)s_q_in_q_%(v_name)s_populate(
+        &%(var_name)s->q_in_q, value);
+    TEST_ASSERT(value != 0);
+    %(cls)s_%(m_name)s_set(
+        obj, %(var_name)s);
+    %(sub_cls)s_delete(%(var_name)s);
+""" % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type,
+           var_name=var_name_map(m_type),
+           v_name=loxi_utils.version_to_name(version)))
         else:
             sub_cls = m_type[:-2] # Trim _t
             out.write("""
@@ -1429,6 +1443,22 @@
     value = of_octets_check(&%(var_name)s, value);
 """ % dict(cls=cls, var_name=var_name_map(m_type), m_name=m_name,
            v_name=loxi_utils.version_to_name(version)))
+        elif m_type == "of_bsn_vport_t": # FIXME: tests only q_in_q
+            sub_cls = m_type[:-2] # Trim _t
+            out.write("""
+    { /* Use get/delete to access on check */
+        %(m_type)s *%(m_name)s_ptr;
+
+        %(m_name)s_ptr = %(cls)s_%(m_name)s_get(obj);
+        TEST_ASSERT(%(m_name)s_ptr != NULL);
+        value = %(sub_cls)s_q_in_q_%(v_name)s_check(
+            &%(m_name)s_ptr->q_in_q, value);
+        TEST_ASSERT(value != 0);
+        %(sub_cls)s_delete(%(m_name)s_ptr);
+    }
+""" % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type,
+           var_name=var_name_map(m_type),
+           v_name=loxi_utils.version_to_name(version)))
         else:
             sub_cls = m_type[:-2] # Trim _t
             out.write("""
diff --git a/c_gen/loxi_utils_legacy.py b/c_gen/loxi_utils_legacy.py
index 38feb44..220d043 100644
--- a/c_gen/loxi_utils_legacy.py
+++ b/c_gen/loxi_utils_legacy.py
@@ -120,6 +120,8 @@
         return True
     if cls.find("of_bsn_tlv") == 0:
         return True
+    if cls.find("of_bsn_vport") == 0:
+        return True
     return False
 
 def class_is_u16_len(cls):
diff --git a/c_gen/templates/loci_show.h b/c_gen/templates/loci_show.h
index b210570..88fd029 100644
--- a/c_gen/templates/loci_show.h
+++ b/c_gen/templates/loci_show.h
@@ -398,5 +398,14 @@
 #define LOCI_SHOW_u64_counter_id(writer, cookie, val) LOCI_SHOW_u64(writer, cookie, val)
 #define LOCI_SHOW_desc_str_description(writer, cookie, val) LOCI_SHOW_desc_str(writer, cookie, val)
 #define LOCI_SHOW_str64_name(writer, cookie, val) LOCI_SHOW_str64(writer, cookie, val)
+#define LOCI_SHOW_mac_local_mac(writer, cookie, val) LOCI_SHOW_mac(writer, cookie, val)
+#define LOCI_SHOW_mac_nh_mac(writer, cookie, val) LOCI_SHOW_mac(writer, cookie, val)
+#define LOCI_SHOW_ipv4_src_ip(writer, cookie, val) LOCI_SHOW_ipv4(writer, cookie, val)
+#define LOCI_SHOW_ipv4_dst_ip(writer, cookie, val) LOCI_SHOW_ipv4(writer, cookie, val)
+#define LOCI_SHOW_u8_dscp_mode(writer, cookie, val) LOCI_SHOW_u8(writer, cookie, val)
+#define LOCI_SHOW_u8_dscp(writer, cookie, val) LOCI_SHOW_u8(writer, cookie, val)
+#define LOCI_SHOW_u8_ttl(writer, cookie, val) LOCI_SHOW_u8(writer, cookie, val)
+#define LOCI_SHOW_u32_vpn(writer, cookie, val) LOCI_SHOW_u32(writer, cookie, val)
+#define LOCI_SHOW_u32_flags(writer, cookie, val) LOCI_SHOW_u32(writer, cookie, val)
 
 #endif /* _LOCI_SHOW_H_ */
diff --git a/c_gen/type_maps.py b/c_gen/type_maps.py
index af43a15..7b745a6 100644
--- a/c_gen/type_maps.py
+++ b/c_gen/type_maps.py
@@ -89,22 +89,10 @@
     }
 
 bsn_vport_types = {
-    # version 1.0
-    of_g.VERSION_1_0:dict(
-        q_in_q      = 0,
-        ),
-    # version 1.1
-    of_g.VERSION_1_1:dict(
-        q_in_q      = 0,
-        ),
-    # version 1.2
-    of_g.VERSION_1_2:dict(
-        q_in_q      = 0,
-        ),
-    # version 1.3
-    of_g.VERSION_1_3:dict(
-        q_in_q      = 0,
-        )
+    of_g.VERSION_1_0:dict(),
+    of_g.VERSION_1_1:dict(),
+    of_g.VERSION_1_2:dict(),
+    of_g.VERSION_1_3:dict(),
     }
 
 oxm_types = {
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index f345e08..452ac58 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -420,8 +420,8 @@
 meter_features = JType("OFMeterFeatures")\
         .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)",
             write="$name.writeTo(bb)")
-bsn_vport_q_in_q = JType("OFBsnVportQInQ")\
-        .op(read="OFBsnVportQInQVer$version.READER.readFrom(bb)",
+bsn_vport = JType("OFBsnVport")\
+        .op(read="OFBsnVportVer$version.READER.readFrom(bb)",
             write="$name.writeTo(bb)")
 flow_wildcards = JType("int") \
         .op(read='bb.readInt()',
@@ -537,7 +537,7 @@
         'of_meter_features_t': meter_features,
         'of_bitmap_128_t': port_bitmap,
         'of_checksum_128_t': checksum,
-        'of_bsn_vport_q_in_q_t': bsn_vport_q_in_q,
+        'of_bsn_vport_t': bsn_vport,
         }
 
 ## Map that defines exceptions from the standard loxi->java mapping scheme
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/VlanVid.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/VlanVid.java
index 2e675d4..ee605de 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/VlanVid.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/VlanVid.java
@@ -33,6 +33,8 @@
     }
 
     public static VlanVid ofVlan(int vid) {
+        if (vid == NO_MASK.vid)
+            return NO_MASK;
         if ((vid & VALIDATION_MASK) != vid)
             throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vid));
         return new VlanVid((short) vid);
diff --git a/openflow_input/bsn_vport b/openflow_input/bsn_vport
index 4eeda8c..82d9d33 100644
--- a/openflow_input/bsn_vport
+++ b/openflow_input/bsn_vport
@@ -42,6 +42,14 @@
     OF_BSN_VPORT_Q_IN_Q_UNTAGGED = 0xffff,
 };
 
+enum ofp_bsn_vport_l2gre_flags(wire_type=uint32_t, bitmask=True) {
+    OF_BSN_VPORT_L2GRE_LOCAL_MAC_IS_VALID = 0x1,
+
+    /* DSCP flags are mutually exclusive */
+    OF_BSN_VPORT_L2GRE_DSCP_ASSIGN = 0x2,
+    OF_BSN_VPORT_L2GRE_DSCP_COPY = 0x4,
+};
+
 // BSN Virtual port object header
 // FIXME For now, inheritance is not exercised.  See below.
 struct of_bsn_vport {
@@ -55,7 +63,7 @@
 
 struct of_bsn_vport_q_in_q : of_bsn_vport {
     uint16_t type == 0;
-    uint16_t length;  /* 32 */
+    uint16_t length;
     uint32_t port_no;     /* OF port number of parent; usually phys port */
     uint16_t ingress_tpid;
     uint16_t ingress_vlan_id;
@@ -64,6 +72,26 @@
     of_port_name_t if_name;  /* Name to use in create operation */
 };
 
+
+// L2GRE tunnel virtual port specification
+
+struct of_bsn_vport_l2gre : of_bsn_vport {
+    uint16_t type == 1;
+    uint16_t length;
+    enum ofp_bsn_vport_l2gre_flags flags;
+    of_port_no_t port_no;       /* OF port number of parent */
+    of_mac_addr_t local_mac;    /* Local MAC */
+    of_mac_addr_t nh_mac;       /* Next Hop MAC */
+    of_ipv4_t src_ip;           /* Source IP */
+    of_ipv4_t dst_ip;           /* Destination IP */
+    uint8_t dscp;
+    uint8_t ttl;
+    pad(2);
+    uint32_t vpn;               /* VPN ID (for GRE Key) */
+    of_port_name_t if_name;     /* Virtual Interface Name */
+};
+
+
 // Request from controller to switch to create vport
 struct of_bsn_virtual_port_create_request : of_bsn_header {
     uint8_t version;
@@ -72,8 +100,7 @@
     uint32_t xid;
     uint32_t experimenter == 0x5c16c7;
     uint32_t subtype == 15;
-    // FIXME This should be an instance of the inheritance superclass
-    of_bsn_vport_q_in_q_t vport;   // Description of vport to create
+    of_bsn_vport_t vport;   // Description of vport to create
     // Additional data follows depending on header type
 };
 
diff --git a/py_gen/oftype.py b/py_gen/oftype.py
index ddde14a..7bd242e 100644
--- a/py_gen/oftype.py
+++ b/py_gen/oftype.py
@@ -142,7 +142,7 @@
     'of_match_t': 'common.match',
     'of_port_desc_t': 'common.port_desc',
     'of_meter_features_t': 'common.meter_features',
-    'of_bsn_vport_q_in_q_t': 'common.bsn_vport_q_in_q',
+    'of_bsn_vport_t': 'common.bsn_vport',
 }
 
 for (cls, pyclass) in embedded_structs.items():
diff --git a/test_data/of13/bsn_virtual_port_create_request__l2gre.data b/test_data/of13/bsn_virtual_port_create_request__l2gre.data
new file mode 100644
index 0000000..b5fd5fa
--- /dev/null
+++ b/test_data/of13/bsn_virtual_port_create_request__l2gre.data
@@ -0,0 +1,77 @@
+-- binary
+04 04               # version, type
+00 48               # len
+01 02 03 04         # xid
+00 5c 16 c7         # experimenter
+00 00 00 0f         # subtype
+00 01               # vport type
+00 38               # vport len
+00 00 00 03         # vport flags
+00 00 00 01         # vport port no
+0a 0b 0c 0d 0e 0f   # local mac
+01 02 03 04 05 06   # next hop mac
+c0 00 00 02         # src ip
+c0 00 10 02         # dst ip
+01 40 00 00         # dscp, ttl, pad(2)
+00 00 be ef         # vpn
+66 6f 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 # vport if name
+-- python
+ofp.message.bsn_virtual_port_create_request(
+xid=0x01020304, vport=ofp.bsn_vport_l2gre(
+flags=ofp.OF_BSN_VPORT_L2GRE_LOCAL_MAC_IS_VALID | ofp.OF_BSN_VPORT_L2GRE_DSCP_ASSIGN,
+port_no=1,
+local_mac=[0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
+nh_mac=[0x01, 0x02, 0x03, 0x04, 0x05, 0x06],
+src_ip=0xc0000002,
+dst_ip=0xc0001002,
+dscp=1,
+ttl=64,
+vpn=0xbeef,
+if_name="foo"
+))
+-- c
+obj = of_bsn_virtual_port_create_request_new(OF_VERSION_1_3);
+of_bsn_virtual_port_create_request_xid_set(obj, 0x01020304);
+{
+    of_object_t *vport = of_bsn_vport_l2gre_new(OF_VERSION_1_3);
+    {
+        of_port_name_t if_name = "foo";
+        of_mac_addr_t local_mac = { { 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f } };
+        of_mac_addr_t nh_mac = { { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 } };
+        of_bsn_vport_l2gre_flags_set(vport,
+            OF_BSN_VPORT_L2GRE_LOCAL_MAC_IS_VALID |
+            OF_BSN_VPORT_L2GRE_DSCP_ASSIGN);
+        of_bsn_vport_l2gre_port_no_set(vport, 1);
+        of_bsn_vport_l2gre_local_mac_set(vport, local_mac);
+        of_bsn_vport_l2gre_nh_mac_set(vport, nh_mac);
+        of_bsn_vport_l2gre_src_ip_set(vport, 0xc0000002);
+        of_bsn_vport_l2gre_dst_ip_set(vport, 0xc0001002);
+        of_bsn_vport_l2gre_dscp_set(vport, 1);
+        of_bsn_vport_l2gre_ttl_set(vport, 64);
+        of_bsn_vport_l2gre_vpn_set(vport, 0xbeef);
+        of_bsn_vport_l2gre_if_name_set(vport, if_name);
+    }
+    of_bsn_virtual_port_create_request_vport_set(obj, vport);
+    of_object_delete(vport);
+}
+-- java
+builder.setXid(0x01020304)
+    .setVport(
+        factory.buildBsnVportL2Gre()
+            .setFlags(
+                ImmutableSet.<OFBsnVportL2GreFlags>of(
+                    OFBsnVportL2GreFlags.BSN_VPORT_L2GRE_LOCAL_MAC_IS_VALID,
+                    OFBsnVportL2GreFlags.BSN_VPORT_L2GRE_DSCP_ASSIGN
+                )
+            )
+            .setPortNo(OFPort.of(1))
+            .setLocalMac(MacAddress.of("0a:0b:0c:0d:0e:0f"))
+            .setNhMac(MacAddress.of("01:02:03:04:05:06"))
+            .setSrcIp(IPv4Address.of("192.0.0.2"))
+            .setDstIp(IPv4Address.of("192.0.16.2"))
+            .setDscp((short)1)
+            .setTtl((short)64)
+            .setVpn(0xbeef)
+            .setIfName("foo")
+            .build()
+    );
diff --git a/test_data/of13/bsn_virtual_port_create_request__q_in_q.data b/test_data/of13/bsn_virtual_port_create_request__q_in_q.data
new file mode 100644
index 0000000..ca53fbe
--- /dev/null
+++ b/test_data/of13/bsn_virtual_port_create_request__q_in_q.data
@@ -0,0 +1,53 @@
+-- binary
+04 04           # version, type
+00 30           # len
+01 02 03 04     # xid
+00 5c 16 c7     # experimenter
+00 00 00 0f     # subtype
+00 00           # vport type
+00 20           # vport len
+00 00 00 01     # vport port no
+00 02           # vport ingress tpid
+00 03           # vport ingress vlan id
+00 04           # vport egress tpid
+00 05           # vport egress vlan id
+66 6f 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 # vport if name
+-- python
+ofp.message.bsn_virtual_port_create_request(
+xid=0x01020304, vport=ofp.bsn_vport_q_in_q(
+port_no=1,
+ingress_tpid=2,
+ingress_vlan_id=3,
+egress_tpid=4,
+egress_vlan_id=5,
+if_name="foo"
+))
+-- c
+obj = of_bsn_virtual_port_create_request_new(OF_VERSION_1_3);
+of_bsn_virtual_port_create_request_xid_set(obj, 0x01020304);
+{
+    of_object_t *vport = of_bsn_vport_q_in_q_new(OF_VERSION_1_3);
+    {
+        of_port_name_t if_name = "foo";
+        of_bsn_vport_q_in_q_port_no_set(vport, 1);
+        of_bsn_vport_q_in_q_ingress_tpid_set(vport, 2);
+        of_bsn_vport_q_in_q_ingress_vlan_id_set(vport, 3);
+        of_bsn_vport_q_in_q_egress_tpid_set(vport, 4);
+        of_bsn_vport_q_in_q_egress_vlan_id_set(vport, 5);
+        of_bsn_vport_q_in_q_if_name_set(vport, if_name);
+    }
+    of_bsn_virtual_port_create_request_vport_set(obj, vport);
+    of_object_delete(vport);
+}
+-- java
+builder.setXid(0x01020304)
+    .setVport(
+        factory.buildBsnVportQInQ()
+            .setPortNo(1)
+            .setIngressTpid(2)
+            .setIngressVlanId(3)
+            .setEgressTpid(4)
+            .setEgressVlanId(5)
+            .setIfName("foo")
+            .build()
+    );