Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved. |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 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_profiler.c |
| 19 | * NSS profiler APIs |
| 20 | */ |
| 21 | |
| 22 | #include "nss_tx_rx_common.h" |
| 23 | |
| 24 | /* |
| 25 | * nss_profiler_rx_msg_handler() |
| 26 | * Handle profiler information. |
| 27 | */ |
| 28 | static void nss_profiler_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app) |
| 29 | { |
| 30 | struct nss_profiler_msg *pm = (struct nss_profiler_msg*)ncm; |
| 31 | void *ctx = nss_ctx->nss_top->profiler_ctx[nss_ctx->id]; |
| 32 | nss_profiler_callback_t cb = nss_ctx->nss_top->profiler_callback[nss_ctx->id]; |
| 33 | |
| 34 | if (ncm->type >= NSS_PROFILER_MAX_MSG_TYPES) { |
| 35 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (ncm->type <= NSS_PROFILER_FLOWCTRL_MSG) { |
| 40 | if (ncm->len > sizeof(pm->payload.pcmdp)) { |
| 41 | nss_warning("%p: reply for cmd %d size is wrong %d : %d\n", nss_ctx, ncm->type, ncm->len, ncm->interface); |
| 42 | return; |
| 43 | } |
| 44 | } else if (ncm->type <= NSS_PROFILER_DEBUG_REPLY_MSG) { |
| 45 | if (ncm->len > sizeof(pm->payload.pdm)) { |
| 46 | nss_warning("%p: reply for debug %d is too big %d\n", nss_ctx, ncm->type, ncm->len); |
| 47 | return; |
| 48 | } |
| 49 | } else if (ncm->type <= NSS_PROFILER_COUNTERS_MSG) { |
| 50 | if (ncm->len < (sizeof(pm->payload.pcmdp) - (PROFILE_MAX_APP_COUNTERS - pm->payload.pcmdp.num_counters) * sizeof(pm->payload.pcmdp.counters[0])) || ncm->len > sizeof(pm->payload.pcmdp)) { |
| 51 | nss_warning("%p: %d params data is too big %d : %d\n", nss_ctx, ncm->type, ncm->len, ncm->interface); |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * status per request callback |
| 58 | */ |
| 59 | if (ncm->response != NSS_CMM_RESPONSE_NOTIFY && ncm->cb) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 60 | nss_info("%p: reply CB %p for %d %d\n", nss_ctx, (void *)ncm->cb, ncm->type, ncm->response); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 61 | cb = (nss_profiler_callback_t)ncm->cb; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * sample related callback |
| 66 | */ |
| 67 | if (!cb || !ctx) { |
| 68 | nss_warning("%p: Event received for profiler interface before registration", nss_ctx); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | cb(ctx, (struct nss_profiler_msg *)ncm); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * nss_tx_profiler_if_buf() |
| 77 | * NSS profiler Tx API |
| 78 | */ |
Guojun Jin | 52fbac5 | 2016-08-23 15:43:58 -0700 | [diff] [blame] | 79 | nss_tx_status_t nss_profiler_if_tx_buf(void *ctx, void *buf, uint32_t len, |
| 80 | void *cb, void *app_data) |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 81 | { |
| 82 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx; |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 83 | struct nss_profiler_msg *npm; |
| 84 | struct nss_profiler_data_msg *pdm = (struct nss_profiler_data_msg *)buf; |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 85 | struct nss_cmn_msg *ncm; |
| 86 | nss_tx_status_t ret; |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 87 | |
| 88 | nss_trace("%p: Profiler If Tx, buf=%p", nss_ctx, buf); |
| 89 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 90 | if (NSS_NBUF_PAYLOAD_SIZE < (len + sizeof(npm))) { |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 91 | return NSS_TX_FAILURE_TOO_LARGE; |
| 92 | } |
| 93 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 94 | npm = kzalloc(sizeof(*npm), GFP_KERNEL); |
| 95 | if (!npm) { |
| 96 | nss_warning("%p: Failed to allocate memory for message\n", nss_ctx); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 97 | return NSS_TX_FAILURE; |
| 98 | } |
| 99 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 100 | ncm = &npm->cm; |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 101 | memcpy(&npm->payload, pdm, len); |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 102 | nss_profiler_msg_init(npm, NSS_PROFILER_INTERFACE, pdm->hd_magic & 0xFF, sizeof(npm), |
| 103 | cb, app_data); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 104 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 105 | ret = nss_core_send_cmd(nss_ctx, npm, sizeof(*npm), NSS_NBUF_PAYLOAD_SIZE); |
| 106 | kfree(npm); |
| 107 | return ret; |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 108 | } |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 109 | EXPORT_SYMBOL(nss_profiler_if_tx_buf); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * nss_profiler_notify_register() |
| 113 | */ |
| 114 | void *nss_profiler_notify_register(nss_core_id_t core_id, nss_profiler_callback_t profiler_callback, void *ctx) |
| 115 | { |
| 116 | nss_assert(core_id < NSS_CORE_MAX); |
| 117 | |
Guojun Jin | 79d7698 | 2017-08-07 16:31:07 -0700 | [diff] [blame] | 118 | if (NSS_CORE_STATUS_SUCCESS != |
| 119 | nss_core_register_handler(&nss_top_main.nss[core_id], NSS_PROFILER_INTERFACE, nss_profiler_rx_msg_handler, NULL)) { |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 120 | nss_warning("Message handler FAILED to be registered for profiler"); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | nss_top_main.profiler_ctx[core_id] = ctx; |
| 125 | nss_top_main.profiler_callback[core_id] = profiler_callback; |
| 126 | |
| 127 | return (void *)&nss_top_main.nss[core_id]; |
| 128 | } |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 129 | EXPORT_SYMBOL(nss_profiler_notify_register); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * nss_profiler_notify_unregister() |
| 133 | */ |
| 134 | void nss_profiler_notify_unregister(nss_core_id_t core_id) |
| 135 | { |
| 136 | nss_assert(core_id < NSS_CORE_MAX); |
| 137 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 138 | nss_core_register_handler(&nss_top_main.nss[core_id], NSS_PROFILER_INTERFACE, NULL, NULL); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 139 | nss_top_main.profiler_callback[core_id] = NULL; |
| 140 | nss_top_main.profiler_ctx[core_id] = NULL; |
| 141 | } |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 142 | EXPORT_SYMBOL(nss_profiler_notify_unregister); |
Guojun Jin | 4b3707b | 2014-05-16 15:55:23 -0700 | [diff] [blame] | 143 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 144 | /* |
| 145 | * nss_profiler_msg_init() |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 146 | * Initialize profiler message. |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 147 | */ |
| 148 | void nss_profiler_msg_init(struct nss_profiler_msg *npm, uint16_t if_num, uint32_t type, uint32_t len, |
Sundarajan Srinivasan | 30a53d4 | 2015-01-30 10:52:08 -0800 | [diff] [blame] | 149 | nss_profiler_callback_t cb, void *app_data) |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 150 | { |
| 151 | nss_cmn_msg_init(&npm->cm, if_num, type, len, (void *)cb, app_data); |
| 152 | } |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 153 | EXPORT_SYMBOL(nss_profiler_msg_init); |