Pieter Jansen van Vuuren | 43f84b7 | 2017-06-29 22:08:17 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. |
| 3 | * |
| 4 | * This software is dual licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| 6 | * source tree or the BSD 2-Clause License provided below. You have the |
| 7 | * option to license this software under the complete terms of either license. |
| 8 | * |
| 9 | * The BSD 2-Clause License: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * 2. Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | #include <linux/hash.h> |
| 35 | #include <linux/hashtable.h> |
| 36 | #include <linux/jhash.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | #include <net/pkt_cls.h> |
| 39 | |
| 40 | #include "cmsg.h" |
| 41 | #include "main.h" |
| 42 | #include "../nfp_app.h" |
| 43 | |
| 44 | struct nfp_mask_id_table { |
| 45 | struct hlist_node link; |
| 46 | u32 hash_key; |
| 47 | u32 ref_cnt; |
| 48 | u8 mask_id; |
| 49 | }; |
| 50 | |
| 51 | /* Must be called with either RTNL or rcu_read_lock */ |
| 52 | struct nfp_fl_payload * |
| 53 | nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie) |
| 54 | { |
| 55 | struct nfp_flower_priv *priv = app->priv; |
| 56 | struct nfp_fl_payload *flower_entry; |
| 57 | |
| 58 | hash_for_each_possible_rcu(priv->flow_table, flower_entry, link, |
| 59 | tc_flower_cookie) |
| 60 | if (flower_entry->tc_flower_cookie == tc_flower_cookie) |
| 61 | return flower_entry; |
| 62 | |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | static int nfp_release_mask_id(struct nfp_app *app, u8 mask_id) |
| 67 | { |
| 68 | struct nfp_flower_priv *priv = app->priv; |
| 69 | struct circ_buf *ring; |
| 70 | struct timespec64 now; |
| 71 | |
| 72 | ring = &priv->mask_ids.mask_id_free_list; |
| 73 | /* Checking if buffer is full. */ |
| 74 | if (CIRC_SPACE(ring->head, ring->tail, NFP_FLOWER_MASK_ENTRY_RS) == 0) |
| 75 | return -ENOBUFS; |
| 76 | |
| 77 | memcpy(&ring->buf[ring->head], &mask_id, NFP_FLOWER_MASK_ELEMENT_RS); |
| 78 | ring->head = (ring->head + NFP_FLOWER_MASK_ELEMENT_RS) % |
| 79 | (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS); |
| 80 | |
| 81 | getnstimeofday64(&now); |
| 82 | priv->mask_ids.last_used[mask_id] = now; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int nfp_mask_alloc(struct nfp_app *app, u8 *mask_id) |
| 88 | { |
| 89 | struct nfp_flower_priv *priv = app->priv; |
| 90 | struct timespec64 delta, now; |
| 91 | struct circ_buf *ring; |
| 92 | u8 temp_id, freed_id; |
| 93 | |
| 94 | ring = &priv->mask_ids.mask_id_free_list; |
| 95 | freed_id = NFP_FLOWER_MASK_ENTRY_RS - 1; |
| 96 | /* Checking for unallocated entries first. */ |
| 97 | if (priv->mask_ids.init_unallocated > 0) { |
| 98 | *mask_id = priv->mask_ids.init_unallocated; |
| 99 | priv->mask_ids.init_unallocated--; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* Checking if buffer is empty. */ |
| 104 | if (ring->head == ring->tail) |
| 105 | goto err_not_found; |
| 106 | |
| 107 | memcpy(&temp_id, &ring->buf[ring->tail], NFP_FLOWER_MASK_ELEMENT_RS); |
| 108 | *mask_id = temp_id; |
| 109 | |
| 110 | getnstimeofday64(&now); |
| 111 | delta = timespec64_sub(now, priv->mask_ids.last_used[*mask_id]); |
| 112 | |
| 113 | if (timespec64_to_ns(&delta) < NFP_FL_MASK_REUSE_TIME_NS) |
| 114 | goto err_not_found; |
| 115 | |
| 116 | memcpy(&ring->buf[ring->tail], &freed_id, NFP_FLOWER_MASK_ELEMENT_RS); |
| 117 | ring->tail = (ring->tail + NFP_FLOWER_MASK_ELEMENT_RS) % |
| 118 | (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS); |
| 119 | |
| 120 | return 0; |
| 121 | |
| 122 | err_not_found: |
| 123 | *mask_id = freed_id; |
| 124 | return -ENOENT; |
| 125 | } |
| 126 | |
| 127 | static int |
| 128 | nfp_add_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len) |
| 129 | { |
| 130 | struct nfp_flower_priv *priv = app->priv; |
| 131 | struct nfp_mask_id_table *mask_entry; |
| 132 | unsigned long hash_key; |
| 133 | u8 mask_id; |
| 134 | |
| 135 | if (nfp_mask_alloc(app, &mask_id)) |
| 136 | return -ENOENT; |
| 137 | |
| 138 | mask_entry = kmalloc(sizeof(*mask_entry), GFP_KERNEL); |
| 139 | if (!mask_entry) { |
| 140 | nfp_release_mask_id(app, mask_id); |
| 141 | return -ENOMEM; |
| 142 | } |
| 143 | |
| 144 | INIT_HLIST_NODE(&mask_entry->link); |
| 145 | mask_entry->mask_id = mask_id; |
| 146 | hash_key = jhash(mask_data, mask_len, priv->mask_id_seed); |
| 147 | mask_entry->hash_key = hash_key; |
| 148 | mask_entry->ref_cnt = 1; |
| 149 | hash_add(priv->mask_table, &mask_entry->link, hash_key); |
| 150 | |
| 151 | return mask_id; |
| 152 | } |
| 153 | |
| 154 | static struct nfp_mask_id_table * |
| 155 | nfp_search_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len) |
| 156 | { |
| 157 | struct nfp_flower_priv *priv = app->priv; |
| 158 | struct nfp_mask_id_table *mask_entry; |
| 159 | unsigned long hash_key; |
| 160 | |
| 161 | hash_key = jhash(mask_data, mask_len, priv->mask_id_seed); |
| 162 | |
| 163 | hash_for_each_possible(priv->mask_table, mask_entry, link, hash_key) |
| 164 | if (mask_entry->hash_key == hash_key) |
| 165 | return mask_entry; |
| 166 | |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | nfp_find_in_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len) |
| 172 | { |
| 173 | struct nfp_mask_id_table *mask_entry; |
| 174 | |
| 175 | mask_entry = nfp_search_mask_table(app, mask_data, mask_len); |
| 176 | if (!mask_entry) |
| 177 | return -ENOENT; |
| 178 | |
| 179 | mask_entry->ref_cnt++; |
| 180 | |
| 181 | /* Casting u8 to int for later use. */ |
| 182 | return mask_entry->mask_id; |
| 183 | } |
| 184 | |
| 185 | static bool |
| 186 | nfp_check_mask_add(struct nfp_app *app, char *mask_data, u32 mask_len, |
| 187 | u8 *meta_flags, u8 *mask_id) |
| 188 | { |
| 189 | int id; |
| 190 | |
| 191 | id = nfp_find_in_mask_table(app, mask_data, mask_len); |
| 192 | if (id < 0) { |
| 193 | id = nfp_add_mask_table(app, mask_data, mask_len); |
| 194 | if (id < 0) |
| 195 | return false; |
| 196 | *meta_flags |= NFP_FL_META_FLAG_NEW_MASK; |
| 197 | } |
| 198 | *mask_id = id; |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | static bool |
| 204 | nfp_check_mask_remove(struct nfp_app *app, char *mask_data, u32 mask_len, |
| 205 | u8 *meta_flags, u8 *mask_id) |
| 206 | { |
| 207 | struct nfp_mask_id_table *mask_entry; |
| 208 | |
| 209 | mask_entry = nfp_search_mask_table(app, mask_data, mask_len); |
| 210 | if (!mask_entry) |
| 211 | return false; |
| 212 | |
| 213 | *mask_id = mask_entry->mask_id; |
| 214 | mask_entry->ref_cnt--; |
| 215 | if (!mask_entry->ref_cnt) { |
| 216 | hash_del(&mask_entry->link); |
| 217 | nfp_release_mask_id(app, *mask_id); |
| 218 | kfree(mask_entry); |
| 219 | if (meta_flags) |
| 220 | *meta_flags |= NFP_FL_META_FLAG_LAST_MASK; |
| 221 | } |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | int nfp_compile_flow_metadata(struct nfp_app *app, |
| 227 | struct tc_cls_flower_offload *flow, |
| 228 | struct nfp_fl_payload *nfp_flow) |
| 229 | { |
| 230 | struct nfp_flower_priv *priv = app->priv; |
| 231 | struct nfp_fl_payload *check_entry; |
| 232 | u8 new_mask_id; |
| 233 | |
| 234 | new_mask_id = 0; |
| 235 | if (!nfp_check_mask_add(app, nfp_flow->mask_data, |
| 236 | nfp_flow->meta.mask_len, |
| 237 | &nfp_flow->meta.flags, &new_mask_id)) |
| 238 | return -ENOENT; |
| 239 | |
| 240 | nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version); |
| 241 | priv->flower_version++; |
| 242 | |
| 243 | /* Update flow payload with mask ids. */ |
| 244 | nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id; |
| 245 | |
| 246 | check_entry = nfp_flower_search_fl_table(app, flow->cookie); |
| 247 | if (check_entry) { |
| 248 | if (!nfp_check_mask_remove(app, nfp_flow->mask_data, |
| 249 | nfp_flow->meta.mask_len, |
| 250 | NULL, &new_mask_id)) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | return -EEXIST; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int nfp_modify_flow_metadata(struct nfp_app *app, |
| 260 | struct nfp_fl_payload *nfp_flow) |
| 261 | { |
| 262 | struct nfp_flower_priv *priv = app->priv; |
| 263 | u8 new_mask_id = 0; |
| 264 | |
| 265 | nfp_check_mask_remove(app, nfp_flow->mask_data, |
| 266 | nfp_flow->meta.mask_len, &nfp_flow->meta.flags, |
| 267 | &new_mask_id); |
| 268 | |
| 269 | nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version); |
| 270 | priv->flower_version++; |
| 271 | |
| 272 | /* Update flow payload with mask ids. */ |
| 273 | nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int nfp_flower_metadata_init(struct nfp_app *app) |
| 279 | { |
| 280 | struct nfp_flower_priv *priv = app->priv; |
| 281 | |
| 282 | hash_init(priv->mask_table); |
| 283 | hash_init(priv->flow_table); |
| 284 | get_random_bytes(&priv->mask_id_seed, sizeof(priv->mask_id_seed)); |
| 285 | |
| 286 | /* Init ring buffer and unallocated mask_ids. */ |
| 287 | priv->mask_ids.mask_id_free_list.buf = |
| 288 | kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS, |
| 289 | NFP_FLOWER_MASK_ELEMENT_RS, GFP_KERNEL); |
| 290 | if (!priv->mask_ids.mask_id_free_list.buf) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | priv->mask_ids.init_unallocated = NFP_FLOWER_MASK_ENTRY_RS - 1; |
| 294 | |
| 295 | /* Init timestamps for mask id*/ |
| 296 | priv->mask_ids.last_used = |
| 297 | kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS, |
| 298 | sizeof(*priv->mask_ids.last_used), GFP_KERNEL); |
| 299 | if (!priv->mask_ids.last_used) |
| 300 | goto err_free_mask_id; |
| 301 | |
| 302 | return 0; |
| 303 | |
| 304 | err_free_mask_id: |
| 305 | kfree(priv->mask_ids.mask_id_free_list.buf); |
| 306 | return -ENOMEM; |
| 307 | } |
| 308 | |
| 309 | void nfp_flower_metadata_cleanup(struct nfp_app *app) |
| 310 | { |
| 311 | struct nfp_flower_priv *priv = app->priv; |
| 312 | |
| 313 | if (!priv) |
| 314 | return; |
| 315 | |
| 316 | kfree(priv->mask_ids.mask_id_free_list.buf); |
| 317 | kfree(priv->mask_ids.last_used); |
| 318 | } |