[ovs-dev] [cfm 3/6] packets: Generalize compose_lacp_packet()

Ethan Jackson ethan at nicira.com
Wed Mar 23 01:32:55 UTC 2011


Creates a new function compose_packet() which replaces
compose_lacp_packet(). The CFM code uses this function as well.
---
 lib/cfm.c         |   28 +++++++++++-----------------
 lib/packets.c     |   20 ++++++++++----------
 lib/packets.h     |    8 +++++---
 vswitchd/bridge.c |    5 +++--
 4 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/lib/cfm.c b/lib/cfm.c
index 6fb82c1..d9c8fb5 100644
--- a/lib/cfm.c
+++ b/lib/cfm.c
@@ -111,28 +111,22 @@ lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
 static struct ofpbuf *
 compose_ccm(struct cfm_internal *cfmi)
 {
-    struct ccm *ccm;
+    struct ccm ccm;
     struct ofpbuf *packet;
-    struct eth_header *eth;
-
-    packet = ofpbuf_new(ETH_HEADER_LEN + CCM_LEN + 2);
 
-    ofpbuf_reserve(packet, 2);
+    packet = ofpbuf_new(0);
 
-    eth = ofpbuf_put_zeros(packet, ETH_HEADER_LEN);
-    ccm = ofpbuf_put_zeros(packet, CCM_LEN);
+    ccm.mdlevel_version = 0;
+    ccm.opcode          = 1;
+    ccm.tlv_offset      = 70;
+    ccm.seq             = htonl(++cfmi->seq);
+    ccm.mpid            = htons(cfmi->cfm.mpid);
+    ccm.flags           = cfmi->ccm_interval;
+    memcpy(ccm.maid, cfmi->cfm.maid, sizeof ccm.maid);
 
-    memcpy(eth->eth_dst, eth_addr_ccm, ETH_ADDR_LEN);
-    memcpy(eth->eth_src, cfmi->cfm.eth_src, sizeof eth->eth_src);
-    eth->eth_type = htons(ETH_TYPE_CFM);
+    compose_packet(packet, eth_addr_ccm, cfmi->cfm.eth_src, ETH_TYPE_CFM,
+                   &ccm, sizeof ccm);
 
-    ccm->mdlevel_version = 0;
-    ccm->opcode          = 1;
-    ccm->tlv_offset      = 70;
-    ccm->seq             = htonl(++cfmi->seq);
-    ccm->mpid            = htons(cfmi->cfm.mpid);
-    ccm->flags           = cfmi->ccm_interval;
-    memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
     return packet;
 }
 
diff --git a/lib/packets.c b/lib/packets.c
index 60ee390..b5ec440 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -204,24 +204,24 @@ ipv6_is_cidr(const struct in6_addr *netmask)
     return true;
 }
 
-/* Populates 'b' with a LACP packet containing 'pdu' with source address
- * 'eth_src'. */
+/* Populates 'b' with a packet containing 'data' with source address
+ * 'eth_src' and destination address 'eth_dst'. */
 void
-compose_lacp_packet(struct ofpbuf *b, const uint8_t eth_src[ETH_ADDR_LEN],
-                    const struct lacp_pdu *pdu)
+compose_packet(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
+               const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
+               const void *data, size_t data_len)
 {
     struct eth_header *eth;
-    struct lacp_pdu *eth_pdu;
 
     ofpbuf_clear(b);
 
-    ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + LACP_PDU_LEN);
-    eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
-    eth_pdu = ofpbuf_put(b, pdu, LACP_PDU_LEN);
+    ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + data_len);
+    eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
+    ofpbuf_put(b, data, data_len);
 
-    memcpy(eth->eth_dst, eth_addr_lacp, ETH_ADDR_LEN);
+    memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
-    eth->eth_type = htons(ETH_TYPE_LACP);
+    eth->eth_type = htons(eth_type);
 }
 
 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
diff --git a/lib/packets.h b/lib/packets.h
index 2bd486e..511ad02 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -415,6 +415,11 @@ struct in6_addr ipv6_create_mask(int mask);
 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
 bool ipv6_is_cidr(const struct in6_addr *netmask);
 
+void
+compose_packet(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
+               const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
+               const void *data, size_t data_len);
+
 /* Masks for lacp_info state member. */
 #define LACP_STATE_ACT  0x01 /* Activity. Active or passive? */
 #define LACP_STATE_TIME 0x02 /* Timeout. Short or long timeout? */
@@ -463,9 +468,6 @@ struct lacp_pdu {
 } __attribute__((packed));
 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
 
-void compose_lacp_packet(struct ofpbuf *, const uint8_t eth_src[ETH_ADDR_LEN],
-                         const struct lacp_pdu *);
-
 void compose_lacp_pdu(const struct lacp_info *actor,
                       const struct lacp_info *partner, struct lacp_pdu *);
 
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index bcaf1d1..4eab695 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -3812,8 +3812,9 @@ lacp_send_pdu_cb(void *aux, const struct lacp_pdu *pdu)
     if (!error) {
         struct ofpbuf packet;
 
-        ofpbuf_init(&packet, ETH_HEADER_LEN + LACP_PDU_LEN);
-        compose_lacp_packet(&packet, ea, pdu);
+        ofpbuf_init(&packet, 0);
+        compose_packet(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
+                       pdu, sizeof *pdu);
         ofproto_send_packet(iface->port->bridge->ofproto,
                             iface->dp_ifidx, 0, &packet);
         ofpbuf_uninit(&packet);
-- 
1.7.4.1




More information about the dev mailing list