1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies Inc. All rights reserved. */
9 static void esw_acl_egress_lgcy_rules_destroy(struct mlx5_vport *vport)
11 esw_acl_egress_vlan_destroy(vport);
12 if (!IS_ERR_OR_NULL(vport->egress.legacy.drop_rule)) {
13 mlx5_del_flow_rules(vport->egress.legacy.drop_rule);
14 vport->egress.legacy.drop_rule = NULL;
18 static int esw_acl_egress_lgcy_groups_create(struct mlx5_eswitch *esw,
19 struct mlx5_vport *vport)
21 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
22 struct mlx5_core_dev *dev = esw->dev;
23 struct mlx5_flow_group *drop_grp;
27 err = esw_acl_egress_vlan_grp_create(esw, vport);
31 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
37 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 1);
38 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 1);
39 drop_grp = mlx5_create_flow_group(vport->egress.acl, flow_group_in);
40 if (IS_ERR(drop_grp)) {
41 err = PTR_ERR(drop_grp);
42 esw_warn(dev, "Failed to create E-Switch vport[%d] egress drop flow group, err(%d)\n",
47 vport->egress.legacy.drop_grp = drop_grp;
48 kvfree(flow_group_in);
52 kvfree(flow_group_in);
54 esw_acl_egress_vlan_grp_destroy(vport);
58 static void esw_acl_egress_lgcy_groups_destroy(struct mlx5_vport *vport)
60 if (!IS_ERR_OR_NULL(vport->egress.legacy.drop_grp)) {
61 mlx5_destroy_flow_group(vport->egress.legacy.drop_grp);
62 vport->egress.legacy.drop_grp = NULL;
64 esw_acl_egress_vlan_grp_destroy(vport);
67 int esw_acl_egress_lgcy_setup(struct mlx5_eswitch *esw,
68 struct mlx5_vport *vport)
70 struct mlx5_flow_destination drop_ctr_dst = {};
71 struct mlx5_flow_destination *dst = NULL;
72 struct mlx5_fc *drop_counter = NULL;
73 struct mlx5_flow_act flow_act = {};
74 /* The egress acl table contains 2 rules:
75 * 1)Allow traffic with vlan_tag=vst_vlan_id
76 * 2)Drop all other traffic.
82 if (vport->egress.legacy.drop_counter) {
83 drop_counter = vport->egress.legacy.drop_counter;
84 } else if (MLX5_CAP_ESW_EGRESS_ACL(esw->dev, flow_counter)) {
85 drop_counter = mlx5_fc_create(esw->dev, false);
86 if (IS_ERR(drop_counter)) {
88 "vport[%d] configure egress drop rule counter err(%ld)\n",
89 vport->vport, PTR_ERR(drop_counter));
92 vport->egress.legacy.drop_counter = drop_counter;
95 esw_acl_egress_lgcy_rules_destroy(vport);
97 if (!vport->info.vlan && !vport->info.qos) {
98 esw_acl_egress_lgcy_cleanup(esw, vport);
102 if (!vport->egress.acl) {
103 vport->egress.acl = esw_acl_table_create(esw, vport,
104 MLX5_FLOW_NAMESPACE_ESW_EGRESS,
106 if (IS_ERR(vport->egress.acl)) {
107 err = PTR_ERR(vport->egress.acl);
108 vport->egress.acl = NULL;
112 err = esw_acl_egress_lgcy_groups_create(esw, vport);
118 "vport[%d] configure egress rules, vlan(%d) qos(%d)\n",
119 vport->vport, vport->info.vlan, vport->info.qos);
121 /* Allowed vlan rule */
122 err = esw_egress_acl_vlan_create(esw, vport, NULL, vport->info.vlan,
123 MLX5_FLOW_CONTEXT_ACTION_ALLOW);
127 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
129 /* Attach egress drop flow counter */
131 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
132 drop_ctr_dst.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
133 drop_ctr_dst.counter_id = mlx5_fc_id(drop_counter);
137 vport->egress.legacy.drop_rule =
138 mlx5_add_flow_rules(vport->egress.acl, NULL,
139 &flow_act, dst, dest_num);
140 if (IS_ERR(vport->egress.legacy.drop_rule)) {
141 err = PTR_ERR(vport->egress.legacy.drop_rule);
143 "vport[%d] configure egress drop rule failed, err(%d)\n",
145 vport->egress.legacy.drop_rule = NULL;
152 esw_acl_egress_lgcy_cleanup(esw, vport);
156 void esw_acl_egress_lgcy_cleanup(struct mlx5_eswitch *esw,
157 struct mlx5_vport *vport)
159 if (IS_ERR_OR_NULL(vport->egress.acl))
160 goto clean_drop_counter;
162 esw_debug(esw->dev, "Destroy vport[%d] E-Switch egress ACL\n", vport->vport);
164 esw_acl_egress_lgcy_rules_destroy(vport);
165 esw_acl_egress_lgcy_groups_destroy(vport);
166 esw_acl_egress_table_destroy(vport);
169 if (vport->egress.legacy.drop_counter) {
170 mlx5_fc_destroy(esw->dev, vport->egress.legacy.drop_counter);
171 vport->egress.legacy.drop_counter = NULL;