Cemil Coskun | cdcd1dd | 2019-01-28 13:35:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
| 3 | * Copyright (c) 2019, The Linux Foundation. All rights reserved. |
| 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * nss_pvxlan.c |
| 19 | * NSS PVXLAN driver interface APIs |
| 20 | */ |
| 21 | #include "nss_core.h" |
| 22 | #include "nss_pvxlan.h" |
| 23 | #include "nss_cmn.h" |
| 24 | #include "nss_tx_rx_common.h" |
| 25 | #include "nss_pvxlan_stats.h" |
| 26 | #include "nss_pvxlan_log.h" |
| 27 | |
| 28 | #define NSS_PVXLAN_TX_TIMEOUT 3000 |
| 29 | |
| 30 | /* |
| 31 | * Spinlock for protecting tunnel operations colliding with a tunnel destroy |
| 32 | */ |
| 33 | DEFINE_SPINLOCK(nss_pvxlan_spinlock); |
| 34 | |
| 35 | /* |
| 36 | * Private data structure |
| 37 | */ |
| 38 | static struct nss_pvxlan_pvt { |
| 39 | struct semaphore sem; /* Semaphore structure. */ |
| 40 | struct completion complete; /* Completion structure. */ |
| 41 | int response; /* Response from FW. */ |
| 42 | void *cb; /* Original cb for msgs. */ |
| 43 | void *app_data; /* Original app_data for msgs. */ |
| 44 | } pvxlan_pvt; |
| 45 | |
| 46 | /* |
| 47 | * Per PVxLAN tunnel/interface number instance. |
| 48 | */ |
| 49 | struct nss_pvxlan_handle { |
| 50 | atomic_t refcnt; /* Reference count on the tunnel */ |
| 51 | uint32_t if_num; /* Interface number */ |
| 52 | uint32_t tunnel_status; /* 0=disable, 1=enabled */ |
| 53 | nss_pvxlan_msg_callback_t msg_callback; /* Msg callback */ |
| 54 | void *app_data; /* App data (argument) */ |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * Array of pointer for NSS PvLAN handles. Each handle has per-tunnel |
| 59 | * stats based on the if_num which is an index. |
| 60 | */ |
| 61 | static struct nss_pvxlan_handle *nss_pvxlan_hdl[NSS_MAX_DYNAMIC_INTERFACES]; |
| 62 | |
| 63 | /* |
| 64 | * nss_pvxlan_verify_if_num() |
| 65 | * Verify if_num passed to us. |
| 66 | */ |
| 67 | static bool nss_pvxlan_verify_if_num(uint32_t if_num) |
| 68 | { |
| 69 | uint32_t type = nss_dynamic_interface_get_type(nss_pvxlan_get_ctx(), if_num); |
| 70 | |
| 71 | return ((type == NSS_DYNAMIC_INTERFACE_TYPE_PVXLAN_HOST_INNER) || |
| 72 | (type == NSS_DYNAMIC_INTERFACE_TYPE_PVXLAN_OUTER)); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * nss_pvxlan_hdl_instance_free() |
| 77 | * Free PVxLAN tunnel handle instance. |
| 78 | */ |
| 79 | static bool nss_pvxlan_hdl_instance_free(struct nss_ctx_instance *nss_ctx, uint32_t if_num) |
| 80 | { |
| 81 | struct nss_pvxlan_handle *h; |
| 82 | |
| 83 | spin_lock(&nss_pvxlan_spinlock); |
| 84 | h = nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START]; |
| 85 | if (!h) { |
| 86 | spin_unlock(&nss_pvxlan_spinlock); |
| 87 | nss_warning("%p: Instance does not exist: %d", nss_ctx, if_num); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (h->if_num != if_num) { |
| 92 | spin_unlock(&nss_pvxlan_spinlock); |
| 93 | nss_warning("%p: Not correct if_num: %d", nss_ctx, if_num); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] = NULL; |
| 98 | spin_unlock(&nss_pvxlan_spinlock); |
| 99 | kfree(h); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * nss_pvxlan_hdl_instance_alloc() |
| 105 | * Allocate PVxLAN tunnel instance. |
| 106 | */ |
| 107 | static bool nss_pvxlan_hdl_instance_alloc(struct nss_ctx_instance *nss_ctx, uint32_t if_num, |
| 108 | nss_pvxlan_msg_callback_t notify_cb, void *app_data) |
| 109 | { |
| 110 | struct nss_pvxlan_handle *h; |
| 111 | |
| 112 | /* |
| 113 | * Allocate a handle |
| 114 | */ |
| 115 | h = kzalloc(sizeof(struct nss_pvxlan_handle), GFP_ATOMIC); |
| 116 | if (!h) { |
| 117 | nss_warning("%p: no memory for allocating PVxLAN handle instance for interface : %d", nss_ctx, if_num); |
| 118 | return false; |
| 119 | } |
| 120 | h->if_num = if_num; |
| 121 | |
| 122 | spin_lock(&nss_pvxlan_spinlock); |
| 123 | if (nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] != NULL) { |
| 124 | spin_unlock(&nss_pvxlan_spinlock); |
| 125 | kfree(h); |
| 126 | nss_warning("%p: The handle has been taken by another thread :%d", nss_ctx, if_num); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | h->msg_callback = notify_cb; |
| 131 | h->app_data = app_data; |
| 132 | nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] = h; |
| 133 | spin_unlock(&nss_pvxlan_spinlock); |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * nss_pvxlan_callback() |
| 140 | * Callback to handle the completion of NSS->HLOS messages. |
| 141 | */ |
| 142 | static void nss_pvxlan_callback(void *app_data, struct nss_pvxlan_msg *nvxm) |
| 143 | { |
| 144 | nss_pvxlan_msg_callback_t callback = (nss_pvxlan_msg_callback_t)pvxlan_pvt.cb; |
| 145 | void *data = pvxlan_pvt.app_data; |
| 146 | |
| 147 | pvxlan_pvt.response = NSS_TX_SUCCESS; |
| 148 | pvxlan_pvt.cb = NULL; |
| 149 | pvxlan_pvt.app_data = NULL; |
| 150 | |
| 151 | if (nvxm->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 152 | nss_warning("Pvxlan Error response %d\n", nvxm->cm.response); |
| 153 | pvxlan_pvt.response = nvxm->cm.response; |
| 154 | } |
| 155 | |
| 156 | if (callback) { |
| 157 | callback(data, nvxm); |
| 158 | } |
| 159 | complete(&pvxlan_pvt.complete); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * nss_pvxlan_handler() |
| 164 | * Handle NSS -> HLOS messages for PVxLAN. |
| 165 | */ |
| 166 | static void nss_pvxlan_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 167 | { |
| 168 | struct nss_pvxlan_msg *nvxm = (struct nss_pvxlan_msg *)ncm; |
| 169 | nss_pvxlan_msg_callback_t cb; |
| 170 | struct nss_pvxlan_handle * h; |
| 171 | |
| 172 | BUG_ON(!nss_pvxlan_verify_if_num(ncm->interface)); |
| 173 | |
| 174 | /* |
| 175 | * Is this a valid request/response packet? |
| 176 | */ |
| 177 | if (ncm->type >= NSS_PVXLAN_MSG_TYPE_MAX) { |
| 178 | nss_warning("%p: received invalid message %d for PVXLAN interface", nss_ctx, ncm->type); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_pvxlan_msg)) { |
| 183 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Trace messages. |
| 189 | */ |
| 190 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 191 | nss_pvxlan_log_rx_msg(nvxm); |
| 192 | |
| 193 | switch (nvxm->cm.type) { |
| 194 | case NSS_PVXLAN_MSG_TYPE_SYNC_STATS: |
| 195 | nss_pvxlan_stats_sync(nss_ctx, &nvxm->msg.stats, ncm->interface); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * Update the callback and app_data for NOTIFY messages. |
| 202 | */ |
| 203 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
| 204 | uint32_t if_num = ncm->interface - NSS_DYNAMIC_IF_START; |
| 205 | spin_lock(&nss_pvxlan_spinlock); |
| 206 | h = nss_pvxlan_hdl[if_num]; |
| 207 | if (h) { |
| 208 | ncm->cb = (nss_ptr_t)h->msg_callback; |
| 209 | ncm->app_data = (nss_ptr_t)h->app_data; |
| 210 | } |
| 211 | spin_unlock(&nss_pvxlan_spinlock); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | cb = (nss_pvxlan_msg_callback_t)ncm->cb; |
| 216 | |
| 217 | /* |
| 218 | * Do we have a callback |
| 219 | */ |
| 220 | if (!cb) { |
| 221 | nss_trace("%p: cb is null for interface %d", nss_ctx, ncm->interface); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | cb((void *)ncm->app_data, nvxm); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * nss_pvxlan_tx_msg() |
| 230 | * Transmit a PVXLAN message to NSS FW. Don't call this from softirq/interrupts. |
| 231 | */ |
| 232 | nss_tx_status_t nss_pvxlan_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_pvxlan_msg *msg) |
| 233 | { |
| 234 | struct nss_cmn_msg *ncm = &msg->cm; |
| 235 | |
| 236 | if (!nss_pvxlan_verify_if_num(msg->cm.interface)) { |
| 237 | return NSS_TX_FAILURE_BAD_PARAM; |
| 238 | } |
| 239 | |
| 240 | if (ncm->type >= NSS_PVXLAN_MSG_TYPE_MAX) { |
| 241 | return NSS_TX_FAILURE_BAD_PARAM; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Trace messages. |
| 246 | */ |
| 247 | nss_pvxlan_log_tx_msg(msg); |
| 248 | |
| 249 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
| 250 | } |
| 251 | EXPORT_SYMBOL(nss_pvxlan_tx_msg); |
| 252 | |
| 253 | /* |
| 254 | * nss_pvxlan_tx_msg_sync() |
| 255 | * Transmit a pvxlan message to NSS firmware synchronously. |
| 256 | */ |
| 257 | nss_tx_status_t nss_pvxlan_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_pvxlan_msg *nvxm) |
| 258 | { |
| 259 | nss_tx_status_t status; |
| 260 | int ret; |
| 261 | |
| 262 | down(&pvxlan_pvt.sem); |
| 263 | nvxm->cm.cb = (nss_ptr_t)nss_pvxlan_callback; |
| 264 | nvxm->cm.app_data = (nss_ptr_t)NULL; |
| 265 | |
| 266 | status = nss_pvxlan_tx_msg(nss_ctx, nvxm); |
| 267 | if (status != NSS_TX_SUCCESS) { |
| 268 | nss_warning("%p: pvxlan_tx_msg failed\n", nss_ctx); |
| 269 | up(&pvxlan_pvt.sem); |
| 270 | return status; |
| 271 | } |
| 272 | |
| 273 | ret = wait_for_completion_timeout(&pvxlan_pvt.complete, msecs_to_jiffies(NSS_PVXLAN_TX_TIMEOUT)); |
| 274 | if (!ret) { |
| 275 | nss_warning("%p: pvxlan tx sync failed due to timeout\n", nss_ctx); |
| 276 | pvxlan_pvt.response = NSS_TX_FAILURE; |
| 277 | } |
| 278 | |
| 279 | status = pvxlan_pvt.response; |
| 280 | up(&pvxlan_pvt.sem); |
| 281 | return status; |
| 282 | } |
| 283 | EXPORT_SYMBOL(nss_pvxlan_tx_msg_sync); |
| 284 | |
| 285 | /* |
| 286 | * nss_pvxlan_tx_buf() |
| 287 | * Transmit data buffer (skb) to a NSS interface number |
| 288 | */ |
| 289 | nss_tx_status_t nss_pvxlan_tx_buf(struct nss_ctx_instance *nss_ctx, struct sk_buff *buf, uint32_t if_num) |
| 290 | { |
| 291 | BUG_ON(!nss_pvxlan_verify_if_num(if_num)); |
| 292 | |
| 293 | return nss_core_send_packet(nss_ctx, buf, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER); |
| 294 | } |
| 295 | EXPORT_SYMBOL(nss_pvxlan_tx_buf); |
| 296 | |
| 297 | /* |
| 298 | *********************************** |
| 299 | * Register/Unregister/Miscellaneous APIs |
| 300 | *********************************** |
| 301 | */ |
| 302 | |
| 303 | /* |
| 304 | * nss_pvxlan_unregister() |
| 305 | * Unregister a data packet notifier with NSS FW. |
| 306 | */ |
| 307 | bool nss_pvxlan_unregister(uint32_t if_num) |
| 308 | { |
| 309 | struct nss_ctx_instance *nss_ctx; |
| 310 | int32_t i; |
| 311 | |
| 312 | nss_ctx = nss_pvxlan_get_ctx(); |
| 313 | if (!nss_pvxlan_verify_if_num(if_num)) { |
| 314 | nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | spin_lock_bh(&nss_pvxlan_tunnel_stats_debug_lock); |
| 319 | for (i = 0; i < NSS_PVXLAN_MAX_INTERFACES; i++) { |
| 320 | if (nss_pvxlan_tunnel_debug_stats[i].if_num != if_num) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | memset(&nss_pvxlan_tunnel_debug_stats[i], 0, |
| 325 | sizeof(struct nss_pvxlan_tunnel_stats_debug)); |
| 326 | break; |
| 327 | } |
| 328 | spin_unlock_bh(&nss_pvxlan_tunnel_stats_debug_lock); |
| 329 | |
| 330 | nss_core_unregister_handler(nss_ctx, if_num); |
| 331 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
| 332 | nss_pvxlan_hdl_instance_free(nss_ctx, if_num); |
| 333 | return true; |
| 334 | } |
| 335 | EXPORT_SYMBOL(nss_pvxlan_unregister); |
| 336 | |
| 337 | /* |
| 338 | * nss_pvxlan_register() |
| 339 | * Registers a data packet notifier with NSS FW. |
| 340 | */ |
| 341 | struct nss_ctx_instance *nss_pvxlan_register(uint32_t if_num, |
| 342 | nss_pvxlan_buf_callback_t data_cb, |
| 343 | nss_pvxlan_msg_callback_t notify_cb, |
| 344 | struct net_device *netdev, |
| 345 | uint32_t features) |
| 346 | { |
| 347 | struct nss_ctx_instance *nss_ctx; |
| 348 | int core_status; |
| 349 | int32_t i; |
| 350 | |
| 351 | nss_ctx = nss_pvxlan_get_ctx(); |
| 352 | if (!nss_pvxlan_verify_if_num(if_num)) { |
| 353 | nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | core_status = nss_core_register_handler(nss_ctx, if_num, nss_pvxlan_msg_handler, NULL); |
| 358 | if (core_status != NSS_CORE_STATUS_SUCCESS) { |
| 359 | nss_warning("%p: nss core register handler failed for if_num:%d with error :%d", nss_ctx, if_num, core_status); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | if (!nss_pvxlan_hdl_instance_alloc(nss_ctx, if_num, notify_cb, (void *)netdev)) { |
| 364 | nss_core_unregister_handler(nss_ctx, if_num); |
| 365 | nss_warning("%p: couldn't allocate handle instance for if_num:%d", nss_ctx, if_num); |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | spin_lock_bh(&nss_pvxlan_tunnel_stats_debug_lock); |
| 370 | for (i = 0; i < NSS_PVXLAN_MAX_INTERFACES; i++) { |
| 371 | if (nss_pvxlan_tunnel_debug_stats[i].valid) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | nss_pvxlan_tunnel_debug_stats[i].valid = true; |
| 376 | nss_pvxlan_tunnel_debug_stats[i].if_num = if_num; |
| 377 | nss_pvxlan_tunnel_debug_stats[i].if_index = netdev->ifindex; |
| 378 | break; |
| 379 | } |
| 380 | spin_unlock_bh(&nss_pvxlan_tunnel_stats_debug_lock); |
| 381 | |
| 382 | if (i == NSS_PVXLAN_MAX_INTERFACES) { |
| 383 | nss_warning("%p: No available debug stats instance :%d", nss_ctx, if_num); |
| 384 | nss_pvxlan_hdl_instance_free(nss_ctx, if_num); |
| 385 | nss_core_unregister_handler(nss_ctx, if_num); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | nss_core_register_subsys_dp(nss_ctx, if_num, data_cb, NULL, NULL, netdev, features); |
| 390 | return nss_ctx; |
| 391 | } |
| 392 | EXPORT_SYMBOL(nss_pvxlan_register); |
| 393 | |
| 394 | /* |
| 395 | * nss_pvxlan_ifnum_with_core_id() |
| 396 | * Append core id to pvxlan interface num. |
| 397 | */ |
| 398 | int nss_pvxlan_ifnum_with_core_id(int if_num) |
| 399 | { |
| 400 | struct nss_ctx_instance *nss_ctx = nss_pvxlan_get_ctx(); |
| 401 | |
| 402 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 403 | |
| 404 | if (!nss_is_dynamic_interface(if_num)) { |
| 405 | nss_warning("%p: Invalid if_num: %d, must be a dynamic interface\n", nss_ctx, if_num); |
| 406 | return 0; |
| 407 | } |
| 408 | return NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num); |
| 409 | } |
| 410 | EXPORT_SYMBOL(nss_pvxlan_ifnum_with_core_id); |
| 411 | |
| 412 | /* |
| 413 | * nss_pvxlan_msg_init() |
| 414 | * Initialize pvxlan message. |
| 415 | */ |
| 416 | void nss_pvxlan_msg_init(struct nss_pvxlan_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, |
| 417 | nss_pvxlan_msg_callback_t cb, void *app_data) |
| 418 | { |
| 419 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void*)cb, app_data); |
| 420 | } |
| 421 | EXPORT_SYMBOL(nss_pvxlan_msg_init); |
| 422 | |
| 423 | /* |
| 424 | * nss_pvxlan_get_ctx() |
| 425 | * Return a Pvxlan NSS context. |
| 426 | */ |
| 427 | struct nss_ctx_instance *nss_pvxlan_get_ctx() |
| 428 | { |
| 429 | struct nss_ctx_instance *nss_ctx; |
| 430 | |
| 431 | nss_ctx = &nss_top_main.nss[nss_top_main.pvxlan_handler_id]; |
| 432 | return nss_ctx; |
| 433 | } |
| 434 | EXPORT_SYMBOL(nss_pvxlan_get_ctx); |
| 435 | |
| 436 | /* |
| 437 | * nss_pvxlan_init() |
| 438 | * Initializes Pvxlan. Gets called from nss_init.c. |
| 439 | */ |
| 440 | void nss_pvxlan_init() |
| 441 | { |
| 442 | nss_pvxlan_stats_dentry_create(); |
| 443 | sema_init(&pvxlan_pvt.sem, 1); |
| 444 | init_completion(&pvxlan_pvt.complete); |
| 445 | |
| 446 | memset(&nss_pvxlan_hdl, 0, sizeof(nss_pvxlan_hdl)); |
| 447 | } |