metadata.c 19.4 KB
Newer Older
1 2
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 4 5 6

#include <linux/hash.h>
#include <linux/hashtable.h>
#include <linux/jhash.h>
7
#include <linux/math64.h>
8 9 10 11
#include <linux/vmalloc.h>
#include <net/pkt_cls.h>

#include "cmsg.h"
L
Louis Peens 已提交
12
#include "conntrack.h"
13 14 15 16 17 18 19 20 21 22
#include "main.h"
#include "../nfp_app.h"

struct nfp_mask_id_table {
	struct hlist_node link;
	u32 hash_key;
	u32 ref_cnt;
	u8 mask_id;
};

23 24 25 26 27
struct nfp_fl_flow_table_cmp_arg {
	struct net_device *netdev;
	unsigned long cookie;
};

28 29 30 31 32 33 34 35 36 37 38 39
struct nfp_fl_stats_ctx_to_flow {
	struct rhash_head ht_node;
	u32 stats_cxt;
	struct nfp_fl_payload *flow;
};

static const struct rhashtable_params stats_ctx_table_params = {
	.key_offset	= offsetof(struct nfp_fl_stats_ctx_to_flow, stats_cxt),
	.head_offset	= offsetof(struct nfp_fl_stats_ctx_to_flow, ht_node),
	.key_len	= sizeof(u32),
};

40 41 42 43 44 45 46
static int nfp_release_stats_entry(struct nfp_app *app, u32 stats_context_id)
{
	struct nfp_flower_priv *priv = app->priv;
	struct circ_buf *ring;

	ring = &priv->stats_ids.free_list;
	/* Check if buffer is full. */
47 48
	if (!CIRC_SPACE(ring->head, ring->tail,
			priv->stats_ring_size * NFP_FL_STATS_ELEM_RS -
49 50 51 52 53
			NFP_FL_STATS_ELEM_RS + 1))
		return -ENOBUFS;

	memcpy(&ring->buf[ring->head], &stats_context_id, NFP_FL_STATS_ELEM_RS);
	ring->head = (ring->head + NFP_FL_STATS_ELEM_RS) %
54
		     (priv->stats_ring_size * NFP_FL_STATS_ELEM_RS);
55 56 57 58 59 60 61 62 63 64 65

	return 0;
}

static int nfp_get_stats_entry(struct nfp_app *app, u32 *stats_context_id)
{
	struct nfp_flower_priv *priv = app->priv;
	u32 freed_stats_id, temp_stats_id;
	struct circ_buf *ring;

	ring = &priv->stats_ids.free_list;
66
	freed_stats_id = priv->stats_ring_size;
67 68
	/* Check for unallocated entries first. */
	if (priv->stats_ids.init_unalloc > 0) {
69 70 71 72 73
		*stats_context_id =
			FIELD_PREP(NFP_FL_STAT_ID_STAT,
				   priv->stats_ids.init_unalloc - 1) |
			FIELD_PREP(NFP_FL_STAT_ID_MU_NUM,
				   priv->active_mem_unit);
J
John Hurley 已提交
74 75 76 77 78 79

		if (++priv->active_mem_unit == priv->total_mem_units) {
			priv->stats_ids.init_unalloc--;
			priv->active_mem_unit = 0;
		}

80 81 82 83 84 85 86 87 88 89 90 91 92
		return 0;
	}

	/* Check if buffer is empty. */
	if (ring->head == ring->tail) {
		*stats_context_id = freed_stats_id;
		return -ENOENT;
	}

	memcpy(&temp_stats_id, &ring->buf[ring->tail], NFP_FL_STATS_ELEM_RS);
	*stats_context_id = temp_stats_id;
	memcpy(&ring->buf[ring->tail], &freed_stats_id, NFP_FL_STATS_ELEM_RS);
	ring->tail = (ring->tail + NFP_FL_STATS_ELEM_RS) %
93
		     (priv->stats_ring_size * NFP_FL_STATS_ELEM_RS);
94 95 96 97

	return 0;
}

98 99
/* Must be called with either RTNL or rcu_read_lock */
struct nfp_fl_payload *
100
nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
101
			   struct net_device *netdev)
102
{
103
	struct nfp_fl_flow_table_cmp_arg flower_cmp_arg;
104 105
	struct nfp_flower_priv *priv = app->priv;

106 107
	flower_cmp_arg.netdev = netdev;
	flower_cmp_arg.cookie = tc_flower_cookie;
108

109 110
	return rhashtable_lookup_fast(&priv->flow_table, &flower_cmp_arg,
				      nfp_flower_table_params);
111 112
}

113 114
void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb)
{
115
	unsigned int msg_len = nfp_flower_cmsg_get_data_len(skb);
116 117
	struct nfp_flower_priv *priv = app->priv;
	struct nfp_fl_stats_frame *stats;
118
	unsigned char *msg;
119
	u32 ctx_id;
120 121 122 123
	int i;

	msg = nfp_flower_cmsg_get_data(skb);

124 125 126 127 128 129 130 131 132
	spin_lock(&priv->stats_lock);
	for (i = 0; i < msg_len / sizeof(*stats); i++) {
		stats = (struct nfp_fl_stats_frame *)msg + i;
		ctx_id = be32_to_cpu(stats->stats_con_id);
		priv->stats[ctx_id].pkts += be32_to_cpu(stats->pkt_count);
		priv->stats[ctx_id].bytes += be64_to_cpu(stats->byte_count);
		priv->stats[ctx_id].used = jiffies;
	}
	spin_unlock(&priv->stats_lock);
133 134
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148
static int nfp_release_mask_id(struct nfp_app *app, u8 mask_id)
{
	struct nfp_flower_priv *priv = app->priv;
	struct circ_buf *ring;

	ring = &priv->mask_ids.mask_id_free_list;
	/* Checking if buffer is full. */
	if (CIRC_SPACE(ring->head, ring->tail, NFP_FLOWER_MASK_ENTRY_RS) == 0)
		return -ENOBUFS;

	memcpy(&ring->buf[ring->head], &mask_id, NFP_FLOWER_MASK_ELEMENT_RS);
	ring->head = (ring->head + NFP_FLOWER_MASK_ELEMENT_RS) %
		     (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS);

149
	priv->mask_ids.last_used[mask_id] = ktime_get();
150 151 152 153 154 155 156

	return 0;
}

static int nfp_mask_alloc(struct nfp_app *app, u8 *mask_id)
{
	struct nfp_flower_priv *priv = app->priv;
157
	ktime_t reuse_timeout;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	struct circ_buf *ring;
	u8 temp_id, freed_id;

	ring = &priv->mask_ids.mask_id_free_list;
	freed_id = NFP_FLOWER_MASK_ENTRY_RS - 1;
	/* Checking for unallocated entries first. */
	if (priv->mask_ids.init_unallocated > 0) {
		*mask_id = priv->mask_ids.init_unallocated;
		priv->mask_ids.init_unallocated--;
		return 0;
	}

	/* Checking if buffer is empty. */
	if (ring->head == ring->tail)
		goto err_not_found;

	memcpy(&temp_id, &ring->buf[ring->tail], NFP_FLOWER_MASK_ELEMENT_RS);
	*mask_id = temp_id;

177 178
	reuse_timeout = ktime_add_ns(priv->mask_ids.last_used[*mask_id],
				     NFP_FL_MASK_REUSE_TIME_NS);
179

180
	if (ktime_before(ktime_get(), reuse_timeout))
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		goto err_not_found;

	memcpy(&ring->buf[ring->tail], &freed_id, NFP_FLOWER_MASK_ELEMENT_RS);
	ring->tail = (ring->tail + NFP_FLOWER_MASK_ELEMENT_RS) %
		     (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS);

	return 0;

err_not_found:
	*mask_id = freed_id;
	return -ENOENT;
}

static int
nfp_add_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
{
	struct nfp_flower_priv *priv = app->priv;
	struct nfp_mask_id_table *mask_entry;
	unsigned long hash_key;
	u8 mask_id;

	if (nfp_mask_alloc(app, &mask_id))
		return -ENOENT;

	mask_entry = kmalloc(sizeof(*mask_entry), GFP_KERNEL);
	if (!mask_entry) {
		nfp_release_mask_id(app, mask_id);
		return -ENOMEM;
	}

	INIT_HLIST_NODE(&mask_entry->link);
	mask_entry->mask_id = mask_id;
	hash_key = jhash(mask_data, mask_len, priv->mask_id_seed);
	mask_entry->hash_key = hash_key;
	mask_entry->ref_cnt = 1;
	hash_add(priv->mask_table, &mask_entry->link, hash_key);

	return mask_id;
}

static struct nfp_mask_id_table *
nfp_search_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
{
	struct nfp_flower_priv *priv = app->priv;
	struct nfp_mask_id_table *mask_entry;
	unsigned long hash_key;

	hash_key = jhash(mask_data, mask_len, priv->mask_id_seed);

	hash_for_each_possible(priv->mask_table, mask_entry, link, hash_key)
		if (mask_entry->hash_key == hash_key)
			return mask_entry;

	return NULL;
}

static int
nfp_find_in_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
{
	struct nfp_mask_id_table *mask_entry;

	mask_entry = nfp_search_mask_table(app, mask_data, mask_len);
	if (!mask_entry)
		return -ENOENT;

	mask_entry->ref_cnt++;

	/* Casting u8 to int for later use. */
	return mask_entry->mask_id;
}

static bool
nfp_check_mask_add(struct nfp_app *app, char *mask_data, u32 mask_len,
		   u8 *meta_flags, u8 *mask_id)
{
	int id;

	id = nfp_find_in_mask_table(app, mask_data, mask_len);
	if (id < 0) {
		id = nfp_add_mask_table(app, mask_data, mask_len);
		if (id < 0)
			return false;
263
		*meta_flags |= NFP_FL_META_FLAG_MANAGE_MASK;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	}
	*mask_id = id;

	return true;
}

static bool
nfp_check_mask_remove(struct nfp_app *app, char *mask_data, u32 mask_len,
		      u8 *meta_flags, u8 *mask_id)
{
	struct nfp_mask_id_table *mask_entry;

	mask_entry = nfp_search_mask_table(app, mask_data, mask_len);
	if (!mask_entry)
		return false;

	*mask_id = mask_entry->mask_id;
	mask_entry->ref_cnt--;
	if (!mask_entry->ref_cnt) {
		hash_del(&mask_entry->link);
		nfp_release_mask_id(app, *mask_id);
		kfree(mask_entry);
		if (meta_flags)
287
			*meta_flags |= NFP_FL_META_FLAG_MANAGE_MASK;
288 289 290 291 292 293
	}

	return true;
}

int nfp_compile_flow_metadata(struct nfp_app *app,
294
			      struct flow_cls_offload *flow,
295
			      struct nfp_fl_payload *nfp_flow,
296 297
			      struct net_device *netdev,
			      struct netlink_ext_ack *extack)
298
{
299
	struct nfp_fl_stats_ctx_to_flow *ctx_entry;
300 301 302
	struct nfp_flower_priv *priv = app->priv;
	struct nfp_fl_payload *check_entry;
	u8 new_mask_id;
303
	u32 stats_cxt;
304
	int err;
305

306
	err = nfp_get_stats_entry(app, &stats_cxt);
307 308
	if (err) {
		NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot allocate new stats context");
309
		return err;
310
	}
311 312 313

	nfp_flow->meta.host_ctx_id = cpu_to_be32(stats_cxt);
	nfp_flow->meta.host_cookie = cpu_to_be64(flow->cookie);
314
	nfp_flow->ingress_dev = netdev;
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	ctx_entry = kzalloc(sizeof(*ctx_entry), GFP_KERNEL);
	if (!ctx_entry) {
		err = -ENOMEM;
		goto err_release_stats;
	}

	ctx_entry->stats_cxt = stats_cxt;
	ctx_entry->flow = nfp_flow;

	if (rhashtable_insert_fast(&priv->stats_ctx_table, &ctx_entry->ht_node,
				   stats_ctx_table_params)) {
		err = -ENOMEM;
		goto err_free_ctx_entry;
	}

331 332 333 334 335
	/* Do net allocate a mask-id for pre_tun_rules. These flows are used to
	 * configure the pre_tun table and are never actually send to the
	 * firmware as an add-flow message. This causes the mask-id allocation
	 * on the firmware to get out of sync if allocated here.
	 */
336
	new_mask_id = 0;
337 338
	if (!nfp_flow->pre_tun_rule.dev &&
	    !nfp_check_mask_add(app, nfp_flow->mask_data,
339
				nfp_flow->meta.mask_len,
340
				&nfp_flow->meta.flags, &new_mask_id)) {
341 342 343 344 345 346
		NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot allocate a new mask id");
		if (nfp_release_stats_entry(app, stats_cxt)) {
			NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot release stats context");
			err = -EINVAL;
			goto err_remove_rhash;
		}
347 348
		err = -ENOENT;
		goto err_remove_rhash;
349
	}
350 351 352 353 354 355

	nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version);
	priv->flower_version++;

	/* Update flow payload with mask ids. */
	nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id;
356 357 358
	priv->stats[stats_cxt].pkts = 0;
	priv->stats[stats_cxt].bytes = 0;
	priv->stats[stats_cxt].used = jiffies;
359

360
	check_entry = nfp_flower_search_fl_table(app, flow->cookie, netdev);
361
	if (check_entry) {
362 363 364 365 366 367 368
		NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot offload duplicate flow entry");
		if (nfp_release_stats_entry(app, stats_cxt)) {
			NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot release stats context");
			err = -EINVAL;
			goto err_remove_mask;
		}

369 370
		if (!nfp_flow->pre_tun_rule.dev &&
		    !nfp_check_mask_remove(app, nfp_flow->mask_data,
371 372 373 374 375 376 377
					   nfp_flow->meta.mask_len,
					   NULL, &new_mask_id)) {
			NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot release mask id");
			err = -EINVAL;
			goto err_remove_mask;
		}

378 379
		err = -EEXIST;
		goto err_remove_mask;
380 381 382
	}

	return 0;
383 384

err_remove_mask:
385 386 387 388
	if (!nfp_flow->pre_tun_rule.dev)
		nfp_check_mask_remove(app, nfp_flow->mask_data,
				      nfp_flow->meta.mask_len,
				      NULL, &new_mask_id);
389 390 391 392 393 394 395 396 397 398
err_remove_rhash:
	WARN_ON_ONCE(rhashtable_remove_fast(&priv->stats_ctx_table,
					    &ctx_entry->ht_node,
					    stats_ctx_table_params));
err_free_ctx_entry:
	kfree(ctx_entry);
err_release_stats:
	nfp_release_stats_entry(app, stats_cxt);

	return err;
399 400
}

J
John Hurley 已提交
401 402 403 404 405 406 407 408
void __nfp_modify_flow_metadata(struct nfp_flower_priv *priv,
				struct nfp_fl_payload *nfp_flow)
{
	nfp_flow->meta.flags &= ~NFP_FL_META_FLAG_MANAGE_MASK;
	nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version);
	priv->flower_version++;
}

409 410 411
int nfp_modify_flow_metadata(struct nfp_app *app,
			     struct nfp_fl_payload *nfp_flow)
{
412
	struct nfp_fl_stats_ctx_to_flow *ctx_entry;
413 414
	struct nfp_flower_priv *priv = app->priv;
	u8 new_mask_id = 0;
415
	u32 temp_ctx_id;
416

J
John Hurley 已提交
417 418
	__nfp_modify_flow_metadata(priv, nfp_flow);

419 420 421 422
	if (!nfp_flow->pre_tun_rule.dev)
		nfp_check_mask_remove(app, nfp_flow->mask_data,
				      nfp_flow->meta.mask_len, &nfp_flow->meta.flags,
				      &new_mask_id);
423 424 425 426

	/* Update flow payload with mask ids. */
	nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id;

427
	/* Release the stats ctx id and ctx to flow table entry. */
428 429
	temp_ctx_id = be32_to_cpu(nfp_flow->meta.host_ctx_id);

430 431 432 433 434 435 436 437 438 439
	ctx_entry = rhashtable_lookup_fast(&priv->stats_ctx_table, &temp_ctx_id,
					   stats_ctx_table_params);
	if (!ctx_entry)
		return -ENOENT;

	WARN_ON_ONCE(rhashtable_remove_fast(&priv->stats_ctx_table,
					    &ctx_entry->ht_node,
					    stats_ctx_table_params));
	kfree(ctx_entry);

440
	return nfp_release_stats_entry(app, temp_ctx_id);
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456
struct nfp_fl_payload *
nfp_flower_get_fl_payload_from_ctx(struct nfp_app *app, u32 ctx_id)
{
	struct nfp_fl_stats_ctx_to_flow *ctx_entry;
	struct nfp_flower_priv *priv = app->priv;

	ctx_entry = rhashtable_lookup_fast(&priv->stats_ctx_table, &ctx_id,
					   stats_ctx_table_params);
	if (!ctx_entry)
		return NULL;

	return ctx_entry->flow;
}

457 458 459 460 461 462
static int nfp_fl_obj_cmpfn(struct rhashtable_compare_arg *arg,
			    const void *obj)
{
	const struct nfp_fl_flow_table_cmp_arg *cmp_arg = arg->key;
	const struct nfp_fl_payload *flow_entry = obj;

463
	if (flow_entry->ingress_dev == cmp_arg->netdev)
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
		return flow_entry->tc_flower_cookie != cmp_arg->cookie;

	return 1;
}

static u32 nfp_fl_obj_hashfn(const void *data, u32 len, u32 seed)
{
	const struct nfp_fl_payload *flower_entry = data;

	return jhash2((u32 *)&flower_entry->tc_flower_cookie,
		      sizeof(flower_entry->tc_flower_cookie) / sizeof(u32),
		      seed);
}

static u32 nfp_fl_key_hashfn(const void *data, u32 len, u32 seed)
{
	const struct nfp_fl_flow_table_cmp_arg *cmp_arg = data;

	return jhash2((u32 *)&cmp_arg->cookie,
		      sizeof(cmp_arg->cookie) / sizeof(u32), seed);
}

const struct rhashtable_params nfp_flower_table_params = {
	.head_offset		= offsetof(struct nfp_fl_payload, fl_node),
	.hashfn			= nfp_fl_key_hashfn,
	.obj_cmpfn		= nfp_fl_obj_cmpfn,
	.obj_hashfn		= nfp_fl_obj_hashfn,
	.automatic_shrinking	= true,
};

494 495 496 497 498 499
const struct rhashtable_params merge_table_params = {
	.key_offset	= offsetof(struct nfp_merge_info, parent_ctx),
	.head_offset	= offsetof(struct nfp_merge_info, ht_node),
	.key_len	= sizeof(u64),
};

L
Louis Peens 已提交
500 501 502 503 504 505 506
const struct rhashtable_params nfp_zone_table_params = {
	.head_offset		= offsetof(struct nfp_fl_ct_zone_entry, hash_node),
	.key_len		= sizeof(u16),
	.key_offset		= offsetof(struct nfp_fl_ct_zone_entry, zone),
	.automatic_shrinking	= false,
};

507 508 509 510 511 512 513
const struct rhashtable_params nfp_ct_map_params = {
	.head_offset		= offsetof(struct nfp_fl_ct_map_entry, hash_node),
	.key_len		= sizeof(unsigned long),
	.key_offset		= offsetof(struct nfp_fl_ct_map_entry, cookie),
	.automatic_shrinking	= true,
};

514 515
int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count,
			     unsigned int host_num_mems)
516 517
{
	struct nfp_flower_priv *priv = app->priv;
518
	int err, stats_size;
519 520

	hash_init(priv->mask_table);
521 522 523 524 525

	err = rhashtable_init(&priv->flow_table, &nfp_flower_table_params);
	if (err)
		return err;

526 527 528 529
	err = rhashtable_init(&priv->stats_ctx_table, &stats_ctx_table_params);
	if (err)
		goto err_free_flow_table;

530 531 532 533
	err = rhashtable_init(&priv->merge_table, &merge_table_params);
	if (err)
		goto err_free_stats_ctx_table;

L
Louis Peens 已提交
534 535 536 537
	err = rhashtable_init(&priv->ct_zone_table, &nfp_zone_table_params);
	if (err)
		goto err_free_merge_table;

538 539 540 541
	err = rhashtable_init(&priv->ct_map_table, &nfp_ct_map_params);
	if (err)
		goto err_free_ct_zone_table;

542 543 544 545 546 547 548
	get_random_bytes(&priv->mask_id_seed, sizeof(priv->mask_id_seed));

	/* Init ring buffer and unallocated mask_ids. */
	priv->mask_ids.mask_id_free_list.buf =
		kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS,
			      NFP_FLOWER_MASK_ELEMENT_RS, GFP_KERNEL);
	if (!priv->mask_ids.mask_id_free_list.buf)
549
		goto err_free_ct_map_table;
550 551 552 553 554 555 556 557 558 559

	priv->mask_ids.init_unallocated = NFP_FLOWER_MASK_ENTRY_RS - 1;

	/* Init timestamps for mask id*/
	priv->mask_ids.last_used =
		kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS,
			      sizeof(*priv->mask_ids.last_used), GFP_KERNEL);
	if (!priv->mask_ids.last_used)
		goto err_free_mask_id;

560 561
	/* Init ring buffer and unallocated stats_ids. */
	priv->stats_ids.free_list.buf =
562
		vmalloc(array_size(NFP_FL_STATS_ELEM_RS,
563
				   priv->stats_ring_size));
564 565 566
	if (!priv->stats_ids.free_list.buf)
		goto err_free_last_used;

567
	priv->stats_ids.init_unalloc = div_u64(host_ctx_count, host_num_mems);
568

569 570 571 572
	stats_size = FIELD_PREP(NFP_FL_STAT_ID_STAT, host_ctx_count) |
		     FIELD_PREP(NFP_FL_STAT_ID_MU_NUM, host_num_mems - 1);
	priv->stats = kvmalloc_array(stats_size, sizeof(struct nfp_fl_stats),
				     GFP_KERNEL);
573 574 575 576 577
	if (!priv->stats)
		goto err_free_ring_buf;

	spin_lock_init(&priv->stats_lock);

578 579
	return 0;

580 581
err_free_ring_buf:
	vfree(priv->stats_ids.free_list.buf);
582
err_free_last_used:
D
Dan Carpenter 已提交
583
	kfree(priv->mask_ids.last_used);
584 585
err_free_mask_id:
	kfree(priv->mask_ids.mask_id_free_list.buf);
586 587
err_free_ct_map_table:
	rhashtable_destroy(&priv->ct_map_table);
L
Louis Peens 已提交
588 589
err_free_ct_zone_table:
	rhashtable_destroy(&priv->ct_zone_table);
590 591
err_free_merge_table:
	rhashtable_destroy(&priv->merge_table);
592 593
err_free_stats_ctx_table:
	rhashtable_destroy(&priv->stats_ctx_table);
594 595
err_free_flow_table:
	rhashtable_destroy(&priv->flow_table);
596 597 598
	return -ENOMEM;
}

599 600 601 602 603 604
static void nfp_zone_table_entry_destroy(struct nfp_fl_ct_zone_entry *zt)
{
	if (!zt)
		return;

	if (!list_empty(&zt->pre_ct_list)) {
605
		struct rhashtable *m_table = &zt->priv->ct_map_table;
606
		struct nfp_fl_ct_flow_entry *entry, *tmp;
607
		struct nfp_fl_ct_map_entry *map;
608 609 610 611

		WARN_ONCE(1, "pre_ct_list not empty as expected, cleaning up\n");
		list_for_each_entry_safe(entry, tmp, &zt->pre_ct_list,
					 list_node) {
612 613 614 615 616 617
			map = rhashtable_lookup_fast(m_table,
						     &entry->cookie,
						     nfp_ct_map_params);
			WARN_ON_ONCE(rhashtable_remove_fast(m_table,
							    &map->hash_node,
							    nfp_ct_map_params));
618
			nfp_fl_ct_clean_flow_entry(entry);
619
			kfree(map);
620 621 622 623
		}
	}

	if (!list_empty(&zt->post_ct_list)) {
624
		struct rhashtable *m_table = &zt->priv->ct_map_table;
625
		struct nfp_fl_ct_flow_entry *entry, *tmp;
626
		struct nfp_fl_ct_map_entry *map;
627 628 629 630

		WARN_ONCE(1, "post_ct_list not empty as expected, cleaning up\n");
		list_for_each_entry_safe(entry, tmp, &zt->post_ct_list,
					 list_node) {
631 632 633 634 635 636
			map = rhashtable_lookup_fast(m_table,
						     &entry->cookie,
						     nfp_ct_map_params);
			WARN_ON_ONCE(rhashtable_remove_fast(m_table,
							    &map->hash_node,
							    nfp_ct_map_params));
637
			nfp_fl_ct_clean_flow_entry(entry);
638
			kfree(map);
639 640
		}
	}
L
Louis Peens 已提交
641

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	if (zt->nft) {
		nf_flow_table_offload_del_cb(zt->nft,
					     nfp_fl_ct_handle_nft_flow,
					     zt);
		zt->nft = NULL;
	}

	if (!list_empty(&zt->nft_flows_list)) {
		struct rhashtable *m_table = &zt->priv->ct_map_table;
		struct nfp_fl_ct_flow_entry *entry, *tmp;
		struct nfp_fl_ct_map_entry *map;

		WARN_ONCE(1, "nft_flows_list not empty as expected, cleaning up\n");
		list_for_each_entry_safe(entry, tmp, &zt->nft_flows_list,
					 list_node) {
			map = rhashtable_lookup_fast(m_table,
						     &entry->cookie,
						     nfp_ct_map_params);
			WARN_ON_ONCE(rhashtable_remove_fast(m_table,
							    &map->hash_node,
							    nfp_ct_map_params));
			nfp_fl_ct_clean_flow_entry(entry);
			kfree(map);
		}
	}

L
Louis Peens 已提交
668 669
	rhashtable_free_and_destroy(&zt->tc_merge_tb,
				    nfp_check_rhashtable_empty, NULL);
670 671
	rhashtable_free_and_destroy(&zt->nft_merge_tb,
				    nfp_check_rhashtable_empty, NULL);
L
Louis Peens 已提交
672

673 674 675
	kfree(zt);
}

L
Louis Peens 已提交
676 677
static void nfp_free_zone_table_entry(void *ptr, void *arg)
{
678 679
	struct nfp_fl_ct_zone_entry *zt = ptr;

680
	nfp_zone_table_entry_destroy(zt);
L
Louis Peens 已提交
681 682
}

683 684 685 686 687 688 689 690 691 692
static void nfp_free_map_table_entry(void *ptr, void *arg)
{
	struct nfp_fl_ct_map_entry *map = ptr;

	if (!map)
		return;

	kfree(map);
}

693 694 695 696 697 698 699
void nfp_flower_metadata_cleanup(struct nfp_app *app)
{
	struct nfp_flower_priv *priv = app->priv;

	if (!priv)
		return;

700 701
	rhashtable_free_and_destroy(&priv->flow_table,
				    nfp_check_rhashtable_empty, NULL);
702 703
	rhashtable_free_and_destroy(&priv->stats_ctx_table,
				    nfp_check_rhashtable_empty, NULL);
704 705
	rhashtable_free_and_destroy(&priv->merge_table,
				    nfp_check_rhashtable_empty, NULL);
L
Louis Peens 已提交
706 707
	rhashtable_free_and_destroy(&priv->ct_zone_table,
				    nfp_free_zone_table_entry, NULL);
708
	nfp_zone_table_entry_destroy(priv->ct_zone_wc);
709 710 711

	rhashtable_free_and_destroy(&priv->ct_map_table,
				    nfp_free_map_table_entry, NULL);
712
	kvfree(priv->stats);
713 714
	kfree(priv->mask_ids.mask_id_free_list.buf);
	kfree(priv->mask_ids.last_used);
715
	vfree(priv->stats_ids.free_list.buf);
716
}