Skip to content

Commit

Permalink
net/mlx5e: Add MACsec TX steering rules
Browse files Browse the repository at this point in the history
Tx flow steering consists of two flow tables (FTs).

The first FT (crypto table) has two fixed rules:
One default miss rule so non MACsec offloaded packets bypass the MACSec
tables, another rule to make sure that MACsec key exchange (MKE) traffic
passes unencrypted as expected (matched of ethertype).
On each new MACsec offload flow, a new MACsec rule is added.
This rule is matched on metadata_reg_a (which contains the id of the
flow) and invokes the MACsec offload action on match.

The second FT (check table) has two fixed rules:
One rule for verifying that the previous offload actions were
finished successfully and packet need to be transmitted.
Another default rule for dropping packets that were failed in the
offload actions.

The MACsec FTs should be created on demand when the first MACsec rule is
added and destroyed when the last MACsec rule is deleted.

Signed-off-by: Lior Nahmanson <liorna@nvidia.com>
Reviewed-by: Raed Salem <raeds@nvidia.com>
Signed-off-by: Raed Salem <raeds@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Lior Nahmanson authored and David S. Miller committed Sep 7, 2022
1 parent ee534d7 commit e467b28
Show file tree
Hide file tree
Showing 5 changed files with 770 additions and 15 deletions.
2 changes: 1 addition & 1 deletion drivers/net/ethernet/mellanox/mlx5/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mlx5_core-$(CONFIG_MLX5_CORE_IPOIB) += ipoib/ipoib.o ipoib/ethtool.o ipoib/ipoib
#
mlx5_core-$(CONFIG_MLX5_FPGA) += fpga/cmd.o fpga/core.o fpga/conn.o fpga/sdk.o

mlx5_core-$(CONFIG_MLX5_EN_MACSEC) += en_accel/macsec.o
mlx5_core-$(CONFIG_MLX5_EN_MACSEC) += en_accel/macsec.o en_accel/macsec_fs.o

mlx5_core-$(CONFIG_MLX5_EN_IPSEC) += en_accel/ipsec.o en_accel/ipsec_rxtx.o \
en_accel/ipsec_stats.o en_accel/ipsec_fs.o \
Expand Down
65 changes: 51 additions & 14 deletions drivers/net/ethernet/mellanox/mlx5/core/en_accel/macsec.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "en.h"
#include "lib/mlx5.h"
#include "en_accel/macsec.h"
#include "en_accel/macsec_fs.h"

#define MLX5_MACSEC_ASO_INC_SN 0x2
#define MLX5_MACSEC_ASO_REG_C_4_5 0x2
Expand All @@ -18,9 +19,12 @@ struct mlx5e_macsec_sa {
u32 enc_key_id;
u32 next_pn;
sci_t sci;

struct mlx5e_macsec_tx_rule *tx_rule;
};

struct mlx5e_macsec {
struct mlx5e_macsec_fs *macsec_fs;
struct mlx5e_macsec_sa *tx_sa[MACSEC_NUM_AN];
struct mutex lock; /* Protects mlx5e_macsec internal contexts */

Expand Down Expand Up @@ -90,18 +94,26 @@ static void mlx5e_macsec_destroy_object(struct mlx5_core_dev *mdev, u32 macsec_o
mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
}

static void mlx5e_macsec_cleanup_object(struct mlx5e_macsec *macsec,
struct mlx5e_macsec_sa *sa)
static void mlx5e_macsec_cleanup_sa(struct mlx5e_macsec *macsec, struct mlx5e_macsec_sa *sa)
{

if (!sa->tx_rule)
return;

mlx5e_macsec_fs_del_rule(macsec->macsec_fs, sa->tx_rule,
MLX5_ACCEL_MACSEC_ACTION_ENCRYPT);
mlx5e_macsec_destroy_object(macsec->mdev, sa->macsec_obj_id);
sa->tx_rule = NULL;
}

static int mlx5e_macsec_init_object(struct macsec_context *ctx,
struct mlx5e_macsec_sa *sa,
bool encrypt)
static int mlx5e_macsec_init_sa(struct macsec_context *ctx,
struct mlx5e_macsec_sa *sa,
bool encrypt)
{
struct mlx5e_priv *priv = netdev_priv(ctx->netdev);
struct mlx5e_macsec *macsec = priv->macsec;
struct mlx5_macsec_rule_attrs rule_attrs;
struct mlx5e_macsec_tx_rule *tx_rule;
struct mlx5_core_dev *mdev = priv->mdev;
struct mlx5_macsec_obj_attrs obj_attrs;
int err;
Expand All @@ -116,7 +128,21 @@ static int mlx5e_macsec_init_object(struct macsec_context *ctx,
if (err)
return err;

rule_attrs.macsec_obj_id = sa->macsec_obj_id;
rule_attrs.action = MLX5_ACCEL_MACSEC_ACTION_ENCRYPT;

tx_rule = mlx5e_macsec_fs_add_rule(macsec->macsec_fs, ctx, &rule_attrs);
if (IS_ERR_OR_NULL(tx_rule))
goto destroy_macsec_object;

sa->tx_rule = tx_rule;

return 0;

destroy_macsec_object:
mlx5e_macsec_destroy_object(mdev, sa->macsec_obj_id);

return err;
}

static int mlx5e_macsec_add_txsa(struct macsec_context *ctx)
Expand Down Expand Up @@ -168,7 +194,7 @@ static int mlx5e_macsec_add_txsa(struct macsec_context *ctx)
!tx_sa->active)
goto out;

err = mlx5e_macsec_init_object(ctx, tx_sa, tx_sc->encrypt);
err = mlx5e_macsec_init_sa(ctx, tx_sa, tx_sc->encrypt);
if (err)
goto destroy_encryption_key;

Expand Down Expand Up @@ -228,15 +254,17 @@ static int mlx5e_macsec_upd_txsa(struct macsec_context *ctx)
goto out;

if (ctx_tx_sa->active) {
err = mlx5e_macsec_init_object(ctx, tx_sa, tx_sc->encrypt);
err = mlx5e_macsec_init_sa(ctx, tx_sa, tx_sc->encrypt);
if (err)
goto out;
} else {
mlx5e_macsec_cleanup_object(macsec, tx_sa);
if (!tx_sa->tx_rule)
return -EINVAL;

mlx5e_macsec_cleanup_sa(macsec, tx_sa);
}

tx_sa->active = ctx_tx_sa->active;

out:
mutex_unlock(&macsec->lock);

Expand All @@ -246,7 +274,6 @@ static int mlx5e_macsec_upd_txsa(struct macsec_context *ctx)
static int mlx5e_macsec_del_txsa(struct macsec_context *ctx)
{
struct mlx5e_priv *priv = netdev_priv(ctx->netdev);
struct mlx5_core_dev *mdev = priv->mdev;
u8 assoc_num = ctx->sa.assoc_num;
struct mlx5e_macsec_sa *tx_sa;
struct mlx5e_macsec *macsec;
Expand All @@ -266,10 +293,8 @@ static int mlx5e_macsec_del_txsa(struct macsec_context *ctx)
goto out;
}

mlx5e_macsec_cleanup_object(macsec, tx_sa);

mlx5_destroy_encryption_key(mdev, tx_sa->enc_key_id);

mlx5e_macsec_cleanup_sa(macsec, tx_sa);
mlx5_destroy_encryption_key(macsec->mdev, tx_sa->enc_key_id);
kfree(tx_sa);
macsec->tx_sa[assoc_num] = NULL;

Expand Down Expand Up @@ -334,6 +359,7 @@ int mlx5e_macsec_init(struct mlx5e_priv *priv)
{
struct mlx5_core_dev *mdev = priv->mdev;
struct mlx5e_macsec *macsec = NULL;
struct mlx5e_macsec_fs *macsec_fs;
int err;

if (!mlx5e_is_macsec_device(priv->mdev)) {
Expand All @@ -359,12 +385,21 @@ int mlx5e_macsec_init(struct mlx5e_priv *priv)

macsec->mdev = mdev;

macsec_fs = mlx5e_macsec_fs_init(mdev, priv->netdev);
if (IS_ERR_OR_NULL(macsec_fs))
goto err_out;

macsec->macsec_fs = macsec_fs;

mlx5_core_dbg(mdev, "MACsec attached to netdevice\n");

return 0;

err_out:
mlx5_core_dealloc_pd(priv->mdev, macsec->aso_pdn);
err_pd:
kfree(macsec);
priv->macsec = NULL;
return err;
}

Expand All @@ -375,6 +410,8 @@ void mlx5e_macsec_cleanup(struct mlx5e_priv *priv)
if (!macsec)
return;

mlx5e_macsec_fs_cleanup(macsec->macsec_fs);

priv->macsec = NULL;

mlx5_core_dealloc_pd(priv->mdev, macsec->aso_pdn);
Expand Down
Loading

0 comments on commit e467b28

Please sign in to comment.