| 1 |
/* |
| 2 |
* Copyright (c) 2016, Mellanox Technologies. All rights reserved. |
| 3 |
* |
| 4 |
* This software is available to you under a choice of one of two |
| 5 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 6 |
* General Public License (GPL) Version 2, available from the file |
| 7 |
* COPYING in the main directory of this source tree, or the |
| 8 |
* OpenIB.org BSD license below: |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or |
| 11 |
* without modification, are permitted provided that the following |
| 12 |
* conditions are met: |
| 13 |
* |
| 14 |
* - Redistributions of source code must retain the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer. |
| 17 |
* |
| 18 |
* - Redistributions in binary form must reproduce the above |
| 19 |
* copyright notice, this list of conditions and the following |
| 20 |
* disclaimer in the documentation and/or other materials |
| 21 |
* provided with the distribution. |
| 22 |
* |
| 23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
* SOFTWARE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <linux/hash.h> |
| 34 |
#include <linux/mlx5/fs.h> |
| 35 |
#include <linux/ip.h> |
| 36 |
#include <linux/ipv6.h> |
| 37 |
#include <net/rps.h> |
| 38 |
#include "en.h" |
| 39 |
|
| 40 |
#define ARFS_HASH_SHIFT BITS_PER_BYTE |
| 41 |
#define ARFS_HASH_SIZE BIT(BITS_PER_BYTE) |
| 42 |
|
| 43 |
struct arfs_table { |
| 44 |
struct mlx5e_flow_table ft; |
| 45 |
struct mlx5_flow_handle *default_rule; |
| 46 |
struct hlist_head rules_hash[ARFS_HASH_SIZE]; |
| 47 |
}; |
| 48 |
|
| 49 |
enum { |
| 50 |
MLX5E_ARFS_STATE_ENABLED, |
| 51 |
}; |
| 52 |
|
| 53 |
enum arfs_type { |
| 54 |
ARFS_IPV4_TCP, |
| 55 |
ARFS_IPV6_TCP, |
| 56 |
ARFS_IPV4_UDP, |
| 57 |
ARFS_IPV6_UDP, |
| 58 |
ARFS_NUM_TYPES, |
| 59 |
}; |
| 60 |
|
| 61 |
struct mlx5e_arfs_tables { |
| 62 |
struct arfs_table arfs_tables[ARFS_NUM_TYPES]; |
| 63 |
/* Protect aRFS rules list */ |
| 64 |
spinlock_t arfs_lock; |
| 65 |
int last_filter_id; |
| 66 |
struct workqueue_struct *wq; |
| 67 |
unsigned long state; |
| 68 |
}; |
| 69 |
|
| 70 |
struct arfs_tuple { |
| 71 |
__be16 etype; |
| 72 |
u8 ip_proto; |
| 73 |
union { |
| 74 |
__be32 src_ipv4; |
| 75 |
struct in6_addr src_ipv6; |
| 76 |
}; |
| 77 |
union { |
| 78 |
__be32 dst_ipv4; |
| 79 |
struct in6_addr dst_ipv6; |
| 80 |
}; |
| 81 |
__be16 src_port; |
| 82 |
__be16 dst_port; |
| 83 |
}; |
| 84 |
|
| 85 |
struct arfs_rule { |
| 86 |
struct mlx5e_priv *priv; |
| 87 |
struct work_struct arfs_work; |
| 88 |
struct mlx5_flow_handle *rule; |
| 89 |
struct hlist_node hlist; |
| 90 |
int rxq; |
| 91 |
/* Flow ID passed to ndo_rx_flow_steer */ |
| 92 |
int flow_id; |
| 93 |
/* Filter ID returned by ndo_rx_flow_steer */ |
| 94 |
int filter_id; |
| 95 |
struct arfs_tuple tuple; |
| 96 |
}; |
| 97 |
|
| 98 |
#define mlx5e_for_each_arfs_rule(hn, tmp, arfs_tables, i, j) \ |
| 99 |
for (i = 0; i < ARFS_NUM_TYPES; i++) \ |
| 100 |
mlx5e_for_each_hash_arfs_rule(hn, tmp, arfs_tables[i].rules_hash, j) |
| 101 |
|
| 102 |
#define mlx5e_for_each_hash_arfs_rule(hn, tmp, hash, j) \ |
| 103 |
for (j = 0; j < ARFS_HASH_SIZE; j++) \ |
| 104 |
hlist_for_each_entry_safe(hn, tmp, &hash[j], hlist) |
| 105 |
|
| 106 |
static enum mlx5_traffic_types arfs_get_tt(enum arfs_type type) |
| 107 |
{ |
| 108 |
switch (type) { |
| 109 |
case ARFS_IPV4_TCP: |
| 110 |
return MLX5_TT_IPV4_TCP; |
| 111 |
case ARFS_IPV4_UDP: |
| 112 |
return MLX5_TT_IPV4_UDP; |
| 113 |
case ARFS_IPV6_TCP: |
| 114 |
return MLX5_TT_IPV6_TCP; |
| 115 |
case ARFS_IPV6_UDP: |
| 116 |
return MLX5_TT_IPV6_UDP; |
| 117 |
default: |
| 118 |
return -EINVAL; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static int arfs_disable(struct mlx5e_flow_steering *fs) |
| 123 |
{ |
| 124 |
struct mlx5_ttc_table *ttc = mlx5e_fs_get_ttc(fs, false); |
| 125 |
int err, i; |
| 126 |
|
| 127 |
for (i = 0; i < ARFS_NUM_TYPES; i++) { |
| 128 |
/* Modify ttc rules destination back to their default */ |
| 129 |
err = mlx5_ttc_fwd_default_dest(ttc, arfs_get_tt(i)); |
| 130 |
if (err) { |
| 131 |
fs_err(fs, |
| 132 |
"%s: modify ttc[%d] default destination failed, err(%d)\n", |
| 133 |
__func__, arfs_get_tt(i), err); |
| 134 |
return err; |
| 135 |
} |
| 136 |
} |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static void arfs_del_rules(struct mlx5e_flow_steering *fs); |
| 141 |
|
| 142 |
int mlx5e_arfs_disable(struct mlx5e_flow_steering *fs) |
| 143 |
{ |
| 144 |
/* Moving to switchdev mode, fs->arfs is freed by mlx5e_nic_profile |
| 145 |
* cleanup_rx callback and it is not recreated when |
| 146 |
* mlx5e_uplink_rep_profile is loaded as mlx5e_create_flow_steering() |
| 147 |
* is not called by the uplink_rep profile init_rx callback. Thus, if |
| 148 |
* ntuple is set, moving to switchdev flow will enter this function |
| 149 |
* with fs->arfs nullified. |
| 150 |
*/ |
| 151 |
if (!mlx5e_fs_get_arfs(fs)) |
| 152 |
return 0; |
| 153 |
|
| 154 |
arfs_del_rules(fs); |
| 155 |
|
| 156 |
return arfs_disable(fs); |
| 157 |
} |
| 158 |
|
| 159 |
int mlx5e_arfs_enable(struct mlx5e_flow_steering *fs) |
| 160 |
{ |
| 161 |
struct mlx5_ttc_table *ttc = mlx5e_fs_get_ttc(fs, false); |
| 162 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 163 |
struct mlx5_flow_destination dest = {}; |
| 164 |
int err, i; |
| 165 |
|
| 166 |
dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; |
| 167 |
for (i = 0; i < ARFS_NUM_TYPES; i++) { |
| 168 |
dest.ft = arfs->arfs_tables[i].ft.t; |
| 169 |
/* Modify ttc rules destination to point on the aRFS FTs */ |
| 170 |
err = mlx5_ttc_fwd_dest(ttc, arfs_get_tt(i), &dest); |
| 171 |
if (err) { |
| 172 |
fs_err(fs, "%s: modify ttc[%d] dest to arfs, failed err(%d)\n", |
| 173 |
__func__, arfs_get_tt(i), err); |
| 174 |
arfs_disable(fs); |
| 175 |
return err; |
| 176 |
} |
| 177 |
} |
| 178 |
set_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state); |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
static void arfs_destroy_table(struct arfs_table *arfs_t) |
| 184 |
{ |
| 185 |
mlx5_del_flow_rules(arfs_t->default_rule); |
| 186 |
mlx5e_destroy_flow_table(&arfs_t->ft); |
| 187 |
} |
| 188 |
|
| 189 |
static void _mlx5e_cleanup_tables(struct mlx5e_flow_steering *fs) |
| 190 |
{ |
| 191 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 192 |
int i; |
| 193 |
|
| 194 |
arfs_del_rules(fs); |
| 195 |
destroy_workqueue(arfs->wq); |
| 196 |
for (i = 0; i < ARFS_NUM_TYPES; i++) { |
| 197 |
if (!IS_ERR_OR_NULL(arfs->arfs_tables[i].ft.t)) |
| 198 |
arfs_destroy_table(&arfs->arfs_tables[i]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void mlx5e_arfs_destroy_tables(struct mlx5e_flow_steering *fs, bool ntuple) |
| 203 |
{ |
| 204 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 205 |
|
| 206 |
if (!ntuple) |
| 207 |
return; |
| 208 |
|
| 209 |
_mlx5e_cleanup_tables(fs); |
| 210 |
mlx5e_fs_set_arfs(fs, NULL); |
| 211 |
kvfree(arfs); |
| 212 |
} |
| 213 |
|
| 214 |
static int arfs_add_default_rule(struct mlx5e_flow_steering *fs, |
| 215 |
struct mlx5e_rx_res *rx_res, |
| 216 |
enum arfs_type type) |
| 217 |
{ |
| 218 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 219 |
struct arfs_table *arfs_t = &arfs->arfs_tables[type]; |
| 220 |
struct mlx5_flow_destination dest = {}; |
| 221 |
MLX5_DECLARE_FLOW_ACT(flow_act); |
| 222 |
enum mlx5_traffic_types tt; |
| 223 |
int err = 0; |
| 224 |
|
| 225 |
dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR; |
| 226 |
tt = arfs_get_tt(type); |
| 227 |
if (tt == -EINVAL) { |
| 228 |
fs_err(fs, "%s: bad arfs_type: %d\n", __func__, type); |
| 229 |
return -EINVAL; |
| 230 |
} |
| 231 |
|
| 232 |
/* FIXME: Must use mlx5_ttc_get_default_dest(), |
| 233 |
* but can't since TTC default is not setup yet ! |
| 234 |
*/ |
| 235 |
dest.tir_num = mlx5e_rx_res_get_tirn_rss(rx_res, tt); |
| 236 |
arfs_t->default_rule = mlx5_add_flow_rules(arfs_t->ft.t, NULL, |
| 237 |
&flow_act, |
| 238 |
&dest, 1); |
| 239 |
if (IS_ERR(arfs_t->default_rule)) { |
| 240 |
err = PTR_ERR(arfs_t->default_rule); |
| 241 |
arfs_t->default_rule = NULL; |
| 242 |
fs_err(fs, "%s: add rule failed, arfs type=%d\n", __func__, type); |
| 243 |
} |
| 244 |
|
| 245 |
return err; |
| 246 |
} |
| 247 |
|
| 248 |
#define MLX5E_ARFS_NUM_GROUPS 2 |
| 249 |
#define MLX5E_ARFS_GROUP1_SIZE (BIT(16) - 1) |
| 250 |
#define MLX5E_ARFS_GROUP2_SIZE BIT(0) |
| 251 |
#define MLX5E_ARFS_TABLE_SIZE (MLX5E_ARFS_GROUP1_SIZE +\ |
| 252 |
MLX5E_ARFS_GROUP2_SIZE) |
| 253 |
static int arfs_create_groups(struct mlx5e_flow_table *ft, |
| 254 |
enum arfs_type type) |
| 255 |
{ |
| 256 |
int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in); |
| 257 |
void *outer_headers_c; |
| 258 |
int ix = 0; |
| 259 |
u32 *in; |
| 260 |
int err; |
| 261 |
u8 *mc; |
| 262 |
|
| 263 |
ft->g = kzalloc_objs(*ft->g, MLX5E_ARFS_NUM_GROUPS); |
| 264 |
if (!ft->g) |
| 265 |
return -ENOMEM; |
| 266 |
|
| 267 |
in = kvzalloc(inlen, GFP_KERNEL); |
| 268 |
if (!in) { |
| 269 |
err = -ENOMEM; |
| 270 |
goto err_free_g; |
| 271 |
} |
| 272 |
|
| 273 |
mc = MLX5_ADDR_OF(create_flow_group_in, in, match_criteria); |
| 274 |
outer_headers_c = MLX5_ADDR_OF(fte_match_param, mc, |
| 275 |
outer_headers); |
| 276 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, ethertype); |
| 277 |
switch (type) { |
| 278 |
case ARFS_IPV4_TCP: |
| 279 |
case ARFS_IPV6_TCP: |
| 280 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport); |
| 281 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport); |
| 282 |
break; |
| 283 |
case ARFS_IPV4_UDP: |
| 284 |
case ARFS_IPV6_UDP: |
| 285 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, udp_dport); |
| 286 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, udp_sport); |
| 287 |
break; |
| 288 |
default: |
| 289 |
err = -EINVAL; |
| 290 |
goto err_free_in; |
| 291 |
} |
| 292 |
|
| 293 |
switch (type) { |
| 294 |
case ARFS_IPV4_TCP: |
| 295 |
case ARFS_IPV4_UDP: |
| 296 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, |
| 297 |
src_ipv4_src_ipv6.ipv4_layout.ipv4); |
| 298 |
MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, |
| 299 |
dst_ipv4_dst_ipv6.ipv4_layout.ipv4); |
| 300 |
break; |
| 301 |
case ARFS_IPV6_TCP: |
| 302 |
case ARFS_IPV6_UDP: |
| 303 |
memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, |
| 304 |
src_ipv4_src_ipv6.ipv6_layout.ipv6), |
| 305 |
0xff, 16); |
| 306 |
memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, |
| 307 |
dst_ipv4_dst_ipv6.ipv6_layout.ipv6), |
| 308 |
0xff, 16); |
| 309 |
break; |
| 310 |
default: |
| 311 |
err = -EINVAL; |
| 312 |
goto err_free_in; |
| 313 |
} |
| 314 |
|
| 315 |
MLX5_SET_CFG(in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS); |
| 316 |
MLX5_SET_CFG(in, start_flow_index, ix); |
| 317 |
ix += MLX5E_ARFS_GROUP1_SIZE; |
| 318 |
MLX5_SET_CFG(in, end_flow_index, ix - 1); |
| 319 |
ft->g[ft->num_groups] = mlx5_create_flow_group(ft->t, in); |
| 320 |
if (IS_ERR(ft->g[ft->num_groups])) |
| 321 |
goto err_clean_group; |
| 322 |
ft->num_groups++; |
| 323 |
|
| 324 |
memset(in, 0, inlen); |
| 325 |
MLX5_SET_CFG(in, start_flow_index, ix); |
| 326 |
ix += MLX5E_ARFS_GROUP2_SIZE; |
| 327 |
MLX5_SET_CFG(in, end_flow_index, ix - 1); |
| 328 |
ft->g[ft->num_groups] = mlx5_create_flow_group(ft->t, in); |
| 329 |
if (IS_ERR(ft->g[ft->num_groups])) |
| 330 |
goto err_clean_group; |
| 331 |
ft->num_groups++; |
| 332 |
|
| 333 |
kvfree(in); |
| 334 |
return 0; |
| 335 |
|
| 336 |
err_clean_group: |
| 337 |
err = PTR_ERR(ft->g[ft->num_groups]); |
| 338 |
ft->g[ft->num_groups] = NULL; |
| 339 |
err_free_in: |
| 340 |
kvfree(in); |
| 341 |
err_free_g: |
| 342 |
kfree(ft->g); |
| 343 |
ft->g = NULL; |
| 344 |
return err; |
| 345 |
} |
| 346 |
|
| 347 |
static int arfs_create_table(struct mlx5e_flow_steering *fs, |
| 348 |
struct mlx5e_rx_res *rx_res, |
| 349 |
enum arfs_type type) |
| 350 |
{ |
| 351 |
struct mlx5_flow_namespace *ns = mlx5e_fs_get_ns(fs, false); |
| 352 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 353 |
struct mlx5e_flow_table *ft = &arfs->arfs_tables[type].ft; |
| 354 |
struct mlx5_flow_table_attr ft_attr = {}; |
| 355 |
int err; |
| 356 |
|
| 357 |
ft->num_groups = 0; |
| 358 |
|
| 359 |
ft_attr.max_fte = MLX5E_ARFS_TABLE_SIZE; |
| 360 |
ft_attr.level = MLX5E_ARFS_FT_LEVEL; |
| 361 |
ft_attr.prio = MLX5E_NIC_PRIO; |
| 362 |
|
| 363 |
ft->t = mlx5_create_flow_table(ns, &ft_attr); |
| 364 |
if (IS_ERR(ft->t)) { |
| 365 |
err = PTR_ERR(ft->t); |
| 366 |
ft->t = NULL; |
| 367 |
return err; |
| 368 |
} |
| 369 |
|
| 370 |
err = arfs_create_groups(ft, type); |
| 371 |
if (err) |
| 372 |
goto err; |
| 373 |
|
| 374 |
err = arfs_add_default_rule(fs, rx_res, type); |
| 375 |
if (err) |
| 376 |
goto err; |
| 377 |
|
| 378 |
return 0; |
| 379 |
err: |
| 380 |
mlx5e_destroy_flow_table(ft); |
| 381 |
return err; |
| 382 |
} |
| 383 |
|
| 384 |
int mlx5e_arfs_create_tables(struct mlx5e_flow_steering *fs, |
| 385 |
struct mlx5e_rx_res *rx_res, bool ntuple) |
| 386 |
{ |
| 387 |
struct mlx5e_arfs_tables *arfs; |
| 388 |
int err = -ENOMEM; |
| 389 |
int i; |
| 390 |
|
| 391 |
if (!ntuple) |
| 392 |
return 0; |
| 393 |
|
| 394 |
arfs = kvzalloc_obj(*arfs); |
| 395 |
if (!arfs) |
| 396 |
return -ENOMEM; |
| 397 |
|
| 398 |
spin_lock_init(&arfs->arfs_lock); |
| 399 |
arfs->wq = create_singlethread_workqueue("mlx5e_arfs"); |
| 400 |
if (!arfs->wq) |
| 401 |
goto err; |
| 402 |
|
| 403 |
mlx5e_fs_set_arfs(fs, arfs); |
| 404 |
|
| 405 |
for (i = 0; i < ARFS_NUM_TYPES; i++) { |
| 406 |
err = arfs_create_table(fs, rx_res, i); |
| 407 |
if (err) |
| 408 |
goto err_des; |
| 409 |
} |
| 410 |
return 0; |
| 411 |
|
| 412 |
err_des: |
| 413 |
_mlx5e_cleanup_tables(fs); |
| 414 |
err: |
| 415 |
mlx5e_fs_set_arfs(fs, NULL); |
| 416 |
kvfree(arfs); |
| 417 |
return err; |
| 418 |
} |
| 419 |
|
| 420 |
#define MLX5E_ARFS_EXPIRY_QUOTA 60 |
| 421 |
|
| 422 |
static void arfs_may_expire_flow(struct mlx5e_priv *priv) |
| 423 |
{ |
| 424 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs); |
| 425 |
struct arfs_rule *arfs_rule; |
| 426 |
struct hlist_node *htmp; |
| 427 |
HLIST_HEAD(del_list); |
| 428 |
int quota = 0; |
| 429 |
int i; |
| 430 |
int j; |
| 431 |
|
| 432 |
spin_lock_bh(&arfs->arfs_lock); |
| 433 |
mlx5e_for_each_arfs_rule(arfs_rule, htmp, arfs->arfs_tables, i, j) { |
| 434 |
if (!work_pending(&arfs_rule->arfs_work) && |
| 435 |
rps_may_expire_flow(priv->netdev, |
| 436 |
arfs_rule->rxq, arfs_rule->flow_id, |
| 437 |
arfs_rule->filter_id)) { |
| 438 |
hlist_del_init(&arfs_rule->hlist); |
| 439 |
hlist_add_head(&arfs_rule->hlist, &del_list); |
| 440 |
if (quota++ > MLX5E_ARFS_EXPIRY_QUOTA) |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
spin_unlock_bh(&arfs->arfs_lock); |
| 445 |
hlist_for_each_entry_safe(arfs_rule, htmp, &del_list, hlist) { |
| 446 |
if (arfs_rule->rule) { |
| 447 |
mlx5_del_flow_rules(arfs_rule->rule); |
| 448 |
priv->channel_stats[arfs_rule->rxq]->rq.arfs_expired++; |
| 449 |
} |
| 450 |
hlist_del(&arfs_rule->hlist); |
| 451 |
kfree(arfs_rule); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
static void arfs_del_rules(struct mlx5e_flow_steering *fs) |
| 456 |
{ |
| 457 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs); |
| 458 |
struct hlist_node *htmp; |
| 459 |
struct arfs_rule *rule; |
| 460 |
HLIST_HEAD(del_list); |
| 461 |
int i; |
| 462 |
int j; |
| 463 |
|
| 464 |
clear_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state); |
| 465 |
|
| 466 |
spin_lock_bh(&arfs->arfs_lock); |
| 467 |
mlx5e_for_each_arfs_rule(rule, htmp, arfs->arfs_tables, i, j) { |
| 468 |
hlist_del_init(&rule->hlist); |
| 469 |
hlist_add_head(&rule->hlist, &del_list); |
| 470 |
} |
| 471 |
spin_unlock_bh(&arfs->arfs_lock); |
| 472 |
|
| 473 |
hlist_for_each_entry_safe(rule, htmp, &del_list, hlist) { |
| 474 |
cancel_work_sync(&rule->arfs_work); |
| 475 |
if (rule->rule) |
| 476 |
mlx5_del_flow_rules(rule->rule); |
| 477 |
hlist_del(&rule->hlist); |
| 478 |
kfree(rule); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
static struct hlist_head * |
| 483 |
arfs_hash_bucket(struct arfs_table *arfs_t, __be16 src_port, |
| 484 |
__be16 dst_port) |
| 485 |
{ |
| 486 |
unsigned long l; |
| 487 |
int bucket_idx; |
| 488 |
|
| 489 |
l = (__force unsigned long)src_port | |
| 490 |
((__force unsigned long)dst_port << 2); |
| 491 |
|
| 492 |
bucket_idx = hash_long(l, ARFS_HASH_SHIFT); |
| 493 |
|
| 494 |
return &arfs_t->rules_hash[bucket_idx]; |
| 495 |
} |
| 496 |
|
| 497 |
static struct arfs_table *arfs_get_table(struct mlx5e_arfs_tables *arfs, |
| 498 |
u8 ip_proto, __be16 etype) |
| 499 |
{ |
| 500 |
if (etype == htons(ETH_P_IP) && ip_proto == IPPROTO_TCP) |
| 501 |
return &arfs->arfs_tables[ARFS_IPV4_TCP]; |
| 502 |
if (etype == htons(ETH_P_IP) && ip_proto == IPPROTO_UDP) |
| 503 |
return &arfs->arfs_tables[ARFS_IPV4_UDP]; |
| 504 |
if (etype == htons(ETH_P_IPV6) && ip_proto == IPPROTO_TCP) |
| 505 |
return &arfs->arfs_tables[ARFS_IPV6_TCP]; |
| 506 |
if (etype == htons(ETH_P_IPV6) && ip_proto == IPPROTO_UDP) |
| 507 |
return &arfs->arfs_tables[ARFS_IPV6_UDP]; |
| 508 |
|
| 509 |
return NULL; |
| 510 |
} |
| 511 |
|
| 512 |
static struct mlx5_flow_handle *arfs_add_rule(struct mlx5e_priv *priv, |
| 513 |
struct arfs_rule *arfs_rule) |
| 514 |
{ |
| 515 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs); |
| 516 |
struct arfs_tuple *tuple = &arfs_rule->tuple; |
| 517 |
struct mlx5_flow_handle *rule = NULL; |
| 518 |
struct mlx5_flow_destination dest = {}; |
| 519 |
MLX5_DECLARE_FLOW_ACT(flow_act); |
| 520 |
struct arfs_table *arfs_table; |
| 521 |
struct mlx5_flow_spec *spec; |
| 522 |
struct mlx5_flow_table *ft; |
| 523 |
int err = 0; |
| 524 |
|
| 525 |
spec = kvzalloc_obj(*spec); |
| 526 |
if (!spec) { |
| 527 |
priv->channel_stats[arfs_rule->rxq]->rq.arfs_err++; |
| 528 |
err = -ENOMEM; |
| 529 |
goto out; |
| 530 |
} |
| 531 |
spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS; |
| 532 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 533 |
outer_headers.ethertype); |
| 534 |
MLX5_SET(fte_match_param, spec->match_value, outer_headers.ethertype, |
| 535 |
ntohs(tuple->etype)); |
| 536 |
arfs_table = arfs_get_table(arfs, tuple->ip_proto, tuple->etype); |
| 537 |
if (!arfs_table) { |
| 538 |
WARN_ONCE(1, "arfs table does not exist for etype %u and ip_proto %u\n", |
| 539 |
tuple->etype, tuple->ip_proto); |
| 540 |
err = -EINVAL; |
| 541 |
goto out; |
| 542 |
} |
| 543 |
|
| 544 |
ft = arfs_table->ft.t; |
| 545 |
if (tuple->ip_proto == IPPROTO_TCP) { |
| 546 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 547 |
outer_headers.tcp_dport); |
| 548 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 549 |
outer_headers.tcp_sport); |
| 550 |
MLX5_SET(fte_match_param, spec->match_value, outer_headers.tcp_dport, |
| 551 |
ntohs(tuple->dst_port)); |
| 552 |
MLX5_SET(fte_match_param, spec->match_value, outer_headers.tcp_sport, |
| 553 |
ntohs(tuple->src_port)); |
| 554 |
} else { |
| 555 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 556 |
outer_headers.udp_dport); |
| 557 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 558 |
outer_headers.udp_sport); |
| 559 |
MLX5_SET(fte_match_param, spec->match_value, outer_headers.udp_dport, |
| 560 |
ntohs(tuple->dst_port)); |
| 561 |
MLX5_SET(fte_match_param, spec->match_value, outer_headers.udp_sport, |
| 562 |
ntohs(tuple->src_port)); |
| 563 |
} |
| 564 |
if (tuple->etype == htons(ETH_P_IP)) { |
| 565 |
memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value, |
| 566 |
outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4), |
| 567 |
&tuple->src_ipv4, |
| 568 |
4); |
| 569 |
memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value, |
| 570 |
outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4), |
| 571 |
&tuple->dst_ipv4, |
| 572 |
4); |
| 573 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 574 |
outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4); |
| 575 |
MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, |
| 576 |
outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4); |
| 577 |
} else { |
| 578 |
memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value, |
| 579 |
outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), |
| 580 |
&tuple->src_ipv6, |
| 581 |
16); |
| 582 |
memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value, |
| 583 |
outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), |
| 584 |
&tuple->dst_ipv6, |
| 585 |
16); |
| 586 |
memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria, |
| 587 |
outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), |
| 588 |
0xff, |
| 589 |
16); |
| 590 |
memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria, |
| 591 |
outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), |
| 592 |
0xff, |
| 593 |
16); |
| 594 |
} |
| 595 |
dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR; |
| 596 |
dest.tir_num = mlx5e_rx_res_get_tirn_direct(priv->rx_res, arfs_rule->rxq); |
| 597 |
rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1); |
| 598 |
if (IS_ERR(rule)) { |
| 599 |
err = PTR_ERR(rule); |
| 600 |
priv->channel_stats[arfs_rule->rxq]->rq.arfs_err++; |
| 601 |
netdev_dbg(priv->netdev, |
| 602 |
"%s: add rule(filter id=%d, rq idx=%d, ip proto=0x%x) failed,err=%d\n", |
| 603 |
__func__, arfs_rule->filter_id, arfs_rule->rxq, |
| 604 |
tuple->ip_proto, err); |
| 605 |
} |
| 606 |
|
| 607 |
out: |
| 608 |
kvfree(spec); |
| 609 |
return err ? ERR_PTR(err) : rule; |
| 610 |
} |
| 611 |
|
| 612 |
static void arfs_modify_rule_rq(struct mlx5e_priv *priv, |
| 613 |
struct mlx5_flow_handle *rule, u16 rxq) |
| 614 |
{ |
| 615 |
struct mlx5_flow_destination dst = {}; |
| 616 |
int err = 0; |
| 617 |
|
| 618 |
dst.type = MLX5_FLOW_DESTINATION_TYPE_TIR; |
| 619 |
dst.tir_num = mlx5e_rx_res_get_tirn_direct(priv->rx_res, rxq); |
| 620 |
err = mlx5_modify_rule_destination(rule, &dst, NULL); |
| 621 |
if (err) { |
| 622 |
priv->channel_stats[rxq]->rq.arfs_err++; |
| 623 |
netdev_warn(priv->netdev, |
| 624 |
"Failed to modify aRFS rule destination to rq=%d\n", rxq); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
static void arfs_handle_work(struct work_struct *work) |
| 629 |
{ |
| 630 |
struct arfs_rule *arfs_rule = container_of(work, |
| 631 |
struct arfs_rule, |
| 632 |
arfs_work); |
| 633 |
struct mlx5e_priv *priv = arfs_rule->priv; |
| 634 |
struct mlx5e_arfs_tables *arfs; |
| 635 |
struct mlx5_flow_handle *rule; |
| 636 |
|
| 637 |
arfs = mlx5e_fs_get_arfs(priv->fs); |
| 638 |
if (!test_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state)) |
| 639 |
return; |
| 640 |
|
| 641 |
if (!arfs_rule->rule) { |
| 642 |
rule = arfs_add_rule(priv, arfs_rule); |
| 643 |
if (IS_ERR(rule)) |
| 644 |
goto out; |
| 645 |
arfs_rule->rule = rule; |
| 646 |
priv->channel_stats[arfs_rule->rxq]->rq.arfs_add++; |
| 647 |
} else { |
| 648 |
arfs_modify_rule_rq(priv, arfs_rule->rule, |
| 649 |
arfs_rule->rxq); |
| 650 |
} |
| 651 |
out: |
| 652 |
arfs_may_expire_flow(priv); |
| 653 |
} |
| 654 |
|
| 655 |
static struct arfs_rule *arfs_alloc_rule(struct mlx5e_priv *priv, |
| 656 |
struct arfs_table *arfs_t, |
| 657 |
const struct flow_keys *fk, |
| 658 |
u16 rxq, u32 flow_id) |
| 659 |
{ |
| 660 |
struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs); |
| 661 |
struct arfs_rule *rule; |
| 662 |
struct arfs_tuple *tuple; |
| 663 |
|
| 664 |
rule = kzalloc_obj(*rule, GFP_ATOMIC); |
| 665 |
if (!rule) { |
| 666 |
priv->channel_stats[rxq]->rq.arfs_err++; |
| 667 |
return NULL; |
| 668 |
} |
| 669 |
|
| 670 |
rule->priv = priv; |
| 671 |
rule->rxq = rxq; |
| 672 |
INIT_WORK(&rule->arfs_work, arfs_handle_work); |
| 673 |
|
| 674 |
tuple = &rule->tuple; |
| 675 |
tuple->etype = fk->basic.n_proto; |
| 676 |
tuple->ip_proto = fk->basic.ip_proto; |
| 677 |
if (tuple->etype == htons(ETH_P_IP)) { |
| 678 |
tuple->src_ipv4 = fk->addrs.v4addrs.src; |
| 679 |
tuple->dst_ipv4 = fk->addrs.v4addrs.dst; |
| 680 |
} else { |
| 681 |
memcpy(&tuple->src_ipv6, &fk->addrs.v6addrs.src, |
| 682 |
sizeof(struct in6_addr)); |
| 683 |
memcpy(&tuple->dst_ipv6, &fk->addrs.v6addrs.dst, |
| 684 |
sizeof(struct in6_addr)); |
| 685 |
} |
| 686 |
tuple->src_port = fk->ports.src; |
| 687 |
tuple->dst_port = fk->ports.dst; |
| 688 |
|
| 689 |
rule->flow_id = flow_id; |
| 690 |
rule->filter_id = arfs->last_filter_id++ % RPS_NO_FILTER; |
| 691 |
|
| 692 |
hlist_add_head(&rule->hlist, |
| 693 |
arfs_hash_bucket(arfs_t, tuple->src_port, |
| 694 |
tuple->dst_port)); |
| 695 |
return rule; |
| 696 |
} |
| 697 |
|
| 698 |
static bool arfs_cmp(const struct arfs_tuple *tuple, const struct flow_keys *fk) |
| 699 |
{ |
| 700 |
if (tuple->src_port != fk->ports.src || tuple->dst_port != fk->ports.dst) |
| 701 |
return false; |
| 702 |
if (tuple->etype != fk->basic.n_proto) |
| 703 |
return false; |
| 704 |
if (tuple->etype == htons(ETH_P_IP)) |
| 705 |
return tuple->src_ipv4 == fk->addrs.v4addrs.src && |
| 706 |
tuple->dst_ipv4 == fk->addrs.v4addrs.dst; |
| 707 |
if (tuple->etype == htons(ETH_P_IPV6)) |
| 708 |
return !memcmp(&tuple->src_ipv6, &fk->addrs.v6addrs.src, |
| 709 |
sizeof(struct in6_addr)) && |
| 710 |
!memcmp(&tuple->dst_ipv6, &fk->addrs.v6addrs.dst, |
| 711 |
sizeof(struct in6_addr)); |
| 712 |
return false; |
| 713 |
} |
| 714 |
|
| 715 |
static struct arfs_rule *arfs_find_rule(struct arfs_table *arfs_t, |
| 716 |
const struct flow_keys *fk) |
| 717 |
{ |
| 718 |
struct arfs_rule *arfs_rule; |
| 719 |
struct hlist_head *head; |
| 720 |
|
| 721 |
head = arfs_hash_bucket(arfs_t, fk->ports.src, fk->ports.dst); |
| 722 |
hlist_for_each_entry(arfs_rule, head, hlist) { |
| 723 |
if (arfs_cmp(&arfs_rule->tuple, fk)) |
| 724 |
return arfs_rule; |
| 725 |
} |
| 726 |
|
| 727 |
return NULL; |
| 728 |
} |
| 729 |
|
| 730 |
int mlx5e_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, |
| 731 |
u16 rxq_index, u32 flow_id) |
| 732 |
{ |
| 733 |
struct mlx5e_priv *priv = netdev_priv(dev); |
| 734 |
struct mlx5e_arfs_tables *arfs; |
| 735 |
struct arfs_rule *arfs_rule; |
| 736 |
struct arfs_table *arfs_t; |
| 737 |
struct flow_keys fk; |
| 738 |
|
| 739 |
arfs = mlx5e_fs_get_arfs(priv->fs); |
| 740 |
if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) |
| 741 |
return -EPROTONOSUPPORT; |
| 742 |
|
| 743 |
if (fk.basic.n_proto != htons(ETH_P_IP) && |
| 744 |
fk.basic.n_proto != htons(ETH_P_IPV6)) |
| 745 |
return -EPROTONOSUPPORT; |
| 746 |
|
| 747 |
if (skb->encapsulation) |
| 748 |
return -EPROTONOSUPPORT; |
| 749 |
|
| 750 |
arfs_t = arfs_get_table(arfs, fk.basic.ip_proto, fk.basic.n_proto); |
| 751 |
if (!arfs_t) |
| 752 |
return -EPROTONOSUPPORT; |
| 753 |
|
| 754 |
spin_lock_bh(&arfs->arfs_lock); |
| 755 |
if (!test_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state)) { |
| 756 |
spin_unlock_bh(&arfs->arfs_lock); |
| 757 |
return -EPERM; |
| 758 |
} |
| 759 |
|
| 760 |
arfs_rule = arfs_find_rule(arfs_t, &fk); |
| 761 |
if (arfs_rule) { |
| 762 |
if (arfs_rule->rxq == rxq_index || work_busy(&arfs_rule->arfs_work)) { |
| 763 |
spin_unlock_bh(&arfs->arfs_lock); |
| 764 |
return arfs_rule->filter_id; |
| 765 |
} |
| 766 |
|
| 767 |
priv->channel_stats[rxq_index]->rq.arfs_request_in++; |
| 768 |
priv->channel_stats[arfs_rule->rxq]->rq.arfs_request_out++; |
| 769 |
arfs_rule->rxq = rxq_index; |
| 770 |
} else { |
| 771 |
arfs_rule = arfs_alloc_rule(priv, arfs_t, &fk, rxq_index, flow_id); |
| 772 |
if (!arfs_rule) { |
| 773 |
spin_unlock_bh(&arfs->arfs_lock); |
| 774 |
return -ENOMEM; |
| 775 |
} |
| 776 |
} |
| 777 |
queue_work(arfs->wq, &arfs_rule->arfs_work); |
| 778 |
spin_unlock_bh(&arfs->arfs_lock); |
| 779 |
return arfs_rule->filter_id; |
| 780 |
} |
| 781 |
|