|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 | +// Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + |
| 4 | +#include <linux/rhashtable.h> |
| 5 | +#include <net/flow_offload.h> |
| 6 | +#include "en/tc_priv.h" |
| 7 | +#include "act_stats.h" |
| 8 | +#include "en/fs.h" |
| 9 | + |
| 10 | +struct mlx5e_tc_act_stats_handle { |
| 11 | + struct rhashtable ht; |
| 12 | + spinlock_t ht_lock; /* protects hashtable */ |
| 13 | +}; |
| 14 | + |
| 15 | +struct mlx5e_tc_act_stats { |
| 16 | + unsigned long tc_act_cookie; |
| 17 | + |
| 18 | + struct mlx5_fc *counter; |
| 19 | + u64 lastpackets; |
| 20 | + u64 lastbytes; |
| 21 | + |
| 22 | + struct rhash_head hash; |
| 23 | + struct rcu_head rcu_head; |
| 24 | +}; |
| 25 | + |
| 26 | +static const struct rhashtable_params act_counters_ht_params = { |
| 27 | + .head_offset = offsetof(struct mlx5e_tc_act_stats, hash), |
| 28 | + .key_offset = 0, |
| 29 | + .key_len = offsetof(struct mlx5e_tc_act_stats, counter), |
| 30 | + .automatic_shrinking = true, |
| 31 | +}; |
| 32 | + |
| 33 | +struct mlx5e_tc_act_stats_handle * |
| 34 | +mlx5e_tc_act_stats_create(void) |
| 35 | +{ |
| 36 | + struct mlx5e_tc_act_stats_handle *handle; |
| 37 | + int err; |
| 38 | + |
| 39 | + handle = kvzalloc(sizeof(*handle), GFP_KERNEL); |
| 40 | + if (IS_ERR(handle)) |
| 41 | + return ERR_PTR(-ENOMEM); |
| 42 | + |
| 43 | + err = rhashtable_init(&handle->ht, &act_counters_ht_params); |
| 44 | + if (err) |
| 45 | + goto err; |
| 46 | + |
| 47 | + spin_lock_init(&handle->ht_lock); |
| 48 | + return handle; |
| 49 | +err: |
| 50 | + kvfree(handle); |
| 51 | + return ERR_PTR(err); |
| 52 | +} |
| 53 | + |
| 54 | +void mlx5e_tc_act_stats_free(struct mlx5e_tc_act_stats_handle *handle) |
| 55 | +{ |
| 56 | + rhashtable_destroy(&handle->ht); |
| 57 | + kvfree(handle); |
| 58 | +} |
| 59 | + |
| 60 | +static int |
| 61 | +mlx5e_tc_act_stats_add(struct mlx5e_tc_act_stats_handle *handle, |
| 62 | + unsigned long act_cookie, |
| 63 | + struct mlx5_fc *counter) |
| 64 | +{ |
| 65 | + struct mlx5e_tc_act_stats *act_stats, *old_act_stats; |
| 66 | + struct rhashtable *ht = &handle->ht; |
| 67 | + int err = 0; |
| 68 | + |
| 69 | + act_stats = kvzalloc(sizeof(*act_stats), GFP_KERNEL); |
| 70 | + if (!act_stats) |
| 71 | + return -ENOMEM; |
| 72 | + |
| 73 | + act_stats->tc_act_cookie = act_cookie; |
| 74 | + act_stats->counter = counter; |
| 75 | + |
| 76 | + rcu_read_lock(); |
| 77 | + old_act_stats = rhashtable_lookup_get_insert_fast(ht, |
| 78 | + &act_stats->hash, |
| 79 | + act_counters_ht_params); |
| 80 | + if (IS_ERR(old_act_stats)) { |
| 81 | + err = PTR_ERR(old_act_stats); |
| 82 | + goto err_hash_insert; |
| 83 | + } else if (old_act_stats) { |
| 84 | + err = -EEXIST; |
| 85 | + goto err_hash_insert; |
| 86 | + } |
| 87 | + rcu_read_unlock(); |
| 88 | + |
| 89 | + return 0; |
| 90 | + |
| 91 | +err_hash_insert: |
| 92 | + rcu_read_unlock(); |
| 93 | + kvfree(act_stats); |
| 94 | + return err; |
| 95 | +} |
| 96 | + |
| 97 | +void |
| 98 | +mlx5e_tc_act_stats_del_flow(struct mlx5e_tc_act_stats_handle *handle, |
| 99 | + struct mlx5e_tc_flow *flow) |
| 100 | +{ |
| 101 | + struct mlx5_flow_attr *attr; |
| 102 | + struct mlx5e_tc_act_stats *act_stats; |
| 103 | + int i; |
| 104 | + |
| 105 | + if (!flow_flag_test(flow, USE_ACT_STATS)) |
| 106 | + return; |
| 107 | + |
| 108 | + list_for_each_entry(attr, &flow->attrs, list) { |
| 109 | + for (i = 0; i < attr->tc_act_cookies_count; i++) { |
| 110 | + struct rhashtable *ht = &handle->ht; |
| 111 | + |
| 112 | + spin_lock(&handle->ht_lock); |
| 113 | + act_stats = rhashtable_lookup_fast(ht, |
| 114 | + &attr->tc_act_cookies[i], |
| 115 | + act_counters_ht_params); |
| 116 | + if (act_stats && |
| 117 | + rhashtable_remove_fast(ht, &act_stats->hash, |
| 118 | + act_counters_ht_params) == 0) |
| 119 | + kvfree_rcu(act_stats, rcu_head); |
| 120 | + |
| 121 | + spin_unlock(&handle->ht_lock); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +int |
| 127 | +mlx5e_tc_act_stats_add_flow(struct mlx5e_tc_act_stats_handle *handle, |
| 128 | + struct mlx5e_tc_flow *flow) |
| 129 | +{ |
| 130 | + struct mlx5_fc *curr_counter = NULL; |
| 131 | + unsigned long last_cookie = 0; |
| 132 | + struct mlx5_flow_attr *attr; |
| 133 | + int err; |
| 134 | + int i; |
| 135 | + |
| 136 | + if (!flow_flag_test(flow, USE_ACT_STATS)) |
| 137 | + return 0; |
| 138 | + |
| 139 | + list_for_each_entry(attr, &flow->attrs, list) { |
| 140 | + if (attr->counter) |
| 141 | + curr_counter = attr->counter; |
| 142 | + |
| 143 | + for (i = 0; i < attr->tc_act_cookies_count; i++) { |
| 144 | + /* jump over identical ids (e.g. pedit)*/ |
| 145 | + if (last_cookie == attr->tc_act_cookies[i]) |
| 146 | + continue; |
| 147 | + |
| 148 | + err = mlx5e_tc_act_stats_add(handle, attr->tc_act_cookies[i], curr_counter); |
| 149 | + if (err) |
| 150 | + goto out_err; |
| 151 | + last_cookie = attr->tc_act_cookies[i]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return 0; |
| 156 | +out_err: |
| 157 | + mlx5e_tc_act_stats_del_flow(handle, flow); |
| 158 | + return err; |
| 159 | +} |
| 160 | + |
| 161 | +int |
| 162 | +mlx5e_tc_act_stats_fill_stats(struct mlx5e_tc_act_stats_handle *handle, |
| 163 | + struct flow_offload_action *fl_act) |
| 164 | +{ |
| 165 | + struct rhashtable *ht = &handle->ht; |
| 166 | + struct mlx5e_tc_act_stats *item; |
| 167 | + struct mlx5e_tc_act_stats key; |
| 168 | + u64 pkts, bytes, lastused; |
| 169 | + int err = 0; |
| 170 | + |
| 171 | + key.tc_act_cookie = fl_act->cookie; |
| 172 | + |
| 173 | + rcu_read_lock(); |
| 174 | + item = rhashtable_lookup(ht, &key, act_counters_ht_params); |
| 175 | + if (!item) { |
| 176 | + rcu_read_unlock(); |
| 177 | + err = -ENOENT; |
| 178 | + goto err_out; |
| 179 | + } |
| 180 | + |
| 181 | + mlx5_fc_query_cached_raw(item->counter, |
| 182 | + &bytes, &pkts, &lastused); |
| 183 | + |
| 184 | + flow_stats_update(&fl_act->stats, |
| 185 | + bytes - item->lastbytes, |
| 186 | + pkts - item->lastpackets, |
| 187 | + 0, lastused, FLOW_ACTION_HW_STATS_DELAYED); |
| 188 | + |
| 189 | + item->lastpackets = pkts; |
| 190 | + item->lastbytes = bytes; |
| 191 | + rcu_read_unlock(); |
| 192 | + |
| 193 | + return 0; |
| 194 | + |
| 195 | +err_out: |
| 196 | + return err; |
| 197 | +} |
0 commit comments