mlxsw: spectrum_span: Add APIs to get / put an analyzed port
authorIdo Schimmel <idosch@mellanox.com>
Thu, 30 Apr 2020 17:01:09 +0000 (20:01 +0300)
committerDavid S. Miller <davem@davemloft.net>
Thu, 30 Apr 2020 20:02:32 +0000 (13:02 -0700)
An analyzed port is a port whose incoming / outgoing traffic is mirrored
to a SPAN agent and analyzed on a remote server.

A port can be analyzed by multiple tc filters and therefore the
corresponding analyzed port entry needs to be reference counted. This is
significant because ports whose outgoing traffic is analyzed need to
have an egress mirror buffer.

Add APIs to get / put an analyzed port. Allocate an egress mirror buffer
on a port when it is first inspected at egress and free the buffer when
it is no longer inspected at egress.

Protect the list of analyzed ports with a mutex, as a later patch will
traverse it from a context in which RTNL lock is not held.

Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.h

index c4159f4a66e2b9d7cf6e1f4e0295a72863de4b6a..5edf9d1bf9375de9802332d3b897b496eb51d906 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <linux/if_bridge.h>
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/refcount.h>
 #include <linux/rtnetlink.h>
 #include <linux/workqueue.h>
 struct mlxsw_sp_span {
        struct work_struct work;
        struct mlxsw_sp *mlxsw_sp;
+       struct list_head analyzed_ports_list;
+       struct mutex analyzed_ports_lock; /* Protects analyzed_ports_list */
        atomic_t active_entries_count;
        int entries_count;
        struct mlxsw_sp_span_entry entries[];
 };
 
+struct mlxsw_sp_span_analyzed_port {
+       struct list_head list; /* Member of analyzed_ports_list */
+       refcount_t ref_count;
+       u8 local_port;
+       bool ingress;
+};
+
 static void mlxsw_sp_span_respin_work(struct work_struct *work);
 
 static u64 mlxsw_sp_span_occ_get(void *priv)
@@ -49,6 +59,8 @@ int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp)
                return -ENOMEM;
        span->entries_count = entries_count;
        atomic_set(&span->active_entries_count, 0);
+       mutex_init(&span->analyzed_ports_lock);
+       INIT_LIST_HEAD(&span->analyzed_ports_list);
        span->mlxsw_sp = mlxsw_sp;
        mlxsw_sp->span = span;
 
@@ -79,6 +91,8 @@ void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp)
 
                WARN_ON_ONCE(!list_empty(&curr->bound_ports_list));
        }
+       WARN_ON_ONCE(!list_empty(&mlxsw_sp->span->analyzed_ports_list));
+       mutex_destroy(&mlxsw_sp->span->analyzed_ports_lock);
        kfree(mlxsw_sp->span);
 }
 
@@ -1082,3 +1096,125 @@ void mlxsw_sp_span_agent_put(struct mlxsw_sp *mlxsw_sp, int span_id)
 
        mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
 }
+
+static struct mlxsw_sp_span_analyzed_port *
+mlxsw_sp_span_analyzed_port_find(struct mlxsw_sp_span *span, u8 local_port,
+                                bool ingress)
+{
+       struct mlxsw_sp_span_analyzed_port *analyzed_port;
+
+       list_for_each_entry(analyzed_port, &span->analyzed_ports_list, list) {
+               if (analyzed_port->local_port == local_port &&
+                   analyzed_port->ingress == ingress)
+                       return analyzed_port;
+       }
+
+       return NULL;
+}
+
+static struct mlxsw_sp_span_analyzed_port *
+mlxsw_sp_span_analyzed_port_create(struct mlxsw_sp_span *span,
+                                  struct mlxsw_sp_port *mlxsw_sp_port,
+                                  bool ingress)
+{
+       struct mlxsw_sp_span_analyzed_port *analyzed_port;
+       int err;
+
+       analyzed_port = kzalloc(sizeof(*analyzed_port), GFP_KERNEL);
+       if (!analyzed_port)
+               return ERR_PTR(-ENOMEM);
+
+       refcount_set(&analyzed_port->ref_count, 1);
+       analyzed_port->local_port = mlxsw_sp_port->local_port;
+       analyzed_port->ingress = ingress;
+       list_add_tail(&analyzed_port->list, &span->analyzed_ports_list);
+
+       /* An egress mirror buffer should be allocated on the egress port which
+        * does the mirroring.
+        */
+       if (!ingress) {
+               u16 mtu = mlxsw_sp_port->dev->mtu;
+
+               err = mlxsw_sp_span_port_buffsize_update(mlxsw_sp_port, mtu);
+               if (err)
+                       goto err_buffsize_update;
+       }
+
+       return analyzed_port;
+
+err_buffsize_update:
+       list_del(&analyzed_port->list);
+       kfree(analyzed_port);
+       return ERR_PTR(err);
+}
+
+static void
+mlxsw_sp_span_analyzed_port_destroy(struct mlxsw_sp_span *span,
+                                   struct mlxsw_sp_span_analyzed_port *
+                                   analyzed_port)
+{
+       struct mlxsw_sp *mlxsw_sp = span->mlxsw_sp;
+       char sbib_pl[MLXSW_REG_SBIB_LEN];
+
+       /* Remove egress mirror buffer now that port is no longer analyzed
+        * at egress.
+        */
+       if (!analyzed_port->ingress) {
+               mlxsw_reg_sbib_pack(sbib_pl, analyzed_port->local_port, 0);
+               mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
+       }
+
+       list_del(&analyzed_port->list);
+       kfree(analyzed_port);
+}
+
+int mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port *mlxsw_sp_port,
+                                   bool ingress)
+{
+       struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
+       struct mlxsw_sp_span_analyzed_port *analyzed_port;
+       u8 local_port = mlxsw_sp_port->local_port;
+       int err = 0;
+
+       mutex_lock(&mlxsw_sp->span->analyzed_ports_lock);
+
+       analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span,
+                                                        local_port, ingress);
+       if (analyzed_port) {
+               refcount_inc(&analyzed_port->ref_count);
+               goto out_unlock;
+       }
+
+       analyzed_port = mlxsw_sp_span_analyzed_port_create(mlxsw_sp->span,
+                                                          mlxsw_sp_port,
+                                                          ingress);
+       if (IS_ERR(analyzed_port))
+               err = PTR_ERR(analyzed_port);
+
+out_unlock:
+       mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock);
+       return err;
+}
+
+void mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port *mlxsw_sp_port,
+                                    bool ingress)
+{
+       struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
+       struct mlxsw_sp_span_analyzed_port *analyzed_port;
+       u8 local_port = mlxsw_sp_port->local_port;
+
+       mutex_lock(&mlxsw_sp->span->analyzed_ports_lock);
+
+       analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span,
+                                                        local_port, ingress);
+       if (WARN_ON_ONCE(!analyzed_port))
+               goto out_unlock;
+
+       if (!refcount_dec_and_test(&analyzed_port->ref_count))
+               goto out_unlock;
+
+       mlxsw_sp_span_analyzed_port_destroy(mlxsw_sp->span, analyzed_port);
+
+out_unlock:
+       mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock);
+}
index b79de9a125bba87b97bc5c0623047bd33d7ac8f7..1345eda5cc34914bd4076dbebca41f59071e2baf 100644 (file)
@@ -80,5 +80,9 @@ void mlxsw_sp_span_speed_update_work(struct work_struct *work);
 int mlxsw_sp_span_agent_get(struct mlxsw_sp *mlxsw_sp,
                            const struct net_device *to_dev, int *p_span_id);
 void mlxsw_sp_span_agent_put(struct mlxsw_sp *mlxsw_sp, int span_id);
+int mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port *mlxsw_sp_port,
+                                   bool ingress);
+void mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port *mlxsw_sp_port,
+                                    bool ingress);
 
 #endif