netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / lacp.c
index 9eec1f9..c5d0fb1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011, 2012, 2013 Nicira, Inc.
+/* Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,7 +22,8 @@
 #include "dynamic-string.h"
 #include "hash.h"
 #include "hmap.h"
-#include "ofpbuf.h"
+#include "dp-packet.h"
+#include "ovs-atomic.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "seq.h"
@@ -30,7 +31,7 @@
 #include "timer.h"
 #include "timeval.h"
 #include "unixctl.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(lacp);
 
@@ -52,7 +53,7 @@ VLOG_DEFINE_THIS_MODULE(lacp);
 OVS_PACKED(
 struct lacp_info {
     ovs_be16 sys_priority;            /* System priority. */
-    uint8_t sys_id[ETH_ADDR_LEN];     /* System ID. */
+    struct eth_addr sys_id;           /* System ID. */
     ovs_be16 key;                     /* Operational key. */
     ovs_be16 port_priority;           /* Port priority. */
     ovs_be16 port_id;                 /* Port ID. */
@@ -61,7 +62,6 @@ struct lacp_info {
 BUILD_ASSERT_DECL(LACP_INFO_LEN == sizeof(struct lacp_info));
 
 #define LACP_PDU_LEN 110
-OVS_PACKED(
 struct lacp_pdu {
     uint8_t subtype;          /* Always 1. */
     uint8_t version;          /* Always 1. */
@@ -80,7 +80,7 @@ struct lacp_pdu {
     uint8_t collector_len;    /* Always 16. */
     ovs_be16 collector_delay; /* Maximum collector delay. Set to UINT16_MAX. */
     uint8_t z3[64];           /* Combination of several fields.  Always 0. */
-});
+};
 BUILD_ASSERT_DECL(LACP_PDU_LEN == sizeof(struct lacp_pdu));
 \f
 /* Implementation. */
@@ -92,9 +92,9 @@ enum slave_status {
 };
 
 struct lacp {
-    struct list node;             /* Node in all_lacps list. */
+    struct ovs_list node;         /* Node in all_lacps list. */
     char *name;                   /* Name of this lacp object. */
-    uint8_t sys_id[ETH_ADDR_LEN]; /* System ID. */
+    struct eth_addr sys_id;       /* System ID. */
     uint16_t sys_priority;        /* System Priority. */
     bool active;                  /* Active or Passive. */
 
@@ -106,7 +106,7 @@ struct lacp {
     bool update;             /* True if lacp_update() needs to be called. */
     bool fallback_ab; /* True if fallback to active-backup on LACP failure. */
 
-    atomic_int ref_cnt;
+    struct ovs_refcount ref_cnt;
 };
 
 struct slave {
@@ -125,11 +125,15 @@ struct slave {
     struct lacp_info ntt_actor;   /* Used to decide if we Need To Transmit. */
     struct timer tx;              /* Next message transmission timer. */
     struct timer rx;              /* Expected message receive timer. */
+
+    uint32_t count_rx_pdus;       /* dot3adAggPortStatsLACPDUsRx */
+    uint32_t count_rx_pdus_bad;   /* dot3adAggPortStatsIllegalRx */
+    uint32_t count_tx_pdus;       /* dot3adAggPortStatsLACPDUsTx */
 };
 
 static struct ovs_mutex mutex;
-static struct list all_lacps__ = LIST_INITIALIZER(&all_lacps__);
-static struct list *const all_lacps OVS_GUARDED_BY(mutex) = &all_lacps__;
+static struct ovs_list all_lacps__ = OVS_LIST_INITIALIZER(&all_lacps__);
+static struct ovs_list *const all_lacps OVS_GUARDED_BY(mutex) = &all_lacps__;
 
 static void lacp_update_attached(struct lacp *) OVS_REQUIRES(mutex);
 
@@ -177,11 +181,12 @@ compose_lacp_pdu(const struct lacp_info *actor,
  * supported by OVS.  Otherwise, it returns a pointer to the lacp_pdu contained
  * within 'b'. */
 static const struct lacp_pdu *
-parse_lacp_packet(const struct ofpbuf *b)
+parse_lacp_packet(const struct dp_packet *p)
 {
     const struct lacp_pdu *pdu;
 
-    pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
+    pdu = dp_packet_at(p, (uint8_t *)dp_packet_l3(p) - (uint8_t *)dp_packet_data(p),
+                    LACP_PDU_LEN);
 
     if (pdu && pdu->subtype == 1
         && pdu->actor_type == 1 && pdu->actor_len == 20
@@ -202,25 +207,37 @@ lacp_init(void)
                              lacp_unixctl_show, NULL);
 }
 
-/* Creates a LACP object. */
-struct lacp *
-lacp_create(void) OVS_EXCLUDED(mutex)
+static void
+lacp_lock(void) OVS_ACQUIRES(mutex)
 {
     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
-    struct lacp *lacp;
 
     if (ovsthread_once_start(&once)) {
         ovs_mutex_init_recursive(&mutex);
         ovsthread_once_done(&once);
     }
+    ovs_mutex_lock(&mutex);
+}
+
+static void
+lacp_unlock(void) OVS_RELEASES(mutex)
+{
+    ovs_mutex_unlock(&mutex);
+}
+
+/* Creates a LACP object. */
+struct lacp *
+lacp_create(void) OVS_EXCLUDED(mutex)
+{
+    struct lacp *lacp;
 
     lacp = xzalloc(sizeof *lacp);
     hmap_init(&lacp->slaves);
-    atomic_init(&lacp->ref_cnt, 1);
+    ovs_refcount_init(&lacp->ref_cnt);
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     list_push_back(all_lacps, &lacp->node);
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
     return lacp;
 }
 
@@ -229,9 +246,7 @@ lacp_ref(const struct lacp *lacp_)
 {
     struct lacp *lacp = CONST_CAST(struct lacp *, lacp_);
     if (lacp) {
-        int orig;
-        atomic_add(&lacp->ref_cnt, 1, &orig);
-        ovs_assert(orig > 0);
+        ovs_refcount_ref(&lacp->ref_cnt);
     }
     return lacp;
 }
@@ -240,18 +255,10 @@ lacp_ref(const struct lacp *lacp_)
 void
 lacp_unref(struct lacp *lacp) OVS_EXCLUDED(mutex)
 {
-    int orig;
-
-    if (!lacp) {
-        return;
-    }
-
-    atomic_sub(&lacp->ref_cnt, 1, &orig);
-    ovs_assert(orig > 0);
-    if (orig == 1) {
+    if (lacp && ovs_refcount_unref_relaxed(&lacp->ref_cnt) == 1) {
         struct slave *slave, *next;
 
-        ovs_mutex_lock(&mutex);
+        lacp_lock();
         HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
             slave_destroy(slave);
         }
@@ -260,7 +267,7 @@ lacp_unref(struct lacp *lacp) OVS_EXCLUDED(mutex)
         list_remove(&lacp->node);
         free(lacp->name);
         free(lacp);
-        ovs_mutex_unlock(&mutex);
+        lacp_unlock();
     }
 }
 
@@ -271,7 +278,7 @@ lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
 {
     ovs_assert(!eth_addr_is_zero(s->id));
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     if (!lacp->name || strcmp(s->name, lacp->name)) {
         free(lacp->name);
         lacp->name = xstrdup(s->name);
@@ -279,7 +286,7 @@ lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
 
     if (!eth_addr_equals(lacp->sys_id, s->id)
         || lacp->sys_priority != s->priority) {
-        memcpy(lacp->sys_id, s->id, ETH_ADDR_LEN);
+        lacp->sys_id = s->id;
         lacp->sys_priority = s->priority;
         lacp->update = true;
     }
@@ -292,7 +299,7 @@ lacp_configure(struct lacp *lacp, const struct lacp_settings *s)
         lacp->update = true;
     }
 
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 /* Returns true if 'lacp' is configured in active mode, false if 'lacp' is
@@ -301,9 +308,9 @@ bool
 lacp_is_active(const struct lacp *lacp) OVS_EXCLUDED(mutex)
 {
     bool ret;
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     ret = lacp->active;
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
     return ret;
 }
 
@@ -312,7 +319,7 @@ lacp_is_active(const struct lacp *lacp) OVS_EXCLUDED(mutex)
  */
 void
 lacp_process_packet(struct lacp *lacp, const void *slave_,
-                    const struct ofpbuf *packet)
+                    const struct dp_packet *packet)
     OVS_EXCLUDED(mutex)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -320,14 +327,16 @@ lacp_process_packet(struct lacp *lacp, const void *slave_,
     long long int tx_rate;
     struct slave *slave;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     slave = slave_lookup(lacp, slave_);
     if (!slave) {
         goto out;
     }
+    slave->count_rx_pdus++;
 
     pdu = parse_lacp_packet(packet);
     if (!pdu) {
+       slave->count_rx_pdus_bad++;
         VLOG_WARN_RL(&rl, "%s: received an unparsable LACP PDU.", lacp->name);
         goto out;
     }
@@ -347,25 +356,25 @@ lacp_process_packet(struct lacp *lacp, const void *slave_,
     }
 
 out:
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 /* Returns the lacp_status of the given 'lacp' object (which may be NULL). */
 enum lacp_status
 lacp_status(const struct lacp *lacp) OVS_EXCLUDED(mutex)
 {
-    enum lacp_status ret;
+    if (lacp) {
+        enum lacp_status ret;
 
-    ovs_mutex_lock(&mutex);
-    if (!lacp) {
-        ret = LACP_DISABLED;
-    } else if (lacp->negotiated) {
-        ret = LACP_NEGOTIATED;
+        lacp_lock();
+        ret = lacp->negotiated ? LACP_NEGOTIATED : LACP_CONFIGURED;
+        lacp_unlock();
+        return ret;
     } else {
-        ret = LACP_CONFIGURED;
+        /* Don't take 'mutex'.  It might not even be initialized, since we
+         * don't know that any lacp object has been created. */
+        return LACP_DISABLED;
     }
-    ovs_mutex_unlock(&mutex);
-    return ret;
 }
 
 /* Registers 'slave_' as subordinate to 'lacp'.  This should be called at least
@@ -378,7 +387,7 @@ lacp_slave_register(struct lacp *lacp, void *slave_,
 {
     struct slave *slave;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     slave = slave_lookup(lacp, slave_);
     if (!slave) {
         slave = xzalloc(sizeof *slave);
@@ -410,7 +419,7 @@ lacp_slave_register(struct lacp *lacp, void *slave_,
             slave_set_expired(slave);
         }
     }
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 /* Unregisters 'slave_' with 'lacp'.  */
@@ -420,13 +429,13 @@ lacp_slave_unregister(struct lacp *lacp, const void *slave_)
 {
     struct slave *slave;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     slave = slave_lookup(lacp, slave_);
     if (slave) {
         slave_destroy(slave);
         lacp->update = true;
     }
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 /* This function should be called whenever the carrier status of 'slave_' has
@@ -440,7 +449,7 @@ lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
         return;
     }
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     slave = slave_lookup(lacp, slave_);
     if (!slave) {
         goto out;
@@ -451,7 +460,7 @@ lacp_slave_carrier_changed(const struct lacp *lacp, const void *slave_)
     }
 
 out:
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 static bool
@@ -475,10 +484,10 @@ lacp_slave_may_enable(const struct lacp *lacp, const void *slave_)
         struct slave *slave;
         bool ret;
 
-        ovs_mutex_lock(&mutex);
+        lacp_lock();
         slave = slave_lookup(lacp, slave_);
         ret = slave ? slave_may_enable__(slave) : false;
-        ovs_mutex_unlock(&mutex);
+        lacp_unlock();
         return ret;
     } else {
         return true;
@@ -495,10 +504,10 @@ lacp_slave_is_current(const struct lacp *lacp, const void *slave_)
     struct slave *slave;
     bool ret;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     slave = slave_lookup(lacp, slave_);
     ret = slave ? slave->status != LACP_DEFAULTED : false;
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
     return ret;
 }
 
@@ -508,7 +517,7 @@ lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu) OVS_EXCLUDED(mutex)
 {
     struct slave *slave;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
         if (timer_expired(&slave->rx)) {
             enum slave_status old_status = slave->status;
@@ -545,6 +554,7 @@ lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu) OVS_EXCLUDED(mutex)
             slave->ntt_actor = actor;
             compose_lacp_pdu(&actor, &slave->partner, &pdu);
             send_pdu(slave->aux, &pdu, sizeof pdu);
+           slave->count_tx_pdus++;
 
             duration = (slave->partner.state & LACP_STATE_TIME
                         ? LACP_FAST_TIME_TX
@@ -554,7 +564,7 @@ lacp_run(struct lacp *lacp, lacp_send_pdu *send_pdu) OVS_EXCLUDED(mutex)
             seq_change(connectivity_seq_get());
         }
     }
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 
 /* Causes poll_block() to wake up when lacp_run() needs to be called again. */
@@ -563,7 +573,7 @@ lacp_wait(struct lacp *lacp) OVS_EXCLUDED(mutex)
 {
     struct slave *slave;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     HMAP_FOR_EACH (slave, node, &lacp->slaves) {
         if (slave_may_tx(slave)) {
             timer_wait(&slave->tx);
@@ -573,7 +583,7 @@ lacp_wait(struct lacp *lacp) OVS_EXCLUDED(mutex)
             timer_wait(&slave->rx);
         }
     }
-    ovs_mutex_unlock(&mutex);
+    lacp_unlock();
 }
 \f
 /* Static Helpers. */
@@ -722,7 +732,7 @@ slave_get_actor(struct slave *slave, struct lacp_info *actor)
     actor->port_priority = htons(slave->port_priority);
     actor->port_id = htons(slave->port_id);
     actor->sys_priority = htons(lacp->sys_priority);
-    memcpy(&actor->sys_id, lacp->sys_id, ETH_ADDR_LEN);
+    actor->sys_id = lacp->sys_id;
 }
 
 /* Given 'slave', populates 'priority' with data representing its LACP link
@@ -955,7 +965,7 @@ lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
     struct ds ds = DS_EMPTY_INITIALIZER;
     struct lacp *lacp;
 
-    ovs_mutex_lock(&mutex);
+    lacp_lock();
     if (argc > 1) {
         lacp = lacp_find(argv[1]);
         if (!lacp) {
@@ -973,5 +983,56 @@ lacp_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
     ds_destroy(&ds);
 
 out:
+    lacp_unlock();
+}
+
+/* Extract a snapshot of the current state and counters for a slave port.
+   Return false if the slave is not active. */
+bool
+lacp_get_slave_stats(const struct lacp *lacp, const void *slave_, struct lacp_slave_stats *stats)
+    OVS_EXCLUDED(mutex)
+{
+    struct slave *slave;
+    struct lacp_info actor;
+    bool ret;
+
+    ovs_mutex_lock(&mutex);
+
+    slave = slave_lookup(lacp, slave_);
+    if (slave) {
+       ret = true;
+       slave_get_actor(slave, &actor);
+       stats->dot3adAggPortActorSystemID = actor.sys_id;
+       stats->dot3adAggPortPartnerOperSystemID = slave->partner.sys_id;
+       stats->dot3adAggPortAttachedAggID = (lacp->key_slave->key ?
+                                            lacp->key_slave->key :
+                                            lacp->key_slave->port_id);
+
+       /* Construct my admin-state.  Assume aggregation is configured on. */
+       stats->dot3adAggPortActorAdminState = LACP_STATE_AGG;
+       if (lacp->active) {
+           stats->dot3adAggPortActorAdminState |= LACP_STATE_ACT;
+       }
+       if (lacp->fast) {
+           stats->dot3adAggPortActorAdminState |= LACP_STATE_TIME;
+       }
+       /* XXX Not sure how to know the partner admin state. It
+        * might have to be captured and remembered during the
+        * negotiation phase.
+        */
+       stats->dot3adAggPortPartnerAdminState = 0;
+
+       stats->dot3adAggPortActorOperState = actor.state;
+       stats->dot3adAggPortPartnerOperState = slave->partner.state;
+
+       /* Read out the latest counters */
+       stats->dot3adAggPortStatsLACPDUsRx = slave->count_rx_pdus;
+       stats->dot3adAggPortStatsIllegalRx = slave->count_rx_pdus_bad;
+       stats->dot3adAggPortStatsLACPDUsTx = slave->count_tx_pdus;
+    } else {
+        ret = false;
+    }
     ovs_mutex_unlock(&mutex);
+    return ret;
+
 }