net/mlx5e: Enable traps according to link state
authorAya Levin <ayal@nvidia.com>
Tue, 26 Jan 2021 23:24:19 +0000 (15:24 -0800)
committerJakub Kicinski <kuba@kernel.org>
Thu, 28 Jan 2021 03:53:54 +0000 (19:53 -0800)
Avoid trapping packets when the interface is down, and revive them when
interface is back up. Add API to mlx5 core retrieving the action by trap
id. Use it to apply traps when interface is up, and disable then when
interface is down.

Signed-off-by: Aya Levin <ayal@nvidia.com>
Reviewed-by: Moshe Shemesh <moshe@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/mellanox/mlx5/core/devlink.c
drivers/net/ethernet/mellanox/mlx5/core/devlink.h
drivers/net/ethernet/mellanox/mlx5/core/en/trap.c
drivers/net/ethernet/mellanox/mlx5/core/en/trap.h
drivers/net/ethernet/mellanox/mlx5/core/en_main.c

index c47291467cb0cb69175353c8a19d59ff60ffe0a5..b23b5481435696b67da50d6692b07ef0f3bde57c 100644 (file)
@@ -308,6 +308,22 @@ int mlx5_devlink_trap_get_num_active(struct mlx5_core_dev *dev)
        return count;
 }
 
+int mlx5_devlink_traps_get_action(struct mlx5_core_dev *dev, int trap_id,
+                                 enum devlink_trap_action *action)
+{
+       struct mlx5_devlink_trap *dl_trap;
+
+       dl_trap = mlx5_find_trap_by_id(dev, trap_id);
+       if (!dl_trap) {
+               mlx5_core_err(dev, "Devlink trap: Get action on invalid trap id 0x%x",
+                             trap_id);
+               return -EINVAL;
+       }
+
+       *action = dl_trap->trap.action;
+       return 0;
+}
+
 struct devlink *mlx5_devlink_alloc(void)
 {
        return devlink_alloc(&mlx5_devlink_ops, sizeof(struct mlx5_core_dev));
index a9829006fa78956370d531d1b1984f1496aebbbb..eff107dad922d7c23da54341684488ec05560f79 100644 (file)
@@ -27,6 +27,8 @@ struct mlx5_core_dev;
 void mlx5_devlink_trap_report(struct mlx5_core_dev *dev, int trap_id, struct sk_buff *skb,
                              struct devlink_port *dl_port);
 int mlx5_devlink_trap_get_num_active(struct mlx5_core_dev *dev);
+int mlx5_devlink_traps_get_action(struct mlx5_core_dev *dev, int trap_id,
+                                 enum devlink_trap_action *action);
 
 struct devlink *mlx5_devlink_alloc(void);
 void mlx5_devlink_free(struct devlink *devlink);
index d078281dbd1de4c29194242e288fa52a340c0441..37fc1d77ded7cd1f1968ccc6043149d86d801f3f 100644 (file)
@@ -401,6 +401,14 @@ int mlx5e_handle_trap_event(struct mlx5e_priv *priv, struct mlx5_trap_ctx *trap_
 {
        int err = 0;
 
+       /* Traps are unarmed when interface is down, no need to update
+        * them. The configuration is saved in the core driver,
+        * queried and applied upon interface up operation in
+        * mlx5e_open_locked().
+        */
+       if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
+               return 0;
+
        switch (trap_ctx->action) {
        case DEVLINK_TRAP_ACTION_TRAP:
                err = mlx5e_handle_action_trap(priv, trap_ctx->id);
@@ -415,3 +423,35 @@ int mlx5e_handle_trap_event(struct mlx5e_priv *priv, struct mlx5_trap_ctx *trap_
        }
        return err;
 }
+
+static int mlx5e_apply_trap(struct mlx5e_priv *priv, int trap_id, bool enable)
+{
+       enum devlink_trap_action action;
+       int err;
+
+       err = mlx5_devlink_traps_get_action(priv->mdev, trap_id, &action);
+       if (err)
+               return err;
+       if (action == DEVLINK_TRAP_ACTION_TRAP)
+               err = enable ? mlx5e_handle_action_trap(priv, trap_id) :
+                              mlx5e_handle_action_drop(priv, trap_id);
+       return err;
+}
+
+static const int mlx5e_traps_arr[] = {
+       DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
+       DEVLINK_TRAP_GENERIC_ID_DMAC_FILTER,
+};
+
+int mlx5e_apply_traps(struct mlx5e_priv *priv, bool enable)
+{
+       int err;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(mlx5e_traps_arr); i++) {
+               err = mlx5e_apply_trap(priv, mlx5e_traps_arr[i], enable);
+               if (err)
+                       return err;
+       }
+       return 0;
+}
index cc1fa9f12c45ba2832c320ff0bb986143e3c83c1..aa3f17658c6d4cb9f9452209cf799e35a89de86b 100644 (file)
@@ -32,4 +32,6 @@ struct mlx5e_trap {
 void mlx5e_close_trap(struct mlx5e_trap *trap);
 void mlx5e_deactivate_trap(struct mlx5e_priv *priv);
 int mlx5e_handle_trap_event(struct mlx5e_priv *priv, struct mlx5_trap_ctx *trap_ctx);
+int mlx5e_apply_traps(struct mlx5e_priv *priv, bool enable);
+
 #endif
index 3252919ec7bfd56a40fc2d73e176a19a0aee4c36..f8619d381345250d700b0ac75443941c5101a1ba 100644 (file)
@@ -3247,6 +3247,7 @@ int mlx5e_open_locked(struct net_device *netdev)
 
        priv->profile->update_rx(priv);
        mlx5e_activate_priv_channels(priv);
+       mlx5e_apply_traps(priv, true);
        if (priv->profile->update_carrier)
                priv->profile->update_carrier(priv);
 
@@ -3282,6 +3283,7 @@ int mlx5e_close_locked(struct net_device *netdev)
        if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
                return 0;
 
+       mlx5e_apply_traps(priv, false);
        clear_bit(MLX5E_STATE_OPENED, &priv->state);
 
        netif_carrier_off(priv->netdev);