From: Jianbo Liu IPsec counters are recreated on rule updates (e.g. due to a neighbour update), which loses the previous counter values and may race with the stats update itself. Create the counters once when the SA is added and destroy them when it is freed. Move the counters to the SA itself now that they're no longer owned by the rule. Triggered in CI testing. Reproduced on ConnectX-7 (fw 28.48.1000): 800 pings through tunnel mode SAs while about 3900 "ip neigh replace" commands switch the peer neighbour between reachable, stale and delay (same MAC). x->curlft.packets shows 180 on the TX SA and 88 on the RX SA. With the fix both show 800. Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode") Fixes: dddb49b63d86 ("net/mlx5e: Add IPsec and ASO syndromes check in HW") Signed-off-by: Jianbo Liu Reviewed-by: Saeed Mahameed Reviewed-by: Cosmin Ratiu Signed-off-by: Tariq Toukan --- .../mellanox/mlx5/core/en_accel/ipsec.c | 20 ++- .../mellanox/mlx5/core/en_accel/ipsec.h | 12 +- .../mellanox/mlx5/core/en_accel/ipsec_fs.c | 135 +++++++++--------- 3 files changed, 92 insertions(+), 75 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c index 60f7dc3df355..d37c1deca307 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c @@ -900,12 +900,16 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, if (err) goto release_dwork; - err = mlx5e_accel_ipsec_fs_add_rule(sa_entry); + err = mlx5e_accel_ipsec_fs_alloc_counters(sa_entry); if (err) goto err_hw_ctx; INIT_WORK(&sa_entry->event_work, mlx5e_ipsec_handle_event); + err = mlx5e_accel_ipsec_fs_add_rule(sa_entry); + if (err) + goto err_counters; + /* We use *_bh() variant because xfrm_timer_handler(), which runs * in softirq context, can reach our state delete logic and we need * xa_erase_bh() there. @@ -938,6 +942,8 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, err_add_rule: mlx5e_accel_ipsec_fs_del_rule(sa_entry); +err_counters: + mlx5e_accel_ipsec_fs_free_counters(sa_entry); err_hw_ctx: mlx5_ipsec_free_sa_ctx(sa_entry); release_dwork: @@ -991,6 +997,7 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x) cancel_delayed_work_sync(&sa_entry->dwork->dwork); mlx5e_accel_ipsec_fs_del_rule(sa_entry); + mlx5e_accel_ipsec_fs_free_counters(sa_entry); mlx5_ipsec_free_sa_ctx(sa_entry); kfree(sa_entry->dwork); if (sa_entry->work) @@ -1151,8 +1158,8 @@ static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x) static void mlx5e_xfrm_update_stats(struct xfrm_state *x) { struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x); - struct mlx5e_ipsec_rule *ipsec_rule = &sa_entry->ipsec_rule; struct net *net = dev_net(x->xso.dev); + struct mlx5_fc **fcs = sa_entry->fcs; u64 trailer_packets = 0, trailer_bytes = 0; u64 replay_packets = 0, replay_bytes = 0; u64 auth_packets = 0, auth_bytes = 0; @@ -1168,12 +1175,12 @@ static void mlx5e_xfrm_update_stats(struct xfrm_state *x) return; if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN) { - mlx5_fc_query_cached(ipsec_rule->auth.fc, &auth_bytes, + mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_AUTH], &auth_bytes, &auth_packets, &lastuse); x->stats.integrity_failed += auth_packets; XFRM_ADD_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR, auth_packets); - mlx5_fc_query_cached(ipsec_rule->trailer.fc, &trailer_bytes, + mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_TRAILER], &trailer_bytes, &trailer_packets, &lastuse); XFRM_ADD_STATS(net, LINUX_MIB_XFRMINHDRERROR, trailer_packets); } @@ -1182,13 +1189,14 @@ static void mlx5e_xfrm_update_stats(struct xfrm_state *x) return; if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN) { - mlx5_fc_query_cached(ipsec_rule->replay.fc, &replay_bytes, + mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_REPLAY], &replay_bytes, &replay_packets, &lastuse); x->stats.replay += replay_packets; XFRM_ADD_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR, replay_packets); } - mlx5_fc_query_cached(ipsec_rule->fc, &bytes, &packets, &lastuse); + mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_RULE], &bytes, &packets, + &lastuse); success_packets = packets - auth_packets - trailer_packets - replay_packets; x->curlft.packets += success_packets; /* NIC counts all bytes passed through flow steering and doesn't have diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h index e70189f81ffb..2e8c6fc216de 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h @@ -216,13 +216,20 @@ struct mlx5e_ipsec_drop { struct mlx5_fc *fc; }; +enum { + MLX5_IPSEC_FC_RULE, + MLX5_IPSEC_FC_REPLAY, + MLX5_IPSEC_FC_AUTH, + MLX5_IPSEC_FC_TRAILER, + MLX5_IPSEC_FC_NUM, +}; + struct mlx5e_ipsec_rule { struct mlx5_flow_handle *rule; struct mlx5_flow_handle *status_pass; struct mlx5_flow_handle *sa_sel; struct mlx5_modify_hdr *modify_hdr; struct mlx5_pkt_reformat *pkt_reformat; - struct mlx5_fc *fc; struct mlx5e_ipsec_drop replay; struct mlx5e_ipsec_drop auth; struct mlx5e_ipsec_drop trailer; @@ -295,6 +302,7 @@ struct mlx5e_ipsec_sa_entry { u32 ipsec_obj_id; u32 enc_key_id; struct mlx5e_ipsec_rule ipsec_rule; + struct mlx5_fc *fcs[MLX5_IPSEC_FC_NUM]; struct mlx5e_ipsec_work *work; struct mlx5e_ipsec_dwork *dwork; struct work_struct event_work; @@ -329,6 +337,8 @@ void mlx5e_accel_ipsec_fs_cleanup(struct mlx5e_ipsec *ipsec); int mlx5e_accel_ipsec_fs_init(struct mlx5e_ipsec *ipsec, struct mlx5_devcom_comp_dev **devcom); int mlx5e_accel_ipsec_fs_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry); void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry); +int mlx5e_accel_ipsec_fs_alloc_counters(struct mlx5e_ipsec_sa_entry *sa_entry); +void mlx5e_accel_ipsec_fs_free_counters(struct mlx5e_ipsec_sa_entry *sa_entry); int mlx5e_accel_ipsec_fs_add_pol(struct mlx5e_ipsec_pol_entry *pol_entry); void mlx5e_accel_ipsec_fs_del_pol(struct mlx5e_ipsec_pol_entry *pol_entry); void mlx5e_accel_ipsec_fs_modify(struct mlx5e_ipsec_sa_entry *sa_entry); diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c index d0f738fdeff3..df90ae327f23 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c @@ -195,6 +195,39 @@ static void ipsec_rx_rule_add_match_obj(struct mlx5e_ipsec_sa_entry *sa_entry, } } +static const char * const fc_names[MLX5_IPSEC_FC_NUM] = { + [MLX5_IPSEC_FC_RULE] = "decrypt", + [MLX5_IPSEC_FC_REPLAY] = "replay drop", + [MLX5_IPSEC_FC_AUTH] = "auth drop", + [MLX5_IPSEC_FC_TRAILER] = "trailer drop", +}; + +static int rx_create_counters(struct mlx5e_ipsec_sa_entry *sa_entry) +{ + bool packet = sa_entry->attrs.type == XFRM_DEV_OFFLOAD_PACKET; + struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry); + struct mlx5_fc *fc; + int i, err; + + for (i = 0; i < MLX5_IPSEC_FC_NUM; i++) { + if (i == MLX5_IPSEC_FC_REPLAY && !packet) + continue; + + fc = mlx5_fc_create(mdev, true); + if (IS_ERR(fc)) { + err = PTR_ERR(fc); + mlx5_core_err(mdev, + "Failed to create ipsec rx %s counter, err=%d\n", + fc_names[i], err); + mlx5e_accel_ipsec_fs_free_counters(sa_entry); + return err; + } + sa_entry->fcs[i] = fc; + } + + return 0; +} + static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry, struct mlx5e_ipsec_rx *rx) { @@ -204,7 +237,6 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry, struct mlx5_flow_destination dest = {}; struct mlx5_flow_act flow_act = {}; struct mlx5_flow_handle *rule; - struct mlx5_fc *flow_counter; struct mlx5_flow_spec *spec; int err; @@ -212,19 +244,10 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry, if (!spec) return -ENOMEM; - flow_counter = mlx5_fc_create(mdev, true); - if (IS_ERR(flow_counter)) { - err = PTR_ERR(flow_counter); - mlx5_core_err(mdev, - "Failed to add ipsec rx status drop rule counter, err=%d\n", err); - goto err_cnt; - } - sa_entry->ipsec_rule.auth.fc = flow_counter; - flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP | MLX5_FLOW_CONTEXT_ACTION_COUNT; flow_act.flags = FLOW_ACT_NO_APPEND; dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; - dest.counter = flow_counter; + dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_AUTH]; if (rx == ipsec->rx_esw) spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK; @@ -241,16 +264,7 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry, } sa_entry->ipsec_rule.auth.rule = rule; - flow_counter = mlx5_fc_create(mdev, true); - if (IS_ERR(flow_counter)) { - err = PTR_ERR(flow_counter); - mlx5_core_err(mdev, - "Failed to add ipsec rx status drop rule counter, err=%d\n", err); - goto err_cnt_2; - } - sa_entry->ipsec_rule.trailer.fc = flow_counter; - - dest.counter = flow_counter; + dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_TRAILER]; MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.ipsec_syndrome, 2); rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1); if (IS_ERR(rule)) { @@ -265,12 +279,8 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry, return 0; err_rule_2: - mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.trailer.fc); -err_cnt_2: mlx5_del_flow_rules(sa_entry->ipsec_rule.auth.rule); err_rule: - mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.auth.fc); -err_cnt: kvfree(spec); return err; } @@ -283,7 +293,6 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct struct mlx5_flow_destination dest = {}; struct mlx5_flow_act flow_act = {}; struct mlx5_flow_handle *rule; - struct mlx5_fc *flow_counter; struct mlx5_flow_spec *spec; int err; @@ -291,18 +300,10 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct if (!spec) return -ENOMEM; - flow_counter = mlx5_fc_create(mdev, true); - if (IS_ERR(flow_counter)) { - err = PTR_ERR(flow_counter); - mlx5_core_err(mdev, - "Failed to add ipsec rx status drop rule counter, err=%d\n", err); - goto err_cnt; - } - flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP | MLX5_FLOW_CONTEXT_ACTION_COUNT; flow_act.flags = FLOW_ACT_NO_APPEND; dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; - dest.counter = flow_counter; + dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_REPLAY]; if (rx == ipsec->rx_esw) spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK; @@ -319,14 +320,11 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct } sa_entry->ipsec_rule.replay.rule = rule; - sa_entry->ipsec_rule.replay.fc = flow_counter; kvfree(spec); return 0; err_rule: - mlx5_fc_destroy(mdev, flow_counter); -err_cnt: kvfree(spec); return err; } @@ -2049,7 +2047,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) struct mlx5_flow_handle *rule; struct mlx5_flow_spec *spec; struct mlx5e_ipsec_rx *rx; - struct mlx5_fc *counter; int err = 0; rx = rx_ft_get(mdev, ipsec, attrs->addrs.family, attrs->type); @@ -2094,11 +2091,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) break; } - counter = mlx5_fc_create(mdev, true); - if (IS_ERR(counter)) { - err = PTR_ERR(counter); - goto err_add_cnt; - } flow_act.crypto.type = MLX5_FLOW_CONTEXT_ENCRYPT_DECRYPT_TYPE_IPSEC; flow_act.crypto.obj_id = sa_entry->ipsec_obj_id; flow_act.flags |= FLOW_ACT_NO_APPEND; @@ -2117,7 +2109,7 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; dest[0].ft = rx->ft.status; dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; - dest[1].counter = counter; + dest[1].counter = sa_entry->fcs[MLX5_IPSEC_FC_RULE]; rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, dest, 2); } if (IS_ERR(rule)) { @@ -2145,15 +2137,12 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) sa_entry->ipsec_rule.rule = rule; sa_entry->ipsec_rule.modify_hdr = flow_act.modify_hdr; - sa_entry->ipsec_rule.fc = counter; sa_entry->ipsec_rule.pkt_reformat = flow_act.pkt_reformat; return 0; err_drop_reason: - if (sa_entry->ipsec_rule.replay.rule) { + if (sa_entry->ipsec_rule.replay.rule) mlx5_del_flow_rules(sa_entry->ipsec_rule.replay.rule); - mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.replay.fc); - } err_add_replay: if (sa_entry->ipsec_rule.sa_sel) { mlx5_del_flow_rules(sa_entry->ipsec_rule.sa_sel); @@ -2162,8 +2151,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) err_add_sa_sel: mlx5_del_flow_rules(rule); err_add_flow: - mlx5_fc_destroy(mdev, counter); -err_add_cnt: if (flow_act.pkt_reformat) mlx5_packet_reformat_dealloc(mdev, flow_act.pkt_reformat); err_pkt_reformat: @@ -2186,7 +2173,6 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) struct mlx5_flow_handle *rule; struct mlx5_flow_spec *spec; struct mlx5e_ipsec_tx *tx; - struct mlx5_fc *counter; int err; tx = tx_ft_get(mdev, ipsec, attrs->type); @@ -2222,12 +2208,6 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) break; } - counter = mlx5_fc_create(mdev, true); - if (IS_ERR(counter)) { - err = PTR_ERR(counter); - goto err_add_cnt; - } - flow_act.crypto.type = MLX5_FLOW_CONTEXT_ENCRYPT_DECRYPT_TYPE_IPSEC; flow_act.crypto.obj_id = sa_entry->ipsec_obj_id; flow_act.flags |= FLOW_ACT_NO_APPEND; @@ -2241,7 +2221,7 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) dest[0].ft = tx->ft.status; dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; - dest[1].counter = counter; + dest[1].counter = sa_entry->fcs[MLX5_IPSEC_FC_RULE]; rule = mlx5_add_flow_rules(tx->ft.sa, spec, &flow_act, dest, 2); if (IS_ERR(rule)) { err = PTR_ERR(rule); @@ -2251,13 +2231,10 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry) kvfree(spec); sa_entry->ipsec_rule.rule = rule; - sa_entry->ipsec_rule.fc = counter; sa_entry->ipsec_rule.pkt_reformat = flow_act.pkt_reformat; return 0; err_add_flow: - mlx5_fc_destroy(mdev, counter); -err_add_cnt: if (flow_act.pkt_reformat) mlx5_packet_reformat_dealloc(mdev, flow_act.pkt_reformat); err_pkt_reformat: @@ -2645,7 +2622,6 @@ void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry) struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry); mlx5_del_flow_rules(ipsec_rule->rule); - mlx5_fc_destroy(mdev, ipsec_rule->fc); if (ipsec_rule->pkt_reformat) mlx5_packet_reformat_dealloc(mdev, ipsec_rule->pkt_reformat); @@ -2661,25 +2637,48 @@ void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry) mlx5_modify_header_dealloc(mdev, ipsec_rule->modify_hdr); mlx5_del_flow_rules(ipsec_rule->trailer.rule); - mlx5_fc_destroy(mdev, ipsec_rule->trailer.fc); mlx5_del_flow_rules(ipsec_rule->auth.rule); - mlx5_fc_destroy(mdev, ipsec_rule->auth.fc); if (ipsec_rule->sa_sel) { mlx5_del_flow_rules(ipsec_rule->sa_sel); mlx5_del_flow_rules(ipsec_rule->status_pass); } - if (ipsec_rule->replay.rule) { + if (ipsec_rule->replay.rule) mlx5_del_flow_rules(ipsec_rule->replay.rule); - mlx5_fc_destroy(mdev, ipsec_rule->replay.fc); - } mlx5_esw_ipsec_rx_id_mapping_remove(sa_entry); rx_ft_put(sa_entry->ipsec, sa_entry->attrs.addrs.family, sa_entry->attrs.type); } +int mlx5e_accel_ipsec_fs_alloc_counters(struct mlx5e_ipsec_sa_entry *sa_entry) +{ + struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry); + struct mlx5_fc *fc; + + if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN) + return rx_create_counters(sa_entry); + + fc = mlx5_fc_create(mdev, true); + if (IS_ERR(fc)) + return PTR_ERR(fc); + + sa_entry->fcs[MLX5_IPSEC_FC_RULE] = fc; + return 0; +} + +void mlx5e_accel_ipsec_fs_free_counters(struct mlx5e_ipsec_sa_entry *sa_entry) +{ + struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry); + int i; + + for (i = 0; i < MLX5_IPSEC_FC_NUM; i++) { + mlx5_fc_destroy(mdev, sa_entry->fcs[i]); + sa_entry->fcs[i] = NULL; + } +} + int mlx5e_accel_ipsec_fs_add_pol(struct mlx5e_ipsec_pol_entry *pol_entry) { int err; -- 2.44.0