ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved. |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +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 <linux/l2tp.h> |
| 18 | #include <net/sock.h> |
| 19 | #include "nss_tx_rx_common.h" |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 20 | #include "nss_l2tpv2_stats.h" |
Sachin Shashidhar | 015c28a | 2018-08-06 19:11:00 -0700 | [diff] [blame] | 21 | #include "nss_l2tpv2_log.h" |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Data structures to store l2tpv2 nss debug stats |
| 25 | */ |
| 26 | static DEFINE_SPINLOCK(nss_l2tpv2_session_debug_stats_lock); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 27 | static struct nss_l2tpv2_stats_session_debug nss_l2tpv2_session_debug_stats[NSS_MAX_L2TPV2_DYNAMIC_INTERFACES]; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * nss_l2tpv2_session_debug_stats_sync |
| 31 | * Per session debug stats for l2tpv2 |
| 32 | */ |
| 33 | void nss_l2tpv2_session_debug_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_l2tpv2_sync_session_stats_msg *stats_msg, uint16_t if_num) |
| 34 | { |
| 35 | int i; |
| 36 | spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 37 | for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) { |
| 38 | if (nss_l2tpv2_session_debug_stats[i].if_num == if_num) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 39 | nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_RX_PPP_LCP_PKTS] += stats_msg->debug_stats.rx_ppp_lcp_pkts; |
| 40 | nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_RX_EXP_DATA_PKTS] += stats_msg->debug_stats.rx_exception_data_pkts; |
| 41 | nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_ENCAP_PBUF_ALLOC_FAIL_PKTS] += stats_msg->debug_stats.encap_pbuf_alloc_fail; |
| 42 | nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_DECAP_PBUF_ALLOC_FAIL_PKTS] += stats_msg->debug_stats.decap_pbuf_alloc_fail; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 43 | break; |
| 44 | } |
| 45 | } |
| 46 | spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * nss_l2tpv2_global_session_stats_get() |
| 51 | * Get session l2tpv2 statitics. |
| 52 | */ |
| 53 | void nss_l2tpv2_session_debug_stats_get(void *stats_mem) |
| 54 | { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 55 | struct nss_l2tpv2_stats_session_debug *stats = (struct nss_l2tpv2_stats_session_debug *)stats_mem; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 56 | int i; |
| 57 | |
| 58 | if (!stats) { |
| 59 | nss_warning("No memory to copy l2tpv2 session stats"); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 64 | for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) { |
| 65 | if (nss_l2tpv2_session_debug_stats[i].valid) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 66 | memcpy(stats, &nss_l2tpv2_session_debug_stats[i], sizeof(struct nss_l2tpv2_stats_session_debug)); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 67 | stats++; |
| 68 | } |
| 69 | } |
| 70 | spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * nss_l2tpv2_handler() |
| 75 | * Handle NSS -> HLOS messages for l2tpv2 tunnel |
| 76 | */ |
| 77 | |
| 78 | static void nss_l2tpv2_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 79 | { |
| 80 | struct nss_l2tpv2_msg *ntm = (struct nss_l2tpv2_msg *)ncm; |
| 81 | void *ctx; |
| 82 | |
| 83 | nss_l2tpv2_msg_callback_t cb; |
| 84 | |
| 85 | BUG_ON(!(nss_is_dynamic_interface(ncm->interface) || ncm->interface == NSS_L2TPV2_INTERFACE)); |
| 86 | |
| 87 | /* |
Sachin Shashidhar | 015c28a | 2018-08-06 19:11:00 -0700 | [diff] [blame] | 88 | * Trace Messages |
| 89 | */ |
| 90 | nss_l2tpv2_log_rx_msg(ntm); |
| 91 | |
| 92 | /* |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 93 | * Is this a valid request/response packet? |
| 94 | */ |
| 95 | if (ncm->type >= NSS_L2TPV2_MSG_MAX) { |
| 96 | nss_warning("%p: received invalid message %d for L2TP interface", nss_ctx, ncm->type); |
| 97 | return; |
| 98 | } |
| 99 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 100 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_l2tpv2_msg)) { |
| 101 | nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | switch (ntm->cm.type) { |
| 106 | |
| 107 | case NSS_L2TPV2_MSG_SYNC_STATS: |
| 108 | /* |
| 109 | * session debug stats embeded in session stats msg |
| 110 | */ |
| 111 | nss_l2tpv2_session_debug_stats_sync(nss_ctx, &ntm->msg.stats, ncm->interface); |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Update the callback and app_data for NOTIFY messages, l2tpv2 sends all notify messages |
| 117 | * to the same callback/app_data. |
| 118 | */ |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame] | 119 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 120 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->l2tpv2_msg_callback; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Log failures |
| 125 | */ |
| 126 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 127 | |
| 128 | /* |
| 129 | * Do we have a call back |
| 130 | */ |
| 131 | if (!ncm->cb) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * callback |
| 137 | */ |
| 138 | cb = (nss_l2tpv2_msg_callback_t)ncm->cb; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 139 | ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * call l2tpv2 tunnel callback |
| 143 | */ |
| 144 | if (!ctx) { |
| 145 | nss_warning("%p: Event received for l2tpv2 tunnel interface %d before registration", nss_ctx, ncm->interface); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | cb(ctx, ntm); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * nss_l2tpv2_tx() |
| 154 | * Transmit a l2tpv2 message to NSS firmware |
| 155 | */ |
| 156 | nss_tx_status_t nss_l2tpv2_tx(struct nss_ctx_instance *nss_ctx, struct nss_l2tpv2_msg *msg) |
| 157 | { |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 158 | struct nss_cmn_msg *ncm = &msg->cm; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 159 | |
| 160 | /* |
Sachin Shashidhar | 015c28a | 2018-08-06 19:11:00 -0700 | [diff] [blame] | 161 | * Trace Messages |
| 162 | */ |
| 163 | nss_l2tpv2_log_tx_msg(msg); |
| 164 | |
| 165 | /* |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 166 | * Sanity check the message |
| 167 | */ |
| 168 | if (!nss_is_dynamic_interface(ncm->interface)) { |
| 169 | nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface); |
| 170 | return NSS_TX_FAILURE; |
| 171 | } |
| 172 | |
| 173 | if (ncm->type > NSS_L2TPV2_MSG_MAX) { |
| 174 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 175 | return NSS_TX_FAILURE; |
| 176 | } |
| 177 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 178 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | *********************************** |
| 183 | * Register/Unregister/Miscellaneous APIs |
| 184 | *********************************** |
| 185 | */ |
| 186 | |
| 187 | /* |
| 188 | * nss_register_l2tpv2_if() |
| 189 | */ |
| 190 | struct nss_ctx_instance *nss_register_l2tpv2_if(uint32_t if_num, nss_l2tpv2_callback_t l2tpv2_callback, |
| 191 | nss_l2tpv2_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 192 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 193 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id]; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 194 | int i = 0; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 195 | |
| 196 | nss_assert(nss_ctx); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 197 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 198 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 199 | nss_core_register_subsys_dp(nss_ctx, if_num, l2tpv2_callback, NULL, NULL, netdev, features); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 200 | |
| 201 | nss_top_main.l2tpv2_msg_callback = event_callback; |
| 202 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 203 | nss_core_register_handler(nss_ctx, if_num, nss_l2tpv2_handler, NULL); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 204 | |
| 205 | spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 206 | for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) { |
| 207 | if (!nss_l2tpv2_session_debug_stats[i].valid) { |
| 208 | nss_l2tpv2_session_debug_stats[i].valid = true; |
| 209 | nss_l2tpv2_session_debug_stats[i].if_num = if_num; |
| 210 | nss_l2tpv2_session_debug_stats[i].if_index = netdev->ifindex; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 215 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 216 | return nss_ctx; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * nss_unregister_l2tpv2_if() |
| 221 | */ |
| 222 | void nss_unregister_l2tpv2_if(uint32_t if_num) |
| 223 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 224 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id]; |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 225 | int i; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 226 | |
| 227 | nss_assert(nss_ctx); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 228 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 229 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 230 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 231 | |
| 232 | nss_top_main.l2tpv2_msg_callback = NULL; |
| 233 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 234 | nss_core_unregister_handler(nss_ctx, if_num); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 235 | |
| 236 | spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 237 | for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) { |
| 238 | if (nss_l2tpv2_session_debug_stats[i].if_num == if_num) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 239 | memset(&nss_l2tpv2_session_debug_stats[i], 0, sizeof(struct nss_l2tpv2_stats_session_debug)); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | } |
| 243 | spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * nss_get_l2tpv2_context() |
| 248 | */ |
| 249 | struct nss_ctx_instance *nss_l2tpv2_get_context() |
| 250 | { |
| 251 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id]; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * nss_l2tpv2_msg_init() |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 256 | * Initialize nss_l2tpv2 msg. |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 257 | */ |
| 258 | void nss_l2tpv2_msg_init(struct nss_l2tpv2_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data) |
| 259 | { |
| 260 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 261 | } |
| 262 | |
| 263 | /* nss_l2tpv2_register_handler() |
| 264 | * debugfs stats msg handler received on static l2tpv2 interface |
| 265 | */ |
| 266 | void nss_l2tpv2_register_handler(void) |
| 267 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 268 | struct nss_ctx_instance *nss_ctx = nss_l2tpv2_get_context(); |
| 269 | |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 270 | nss_info("nss_l2tpv2_register_handler"); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 271 | nss_core_register_handler(nss_ctx, NSS_L2TPV2_INTERFACE, nss_l2tpv2_handler, NULL); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 272 | |
| 273 | nss_l2tpv2_stats_dentry_create(); |
ratheesh kannoth | 7af985d | 2015-06-24 15:08:40 +0530 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | EXPORT_SYMBOL(nss_l2tpv2_get_context); |
| 277 | EXPORT_SYMBOL(nss_l2tpv2_tx); |
| 278 | EXPORT_SYMBOL(nss_unregister_l2tpv2_if); |
| 279 | EXPORT_SYMBOL(nss_l2tpv2_msg_init); |
| 280 | EXPORT_SYMBOL(nss_register_l2tpv2_if); |