Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [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 | #include "nss_tx_rx_common.h" |
| 18 | #include "nss_dtls_stats.h" |
| 19 | |
| 20 | #define NSS_DTLS_TX_TIMEOUT 3000 /* 3 Seconds */ |
| 21 | |
| 22 | /* |
| 23 | * Data structures to store DTLS nss debug stats |
| 24 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 25 | static DEFINE_SPINLOCK(nss_dtls_session_stats_lock); |
| 26 | static struct nss_dtls_stats_session session_stats[NSS_MAX_DTLS_SESSIONS]; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Private data structure |
| 30 | */ |
| 31 | static struct nss_dtls_pvt { |
| 32 | struct semaphore sem; |
| 33 | struct completion complete; |
| 34 | int response; |
| 35 | void *cb; |
| 36 | void *app_data; |
| 37 | } dtls_pvt; |
| 38 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 39 | /* |
| 40 | * nss_dtls_verify_if_num() |
| 41 | * Verify if_num passed to us. |
| 42 | */ |
| 43 | static bool nss_dtls_verify_if_num(uint32_t if_num) |
| 44 | { |
| 45 | if (nss_is_dynamic_interface(if_num) == false) |
| 46 | return false; |
| 47 | |
Stephen Wang | e8b8d0d | 2017-02-24 17:05:22 -0800 | [diff] [blame] | 48 | if (nss_dynamic_interface_get_type(nss_dtls_get_context(), if_num) |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 49 | != NSS_DYNAMIC_INTERFACE_TYPE_DTLS) |
| 50 | return false; |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * nss_dtls_session_stats_sync |
| 57 | * Per DTLS session debug stats |
| 58 | */ |
| 59 | static void nss_dtls_session_stats_sync(struct nss_ctx_instance *nss_ctx, |
| 60 | struct nss_dtls_session_stats *stats_msg, |
| 61 | uint16_t if_num) |
| 62 | { |
| 63 | int i; |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 64 | struct nss_dtls_stats_session *s = NULL; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 65 | |
| 66 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 67 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 68 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 69 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 70 | if (session_stats[i].if_num != if_num) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 71 | continue; |
| 72 | } |
| 73 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 74 | s = &session_stats[i]; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | |
| 78 | if (!s) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 79 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 83 | s->stats[NSS_DTLS_STATS_SESSION_RX_PKTS] += stats_msg->node_stats.rx_packets; |
| 84 | s->stats[NSS_DTLS_STATS_SESSION_TX_PKTS] += stats_msg->node_stats.tx_packets; |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 85 | for (i = 0; i < NSS_MAX_NUM_PRI; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 86 | s->stats[NSS_DTLS_STATS_SESSION_RX_QUEUE_0_DROPPED + i] += stats_msg->node_stats.rx_dropped[i]; |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 87 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 88 | s->stats[NSS_DTLS_STATS_SESSION_RX_AUTH_DONE] += stats_msg->rx_auth_done; |
| 89 | s->stats[NSS_DTLS_STATS_SESSION_TX_AUTH_DONE] += stats_msg->tx_auth_done; |
| 90 | s->stats[NSS_DTLS_STATS_SESSION_RX_CIPHER_DONE] += stats_msg->rx_cipher_done; |
| 91 | s->stats[NSS_DTLS_STATS_SESSION_TX_CIPHER_DONE] += stats_msg->tx_cipher_done; |
| 92 | s->stats[NSS_DTLS_STATS_SESSION_RX_CBUF_ALLOC_FAIL] += stats_msg->rx_cbuf_alloc_fail; |
| 93 | s->stats[NSS_DTLS_STATS_SESSION_TX_CBUF_ALLOC_FAIL] += stats_msg->tx_cbuf_alloc_fail; |
| 94 | s->stats[NSS_DTLS_STATS_SESSION_TX_CENQUEUE_FAIL] += stats_msg->tx_cenqueue_fail; |
| 95 | s->stats[NSS_DTLS_STATS_SESSION_RX_CENQUEUE_FAIL] += stats_msg->rx_cenqueue_fail; |
| 96 | s->stats[NSS_DTLS_STATS_SESSION_TX_DROPPED_HROOM] += stats_msg->tx_dropped_hroom; |
| 97 | s->stats[NSS_DTLS_STATS_SESSION_TX_DROPPED_TROOM] += stats_msg->tx_dropped_troom; |
| 98 | s->stats[NSS_DTLS_STATS_SESSION_TX_FORWARD_ENQUEUE_FAIL] += stats_msg->tx_forward_enqueue_fail; |
| 99 | s->stats[NSS_DTLS_STATS_SESSION_RX_FORWARD_ENQUEUE_FAIL] += stats_msg->rx_forward_enqueue_fail; |
| 100 | s->stats[NSS_DTLS_STATS_SESSION_RX_INVALID_VERSION] += stats_msg->rx_invalid_version; |
| 101 | s->stats[NSS_DTLS_STATS_SESSION_RX_INVALID_EPOCH] += stats_msg->rx_invalid_epoch; |
| 102 | s->stats[NSS_DTLS_STATS_SESSION_RX_MALFORMED] += stats_msg->rx_malformed; |
| 103 | s->stats[NSS_DTLS_STATS_SESSION_RX_CIPHER_FAIL] += stats_msg->rx_cipher_fail; |
| 104 | s->stats[NSS_DTLS_STATS_SESSION_RX_AUTH_FAIL] += stats_msg->rx_auth_fail; |
| 105 | s->stats[NSS_DTLS_STATS_SESSION_RX_CAPWAP_CLASSIFY_FAIL] += stats_msg->rx_capwap_classify_fail; |
| 106 | s->stats[NSS_DTLS_STATS_SESSION_RX_SINGLE_REC_DGRAM] += stats_msg->rx_single_rec_dgram; |
| 107 | s->stats[NSS_DTLS_STATS_SESSION_RX_MULTI_REC_DGRAM] += stats_msg->rx_multi_rec_dgram; |
| 108 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_FAIL] += stats_msg->rx_replay_fail; |
| 109 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_DUPLICATE] += stats_msg->rx_replay_duplicate; |
| 110 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_OUT_OF_WINDOW] += stats_msg->rx_replay_out_of_window; |
| 111 | s->stats[NSS_DTLS_STATS_SESSION_OUTFLOW_QUEUE_FULL] += stats_msg->outflow_queue_full; |
| 112 | s->stats[NSS_DTLS_STATS_SESSION_DECAP_QUEUE_FULL] += stats_msg->decap_queue_full; |
| 113 | s->stats[NSS_DTLS_STATS_SESSION_PBUF_ALLOC_FAIL] += stats_msg->pbuf_alloc_fail; |
| 114 | s->stats[NSS_DTLS_STATS_SESSION_PBUF_COPY_FAIL] += stats_msg->pbuf_copy_fail; |
| 115 | s->stats[NSS_DTLS_STATS_SESSION_EPOCH] = stats_msg->epoch; |
| 116 | s->stats[NSS_DTLS_STATS_SESSION_TX_SEQ_HIGH] = stats_msg->tx_seq_high; |
| 117 | s->stats[NSS_DTLS_STATS_SESSION_TX_SEQ_LOW] = stats_msg->tx_seq_low; |
| 118 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 122 | * nss_dtls_session_stats_get() |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 123 | * Get session DTLS statitics. |
| 124 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 125 | void nss_dtls_session_stats_get(struct nss_dtls_stats_session *stats) |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 126 | { |
| 127 | int i; |
| 128 | |
| 129 | if (!stats) { |
| 130 | nss_warning("No memory to copy dtls session stats"); |
| 131 | return; |
| 132 | } |
| 133 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 134 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 135 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 136 | if (session_stats[i].valid) { |
| 137 | memcpy(stats, &session_stats[i], |
| 138 | sizeof(struct nss_dtls_stats_session)); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 139 | stats++; |
| 140 | } |
| 141 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 142 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * nss_dtls_handler() |
| 147 | * Handle NSS -> HLOS messages for dtls tunnel |
| 148 | */ |
| 149 | static void nss_dtls_handler(struct nss_ctx_instance *nss_ctx, |
| 150 | struct nss_cmn_msg *ncm, |
| 151 | __attribute__((unused))void *app_data) |
| 152 | { |
| 153 | struct nss_dtls_msg *ntm = (struct nss_dtls_msg *)ncm; |
| 154 | void *ctx; |
| 155 | |
| 156 | nss_dtls_msg_callback_t cb; |
| 157 | |
| 158 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 159 | BUG_ON(!nss_dtls_verify_if_num(ncm->interface)); |
| 160 | |
| 161 | /* |
| 162 | * Is this a valid request/response packet? |
| 163 | */ |
| 164 | if (ncm->type >= NSS_DTLS_MSG_MAX) { |
| 165 | nss_warning("%p: received invalid message %d " |
| 166 | "for DTLS interface %d", |
| 167 | nss_ctx, ncm->type, ncm->interface); |
| 168 | return; |
| 169 | } |
| 170 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 171 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_dtls_msg)) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 172 | nss_warning("%p: dtls message length is invalid: %d", |
| 173 | nss_ctx, ncm->len); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | switch (ntm->cm.type) { |
| 178 | case NSS_DTLS_MSG_SESSION_STATS: |
| 179 | nss_dtls_session_stats_sync(nss_ctx, |
| 180 | &ntm->msg.stats, |
| 181 | ncm->interface); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Update the callback and app_data for NOTIFY messages |
| 187 | */ |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame] | 188 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 189 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->dtls_msg_callback; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 190 | ncm->app_data = (nss_ptr_t)nss_ctx->subsys_dp_register[ncm->interface].app_data; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Log failures |
| 195 | */ |
| 196 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 197 | |
| 198 | /* |
| 199 | * callback |
| 200 | */ |
| 201 | cb = (nss_dtls_msg_callback_t)ncm->cb; |
| 202 | ctx = (void *)ncm->app_data; |
| 203 | |
| 204 | /* |
| 205 | * call dtls session callback |
| 206 | */ |
| 207 | if (!cb) { |
| 208 | nss_warning("%p: No callback for dtls session interface %d", |
| 209 | nss_ctx, ncm->interface); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | cb(ctx, ntm); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * nss_dtls_callback() |
| 218 | * Callback to handle the completion of NSS->HLOS messages. |
| 219 | */ |
| 220 | static void nss_dtls_callback(void *app_data, struct nss_dtls_msg *nim) |
| 221 | { |
| 222 | nss_dtls_msg_callback_t callback = (nss_dtls_msg_callback_t)dtls_pvt.cb; |
| 223 | void *data = dtls_pvt.app_data; |
| 224 | |
| 225 | dtls_pvt.cb = NULL; |
| 226 | dtls_pvt.app_data = NULL; |
| 227 | |
| 228 | if (nim->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 229 | nss_warning("dtls Error response %d\n", nim->cm.response); |
| 230 | |
| 231 | dtls_pvt.response = NSS_TX_FAILURE; |
| 232 | if (callback) { |
| 233 | callback(data, nim); |
| 234 | } |
| 235 | |
| 236 | complete(&dtls_pvt.complete); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | dtls_pvt.response = NSS_TX_SUCCESS; |
| 241 | if (callback) { |
| 242 | callback(data, nim); |
| 243 | } |
| 244 | |
| 245 | complete(&dtls_pvt.complete); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * nss_dtls_tx_buf() |
| 250 | * Transmit buffer over DTLS interface |
| 251 | */ |
| 252 | nss_tx_status_t nss_dtls_tx_buf(struct sk_buff *skb, uint32_t if_num, |
| 253 | struct nss_ctx_instance *nss_ctx) |
| 254 | { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 255 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 256 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 257 | return nss_core_send_packet(nss_ctx, skb, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 258 | } |
| 259 | EXPORT_SYMBOL(nss_dtls_tx_buf); |
| 260 | |
| 261 | /* |
| 262 | * nss_dtls_tx_msg() |
| 263 | * Transmit a DTLS message to NSS firmware |
| 264 | */ |
| 265 | nss_tx_status_t nss_dtls_tx_msg(struct nss_ctx_instance *nss_ctx, |
| 266 | struct nss_dtls_msg *msg) |
| 267 | { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 268 | struct nss_cmn_msg *ncm = &msg->cm; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * Sanity check the message |
| 272 | */ |
| 273 | BUG_ON(!nss_dtls_verify_if_num(ncm->interface)); |
| 274 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 275 | if (ncm->type > NSS_DTLS_MSG_MAX) { |
| 276 | nss_warning("%p: dtls message type out of range: %d", |
| 277 | nss_ctx, ncm->type); |
| 278 | return NSS_TX_FAILURE; |
| 279 | } |
| 280 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 281 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 282 | } |
| 283 | EXPORT_SYMBOL(nss_dtls_tx_msg); |
| 284 | |
| 285 | /* |
| 286 | * nss_dtls_tx_msg() |
| 287 | * Transmit a DTLS message to NSS firmware synchronously. |
| 288 | */ |
| 289 | nss_tx_status_t nss_dtls_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_dtls_msg *msg) |
| 290 | { |
| 291 | |
| 292 | nss_tx_status_t status; |
| 293 | int ret = 0; |
| 294 | |
| 295 | down(&dtls_pvt.sem); |
| 296 | dtls_pvt.cb = (void *)msg->cm.cb; |
| 297 | dtls_pvt.app_data = (void *)msg->cm.app_data; |
| 298 | |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 299 | msg->cm.cb = (nss_ptr_t)nss_dtls_callback; |
| 300 | msg->cm.app_data = (nss_ptr_t)NULL; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 301 | |
| 302 | status = nss_dtls_tx_msg(nss_ctx, msg); |
| 303 | if (status != NSS_TX_SUCCESS) { |
| 304 | nss_warning("%p: dtls_tx_msg failed\n", nss_ctx); |
| 305 | up(&dtls_pvt.sem); |
| 306 | return status; |
| 307 | } |
| 308 | |
| 309 | ret = wait_for_completion_timeout(&dtls_pvt.complete, msecs_to_jiffies(NSS_DTLS_TX_TIMEOUT)); |
| 310 | |
| 311 | if (!ret) { |
| 312 | nss_warning("%p: DTLS msg tx failed due to timeout\n", nss_ctx); |
| 313 | dtls_pvt.response = NSS_TX_FAILURE; |
| 314 | } |
| 315 | |
| 316 | status = dtls_pvt.response; |
| 317 | up(&dtls_pvt.sem); |
| 318 | return status; |
| 319 | } |
| 320 | EXPORT_SYMBOL(nss_dtls_tx_msg_sync); |
| 321 | |
| 322 | /* |
| 323 | *********************************** |
| 324 | * Register/Unregister/Miscellaneous APIs |
| 325 | *********************************** |
| 326 | */ |
| 327 | |
| 328 | /* |
| 329 | * nss_dtls_register_if() |
| 330 | */ |
| 331 | struct nss_ctx_instance *nss_dtls_register_if(uint32_t if_num, |
| 332 | nss_dtls_data_callback_t cb, |
| 333 | nss_dtls_msg_callback_t ev_cb, |
| 334 | struct net_device *netdev, |
| 335 | uint32_t features, |
| 336 | void *app_ctx) |
| 337 | { |
| 338 | int32_t i; |
| 339 | |
| 340 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
| 341 | |
| 342 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 343 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 344 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 345 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 346 | if (!session_stats[i].valid) { |
| 347 | session_stats[i].valid = true; |
| 348 | session_stats[i].if_num = if_num; |
| 349 | session_stats[i].if_index = netdev->ifindex; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 353 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 354 | |
| 355 | if (i == NSS_MAX_DTLS_SESSIONS) { |
| 356 | nss_warning("%p: Cannot find free slot for " |
| 357 | "DTLS session stats, I/F:%u\n", nss_ctx, if_num); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 361 | if (nss_ctx->subsys_dp_register[if_num].ndev) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 362 | nss_warning("%p: Cannot find free slot for " |
| 363 | "DTLS NSS I/F:%u\n", nss_ctx, if_num); |
| 364 | |
| 365 | return NULL; |
| 366 | } |
| 367 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 368 | nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, app_ctx, netdev, features); |
Tanmay V Jagdale | a8533d5 | 2018-04-24 21:40:02 +0530 | [diff] [blame] | 369 | nss_ctx->subsys_dp_register[if_num].type = NSS_DYNAMIC_INTERFACE_TYPE_DTLS; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 370 | |
| 371 | nss_top_main.dtls_msg_callback = ev_cb; |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 372 | nss_core_register_handler(nss_ctx, if_num, nss_dtls_handler, app_ctx); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 373 | |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 374 | return nss_ctx; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 375 | } |
| 376 | EXPORT_SYMBOL(nss_dtls_register_if); |
| 377 | |
| 378 | /* |
| 379 | * nss_dtls_unregister_if() |
| 380 | */ |
| 381 | void nss_dtls_unregister_if(uint32_t if_num) |
| 382 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 383 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 384 | int32_t i; |
| 385 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 386 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 387 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 388 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 389 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 390 | if (session_stats[i].if_num == if_num) { |
| 391 | memset(&session_stats[i], 0, |
| 392 | sizeof(struct nss_dtls_stats_session)); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 396 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 397 | |
| 398 | if (i == NSS_MAX_DTLS_SESSIONS) { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 399 | nss_warning("%p: Cannot find debug stats for DTLS session %d\n", nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 403 | if (!nss_ctx->subsys_dp_register[if_num].ndev) { |
| 404 | nss_warning("%p: Cannot find registered netdev for DTLS NSS I/F:%u\n", nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 405 | |
| 406 | return; |
| 407 | } |
| 408 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 409 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 410 | |
| 411 | nss_top_main.dtls_msg_callback = NULL; |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 412 | nss_core_unregister_handler(nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 413 | } |
| 414 | EXPORT_SYMBOL(nss_dtls_unregister_if); |
| 415 | |
| 416 | /* |
| 417 | * nss_get_dtls_context() |
| 418 | */ |
| 419 | struct nss_ctx_instance *nss_dtls_get_context(void) |
| 420 | { |
| 421 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.dtls_handler_id]; |
| 422 | } |
| 423 | EXPORT_SYMBOL(nss_dtls_get_context); |
| 424 | |
| 425 | /* |
| 426 | * nss_dtls_msg_init() |
| 427 | * Initialize nss_dtls msg. |
| 428 | */ |
| 429 | void nss_dtls_msg_init(struct nss_dtls_msg *ncm, uint16_t if_num, |
| 430 | uint32_t type, uint32_t len, void *cb, void *app_data) |
| 431 | { |
| 432 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 433 | } |
| 434 | EXPORT_SYMBOL(nss_dtls_msg_init); |
| 435 | |
| 436 | /* |
| 437 | * nss_dtls_get_ifnum_with_coreid() |
| 438 | */ |
| 439 | int32_t nss_dtls_get_ifnum_with_coreid(int32_t if_num) |
| 440 | { |
| 441 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
| 442 | |
| 443 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 444 | return NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num); |
| 445 | } |
| 446 | EXPORT_SYMBOL(nss_dtls_get_ifnum_with_coreid); |
| 447 | |
| 448 | /* |
| 449 | * nss_dtls_register_handler() |
| 450 | */ |
| 451 | void nss_dtls_register_handler(void) |
| 452 | { |
| 453 | sema_init(&dtls_pvt.sem, 1); |
| 454 | init_completion(&dtls_pvt.complete); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 455 | |
| 456 | nss_dtls_stats_dentry_create(); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 457 | } |