Commit c674df97 authored by Leon Romanovsky's avatar Leon Romanovsky Committed by Saeed Mahameed

net/mlx5: Store IPsec ESN update work in XFRM state

mlx5 IPsec code updated ESN through workqueue with allocation calls
in the data path, which can be saved easily if the work is created
during XFRM state initialization routine.

The locking used later in the work didn't protect from anything because
change of HW context is possible during XFRM state add or delete only,
which can cancel work and make sure that it is not running.
Reviewed-by: default avatarRaed Salem <raeds@nvidia.com>
Signed-off-by: default avatarLeon Romanovsky <leonro@nvidia.com>
Signed-off-by: default avatarSaeed Mahameed <saeedm@nvidia.com>
parent a05a5469
...@@ -271,6 +271,16 @@ static inline int mlx5e_xfrm_validate_state(struct xfrm_state *x) ...@@ -271,6 +271,16 @@ static inline int mlx5e_xfrm_validate_state(struct xfrm_state *x)
return 0; return 0;
} }
static void _update_xfrm_state(struct work_struct *work)
{
struct mlx5e_ipsec_modify_state_work *modify_work =
container_of(work, struct mlx5e_ipsec_modify_state_work, work);
struct mlx5e_ipsec_sa_entry *sa_entry = container_of(
modify_work, struct mlx5e_ipsec_sa_entry, modify_work);
mlx5_accel_esp_modify_xfrm(sa_entry->xfrm, &modify_work->attrs);
}
static int mlx5e_xfrm_add_state(struct xfrm_state *x) static int mlx5e_xfrm_add_state(struct xfrm_state *x)
{ {
struct mlx5e_ipsec_sa_entry *sa_entry = NULL; struct mlx5e_ipsec_sa_entry *sa_entry = NULL;
...@@ -334,6 +344,7 @@ static int mlx5e_xfrm_add_state(struct xfrm_state *x) ...@@ -334,6 +344,7 @@ static int mlx5e_xfrm_add_state(struct xfrm_state *x)
mlx5e_ipsec_set_iv_esn : mlx5e_ipsec_set_iv; mlx5e_ipsec_set_iv_esn : mlx5e_ipsec_set_iv;
} }
INIT_WORK(&sa_entry->modify_work.work, _update_xfrm_state);
x->xso.offload_handle = (unsigned long)sa_entry; x->xso.offload_handle = (unsigned long)sa_entry;
goto out; goto out;
...@@ -365,7 +376,7 @@ static void mlx5e_xfrm_free_state(struct xfrm_state *x) ...@@ -365,7 +376,7 @@ static void mlx5e_xfrm_free_state(struct xfrm_state *x)
struct mlx5e_priv *priv = netdev_priv(x->xso.dev); struct mlx5e_priv *priv = netdev_priv(x->xso.dev);
if (sa_entry->hw_context) { if (sa_entry->hw_context) {
flush_workqueue(sa_entry->ipsec->wq); cancel_work_sync(&sa_entry->modify_work.work);
mlx5e_accel_ipsec_fs_del_rule(priv, &sa_entry->xfrm->attrs, mlx5e_accel_ipsec_fs_del_rule(priv, &sa_entry->xfrm->attrs,
&sa_entry->ipsec_rule); &sa_entry->ipsec_rule);
mlx5_accel_esp_free_hw_context(sa_entry->xfrm->mdev, sa_entry->hw_context); mlx5_accel_esp_free_hw_context(sa_entry->xfrm->mdev, sa_entry->hw_context);
...@@ -392,7 +403,6 @@ int mlx5e_ipsec_init(struct mlx5e_priv *priv) ...@@ -392,7 +403,6 @@ int mlx5e_ipsec_init(struct mlx5e_priv *priv)
hash_init(ipsec->sadb_rx); hash_init(ipsec->sadb_rx);
spin_lock_init(&ipsec->sadb_rx_lock); spin_lock_init(&ipsec->sadb_rx_lock);
ipsec->mdev = priv->mdev; ipsec->mdev = priv->mdev;
ipsec->en_priv = priv;
ipsec->wq = alloc_ordered_workqueue("mlx5e_ipsec: %s", 0, ipsec->wq = alloc_ordered_workqueue("mlx5e_ipsec: %s", 0,
priv->netdev->name); priv->netdev->name);
if (!ipsec->wq) { if (!ipsec->wq) {
...@@ -424,7 +434,6 @@ void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv) ...@@ -424,7 +434,6 @@ void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
mlx5e_accel_ipsec_fs_cleanup(ipsec); mlx5e_accel_ipsec_fs_cleanup(ipsec);
destroy_workqueue(ipsec->wq); destroy_workqueue(ipsec->wq);
kfree(ipsec); kfree(ipsec);
priv->ipsec = NULL; priv->ipsec = NULL;
} }
...@@ -444,47 +453,19 @@ static bool mlx5e_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x) ...@@ -444,47 +453,19 @@ static bool mlx5e_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
return true; return true;
} }
struct mlx5e_ipsec_modify_state_work {
struct work_struct work;
struct mlx5_accel_esp_xfrm_attrs attrs;
struct mlx5e_ipsec_sa_entry *sa_entry;
};
static void _update_xfrm_state(struct work_struct *work)
{
int ret;
struct mlx5e_ipsec_modify_state_work *modify_work =
container_of(work, struct mlx5e_ipsec_modify_state_work, work);
struct mlx5e_ipsec_sa_entry *sa_entry = modify_work->sa_entry;
ret = mlx5_accel_esp_modify_xfrm(sa_entry->xfrm,
&modify_work->attrs);
if (ret)
netdev_warn(sa_entry->ipsec->en_priv->netdev,
"Not an IPSec offload device\n");
kfree(modify_work);
}
static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x) static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x)
{ {
struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x); struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
struct mlx5e_ipsec_modify_state_work *modify_work; struct mlx5e_ipsec_modify_state_work *modify_work =
&sa_entry->modify_work;
bool need_update; bool need_update;
need_update = mlx5e_ipsec_update_esn_state(sa_entry); need_update = mlx5e_ipsec_update_esn_state(sa_entry);
if (!need_update) if (!need_update)
return; return;
modify_work = kzalloc(sizeof(*modify_work), GFP_ATOMIC);
if (!modify_work)
return;
mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &modify_work->attrs); mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &modify_work->attrs);
modify_work->sa_entry = sa_entry; queue_work(sa_entry->ipsec->wq, &modify_work->work);
INIT_WORK(&modify_work->work, _update_xfrm_state);
WARN_ON(!queue_work(sa_entry->ipsec->wq, &modify_work->work));
} }
static const struct xfrmdev_ops mlx5e_ipsec_xfrmdev_ops = { static const struct xfrmdev_ops mlx5e_ipsec_xfrmdev_ops = {
......
...@@ -62,7 +62,6 @@ struct mlx5e_ipsec_tx; ...@@ -62,7 +62,6 @@ struct mlx5e_ipsec_tx;
struct mlx5e_ipsec { struct mlx5e_ipsec {
struct mlx5_core_dev *mdev; struct mlx5_core_dev *mdev;
struct mlx5e_priv *en_priv;
DECLARE_HASHTABLE(sadb_rx, MLX5E_IPSEC_SADB_RX_BITS); DECLARE_HASHTABLE(sadb_rx, MLX5E_IPSEC_SADB_RX_BITS);
spinlock_t sadb_rx_lock; /* Protects sadb_rx */ spinlock_t sadb_rx_lock; /* Protects sadb_rx */
struct mlx5e_ipsec_sw_stats sw_stats; struct mlx5e_ipsec_sw_stats sw_stats;
...@@ -82,6 +81,11 @@ struct mlx5e_ipsec_rule { ...@@ -82,6 +81,11 @@ struct mlx5e_ipsec_rule {
struct mlx5_modify_hdr *set_modify_hdr; struct mlx5_modify_hdr *set_modify_hdr;
}; };
struct mlx5e_ipsec_modify_state_work {
struct work_struct work;
struct mlx5_accel_esp_xfrm_attrs attrs;
};
struct mlx5e_ipsec_sa_entry { struct mlx5e_ipsec_sa_entry {
struct hlist_node hlist; /* Item in SADB_RX hashtable */ struct hlist_node hlist; /* Item in SADB_RX hashtable */
struct mlx5e_ipsec_esn_state esn_state; struct mlx5e_ipsec_esn_state esn_state;
...@@ -94,6 +98,7 @@ struct mlx5e_ipsec_sa_entry { ...@@ -94,6 +98,7 @@ struct mlx5e_ipsec_sa_entry {
struct xfrm_offload *xo); struct xfrm_offload *xo);
u32 ipsec_obj_id; u32 ipsec_obj_id;
struct mlx5e_ipsec_rule ipsec_rule; struct mlx5e_ipsec_rule ipsec_rule;
struct mlx5e_ipsec_modify_state_work modify_work;
}; };
int mlx5e_ipsec_init(struct mlx5e_priv *priv); int mlx5e_ipsec_init(struct mlx5e_priv *priv);
......
...@@ -18,7 +18,6 @@ struct mlx5_ipsec_sa_ctx { ...@@ -18,7 +18,6 @@ struct mlx5_ipsec_sa_ctx {
struct mlx5_ipsec_esp_xfrm { struct mlx5_ipsec_esp_xfrm {
/* reference counter of SA ctx */ /* reference counter of SA ctx */
struct mlx5_ipsec_sa_ctx *sa_ctx; struct mlx5_ipsec_sa_ctx *sa_ctx;
struct mutex lock; /* protects mlx5_ipsec_esp_xfrm */
struct mlx5_accel_esp_xfrm accel_xfrm; struct mlx5_accel_esp_xfrm accel_xfrm;
}; };
...@@ -117,7 +116,6 @@ mlx5_ipsec_offload_esp_create_xfrm(struct mlx5_core_dev *mdev, ...@@ -117,7 +116,6 @@ mlx5_ipsec_offload_esp_create_xfrm(struct mlx5_core_dev *mdev,
if (!mxfrm) if (!mxfrm)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
mutex_init(&mxfrm->lock);
memcpy(&mxfrm->accel_xfrm.attrs, attrs, memcpy(&mxfrm->accel_xfrm.attrs, attrs,
sizeof(mxfrm->accel_xfrm.attrs)); sizeof(mxfrm->accel_xfrm.attrs));
...@@ -129,8 +127,6 @@ static void mlx5_ipsec_offload_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm ...@@ -129,8 +127,6 @@ static void mlx5_ipsec_offload_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm
struct mlx5_ipsec_esp_xfrm *mxfrm = container_of(xfrm, struct mlx5_ipsec_esp_xfrm, struct mlx5_ipsec_esp_xfrm *mxfrm = container_of(xfrm, struct mlx5_ipsec_esp_xfrm,
accel_xfrm); accel_xfrm);
/* assuming no sa_ctx are connected to this xfrm_ctx */
WARN_ON(mxfrm->sa_ctx);
kfree(mxfrm); kfree(mxfrm);
} }
...@@ -232,7 +228,6 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev, ...@@ -232,7 +228,6 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev,
sa_ctx->dev = mdev; sa_ctx->dev = mdev;
mxfrm = container_of(accel_xfrm, struct mlx5_ipsec_esp_xfrm, accel_xfrm); mxfrm = container_of(accel_xfrm, struct mlx5_ipsec_esp_xfrm, accel_xfrm);
mutex_lock(&mxfrm->lock);
sa_ctx->mxfrm = mxfrm; sa_ctx->mxfrm = mxfrm;
/* key */ /* key */
...@@ -258,14 +253,12 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev, ...@@ -258,14 +253,12 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev,
*hw_handle = sa_ctx->ipsec_obj_id; *hw_handle = sa_ctx->ipsec_obj_id;
mxfrm->sa_ctx = sa_ctx; mxfrm->sa_ctx = sa_ctx;
mutex_unlock(&mxfrm->lock);
return sa_ctx; return sa_ctx;
err_enc_key: err_enc_key:
mlx5_destroy_encryption_key(mdev, sa_ctx->enc_key_id); mlx5_destroy_encryption_key(mdev, sa_ctx->enc_key_id);
err_sa_ctx: err_sa_ctx:
mutex_unlock(&mxfrm->lock);
kfree(sa_ctx); kfree(sa_ctx);
return ERR_PTR(err); return ERR_PTR(err);
} }
...@@ -273,14 +266,10 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev, ...@@ -273,14 +266,10 @@ static void *mlx5_ipsec_offload_create_sa_ctx(struct mlx5_core_dev *mdev,
static void mlx5_ipsec_offload_delete_sa_ctx(void *context) static void mlx5_ipsec_offload_delete_sa_ctx(void *context)
{ {
struct mlx5_ipsec_sa_ctx *sa_ctx = (struct mlx5_ipsec_sa_ctx *)context; struct mlx5_ipsec_sa_ctx *sa_ctx = (struct mlx5_ipsec_sa_ctx *)context;
struct mlx5_ipsec_esp_xfrm *mxfrm = sa_ctx->mxfrm;
mutex_lock(&mxfrm->lock);
mlx5_destroy_ipsec_obj(sa_ctx->dev, sa_ctx->ipsec_obj_id); mlx5_destroy_ipsec_obj(sa_ctx->dev, sa_ctx->ipsec_obj_id);
mlx5_destroy_encryption_key(sa_ctx->dev, sa_ctx->enc_key_id); mlx5_destroy_encryption_key(sa_ctx->dev, sa_ctx->enc_key_id);
kfree(sa_ctx); kfree(sa_ctx);
mxfrm->sa_ctx = NULL;
mutex_unlock(&mxfrm->lock);
} }
static int mlx5_modify_ipsec_obj(struct mlx5_core_dev *mdev, static int mlx5_modify_ipsec_obj(struct mlx5_core_dev *mdev,
...@@ -331,29 +320,17 @@ static int mlx5_modify_ipsec_obj(struct mlx5_core_dev *mdev, ...@@ -331,29 +320,17 @@ static int mlx5_modify_ipsec_obj(struct mlx5_core_dev *mdev,
return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out)); return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
} }
static int mlx5_ipsec_offload_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm, static void mlx5_ipsec_offload_esp_modify_xfrm(
const struct mlx5_accel_esp_xfrm_attrs *attrs) struct mlx5_accel_esp_xfrm *xfrm,
const struct mlx5_accel_esp_xfrm_attrs *attrs)
{ {
struct mlx5_ipsec_obj_attrs ipsec_attrs = {}; struct mlx5_ipsec_obj_attrs ipsec_attrs = {};
struct mlx5_core_dev *mdev = xfrm->mdev; struct mlx5_core_dev *mdev = xfrm->mdev;
struct mlx5_ipsec_esp_xfrm *mxfrm; struct mlx5_ipsec_esp_xfrm *mxfrm;
int err = 0; int err = 0;
if (!memcmp(&xfrm->attrs, attrs, sizeof(xfrm->attrs)))
return 0;
if (mlx5_ipsec_offload_esp_validate_xfrm_attrs(mdev, attrs))
return -EOPNOTSUPP;
mxfrm = container_of(xfrm, struct mlx5_ipsec_esp_xfrm, accel_xfrm); mxfrm = container_of(xfrm, struct mlx5_ipsec_esp_xfrm, accel_xfrm);
mutex_lock(&mxfrm->lock);
if (!mxfrm->sa_ctx)
/* Not bound xfrm, change only sw attrs */
goto change_sw_xfrm_attrs;
/* need to add find and replace in ipsec_rhash_sa the sa_ctx */ /* need to add find and replace in ipsec_rhash_sa the sa_ctx */
/* modify device with new hw_sa */ /* modify device with new hw_sa */
ipsec_attrs.accel_flags = attrs->flags; ipsec_attrs.accel_flags = attrs->flags;
...@@ -362,12 +339,8 @@ static int mlx5_ipsec_offload_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm, ...@@ -362,12 +339,8 @@ static int mlx5_ipsec_offload_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm,
&ipsec_attrs, &ipsec_attrs,
mxfrm->sa_ctx->ipsec_obj_id); mxfrm->sa_ctx->ipsec_obj_id);
change_sw_xfrm_attrs:
if (!err) if (!err)
memcpy(&xfrm->attrs, attrs, sizeof(xfrm->attrs)); memcpy(&xfrm->attrs, attrs, sizeof(xfrm->attrs));
mutex_unlock(&mxfrm->lock);
return err;
} }
void *mlx5_accel_esp_create_hw_context(struct mlx5_core_dev *mdev, void *mlx5_accel_esp_create_hw_context(struct mlx5_core_dev *mdev,
...@@ -413,8 +386,8 @@ void mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm) ...@@ -413,8 +386,8 @@ void mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm)
mlx5_ipsec_offload_esp_destroy_xfrm(xfrm); mlx5_ipsec_offload_esp_destroy_xfrm(xfrm);
} }
int mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm, void mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm,
const struct mlx5_accel_esp_xfrm_attrs *attrs) const struct mlx5_accel_esp_xfrm_attrs *attrs)
{ {
return mlx5_ipsec_offload_esp_modify_xfrm(xfrm, attrs); mlx5_ipsec_offload_esp_modify_xfrm(xfrm, attrs);
} }
...@@ -127,8 +127,8 @@ struct mlx5_accel_esp_xfrm * ...@@ -127,8 +127,8 @@ struct mlx5_accel_esp_xfrm *
mlx5_accel_esp_create_xfrm(struct mlx5_core_dev *mdev, mlx5_accel_esp_create_xfrm(struct mlx5_core_dev *mdev,
const struct mlx5_accel_esp_xfrm_attrs *attrs); const struct mlx5_accel_esp_xfrm_attrs *attrs);
void mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm); void mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm);
int mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm, void mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm,
const struct mlx5_accel_esp_xfrm_attrs *attrs); const struct mlx5_accel_esp_xfrm_attrs *attrs);
#else #else
...@@ -145,9 +145,11 @@ mlx5_accel_esp_create_xfrm(struct mlx5_core_dev *mdev, ...@@ -145,9 +145,11 @@ mlx5_accel_esp_create_xfrm(struct mlx5_core_dev *mdev,
} }
static inline void static inline void
mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm) {} mlx5_accel_esp_destroy_xfrm(struct mlx5_accel_esp_xfrm *xfrm) {}
static inline int static inline void
mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm, mlx5_accel_esp_modify_xfrm(struct mlx5_accel_esp_xfrm *xfrm,
const struct mlx5_accel_esp_xfrm_attrs *attrs) { return -EOPNOTSUPP; } const struct mlx5_accel_esp_xfrm_attrs *attrs)
{
}
#endif /* CONFIG_MLX5_EN_IPSEC */ #endif /* CONFIG_MLX5_EN_IPSEC */
#endif /* __MLX5_ACCEL_H__ */ #endif /* __MLX5_ACCEL_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment