Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Murat Sezgin | d80f3bd | 2014-12-10 15:38:06 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved. |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +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 | /* |
| 18 | * nss_pppoe.c |
| 19 | * NSS PPPoE APIs |
| 20 | */ |
| 21 | |
| 22 | #include "nss_tx_rx_common.h" |
Murat Sezgin | d80f3bd | 2014-12-10 15:38:06 -0800 | [diff] [blame] | 23 | #include <linux/if_pppox.h> |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 24 | |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 25 | /* |
| 26 | * nss_pppoe_tx() |
| 27 | * Transmit an PPPoe message to the FW. |
| 28 | */ |
| 29 | nss_tx_status_t nss_pppoe_tx(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_msg *nim) |
| 30 | { |
| 31 | struct nss_pppoe_msg *nim2; |
| 32 | struct nss_cmn_msg *ncm = &nim->cm; |
| 33 | struct sk_buff *nbuf; |
| 34 | int32_t status; |
| 35 | |
| 36 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 37 | |
| 38 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 39 | nss_warning("%p: pppoe msg dropped as core not ready", nss_ctx); |
| 40 | return NSS_TX_FAILURE_NOT_READY; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Sanity check the message |
| 45 | */ |
| 46 | if (ncm->interface != NSS_PPPOE_RX_INTERFACE) { |
| 47 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 48 | return NSS_TX_FAILURE; |
| 49 | } |
| 50 | |
| 51 | if (ncm->type > NSS_PPPOE_MAX) { |
| 52 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 53 | return NSS_TX_FAILURE; |
| 54 | } |
| 55 | |
| 56 | if (ncm->len > sizeof(struct nss_pppoe_msg)) { |
| 57 | nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len); |
| 58 | return NSS_TX_FAILURE; |
| 59 | } |
| 60 | |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 61 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 62 | if (unlikely(!nbuf)) { |
| 63 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 64 | nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++; |
| 65 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 66 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 67 | return NSS_TX_FAILURE; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Copy the message to our skb. |
| 72 | */ |
| 73 | nim2 = (struct nss_pppoe_msg *)skb_put(nbuf, sizeof(struct nss_pppoe_msg)); |
| 74 | memcpy(nim2, nim, sizeof(struct nss_pppoe_msg)); |
| 75 | |
| 76 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 77 | if (status != NSS_CORE_STATUS_SUCCESS) { |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 78 | dev_kfree_skb_any(nbuf); |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 79 | nss_warning("%p: unable to enqueue PPPoE msg\n", nss_ctx); |
| 80 | return NSS_TX_FAILURE; |
| 81 | } |
| 82 | |
| 83 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 84 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 85 | |
| 86 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 87 | |
| 88 | return NSS_TX_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | ********************************** |
| 93 | Rx APIs |
| 94 | ********************************** |
| 95 | */ |
| 96 | |
| 97 | /* |
Murat Sezgin | 524f90c | 2014-11-13 17:39:17 -0800 | [diff] [blame] | 98 | * nss_pppoe_reset_session_stats() |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 99 | * Reset PPPoE stats when session is destroyed. |
| 100 | */ |
Murat Sezgin | 524f90c | 2014-11-13 17:39:17 -0800 | [diff] [blame] | 101 | static void nss_pppoe_reset_session_stats(struct nss_ctx_instance *nss_ctx) |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 102 | { |
| 103 | uint32_t i, j, k; |
| 104 | |
| 105 | /* |
| 106 | * Reset the PPPoE statistics. |
| 107 | */ |
| 108 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 109 | |
| 110 | /* |
| 111 | * TODO: Don't reset all the statistics. Reset only the destroyed session's stats. |
| 112 | */ |
| 113 | for (i = 0; i < NSS_MAX_PHYSICAL_INTERFACES; i++) { |
| 114 | for (j = 0; j < NSS_PPPOE_NUM_SESSION_PER_INTERFACE; j++) { |
| 115 | for (k = 0; k < NSS_PPPOE_EXCEPTION_EVENT_MAX; k++) { |
| 116 | nss_ctx->nss_top->stats_if_exception_pppoe[i][j][k] = 0; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 121 | /* |
| 122 | * TODO: Do we need to unregister the destroy method? The ppp_dev has already gone. |
| 123 | */ |
| 124 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * nss_pppoe_exception_stats_sync() |
| 129 | * Handle the syncing of PPPoE exception statistics. |
| 130 | */ |
| 131 | static void nss_pppoe_exception_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_conn_stats_sync_msg *npess) |
| 132 | { |
| 133 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 134 | uint32_t index = npess->index; |
| 135 | uint32_t interface_num = npess->interface_num; |
| 136 | uint32_t i; |
| 137 | |
| 138 | spin_lock_bh(&nss_top->stats_lock); |
| 139 | |
| 140 | if (interface_num >= NSS_MAX_PHYSICAL_INTERFACES) { |
Murat Sezgin | 524f90c | 2014-11-13 17:39:17 -0800 | [diff] [blame] | 141 | spin_unlock_bh(&nss_top->stats_lock); |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 142 | nss_warning("%p: Incorrect interface number %d for PPPoE exception stats", nss_ctx, interface_num); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * pppoe exception stats |
| 148 | */ |
| 149 | for (i = 0; i < NSS_PPPOE_EXCEPTION_EVENT_MAX; i++) { |
| 150 | nss_top->stats_if_exception_pppoe[interface_num][index][i] += npess->exception_events_pppoe[i]; |
| 151 | } |
| 152 | |
| 153 | spin_unlock_bh(&nss_top->stats_lock); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * nss_pppoe_node_stats_sync() |
| 158 | * Handle the syncing of PPPoE node statistics. |
| 159 | */ |
| 160 | static void nss_pppoe_node_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_node_stats_sync_msg *npess) |
| 161 | { |
| 162 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 163 | |
| 164 | spin_lock_bh(&nss_top->stats_lock); |
| 165 | |
| 166 | nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += npess->node_stats.rx_packets; |
| 167 | nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += npess->node_stats.rx_bytes; |
| 168 | nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += npess->node_stats.rx_dropped; |
| 169 | nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += npess->node_stats.tx_packets; |
| 170 | nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += npess->node_stats.tx_bytes; |
| 171 | |
| 172 | nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_REQUESTS] += npess->pppoe_session_create_requests; |
| 173 | nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_FAILURES] += npess->pppoe_session_create_failures; |
| 174 | nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_REQUESTS] += npess->pppoe_session_destroy_requests; |
| 175 | nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_MISSES] += npess->pppoe_session_destroy_misses; |
| 176 | |
| 177 | spin_unlock_bh(&nss_top->stats_lock); |
| 178 | } |
| 179 | |
| 180 | /* |
Sundarajan Srinivasan | f1e5746 | 2014-09-17 15:24:01 -0700 | [diff] [blame] | 181 | * nss_pppoe_destroy_connection_rule() |
| 182 | * Destroy PPoE connection rule associated with the session ID and remote server MAC address. |
| 183 | */ |
| 184 | |
| 185 | /* |
| 186 | * TODO: This API should be deprecated soon and removed. |
| 187 | */ |
| 188 | static void nss_pppoe_destroy_connection_rule(void *ctx, uint16_t pppoe_session_id, uint8_t *pppoe_remote_mac) |
| 189 | { |
| 190 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *) ctx; |
| 191 | struct nss_pppoe_msg npm; |
| 192 | struct nss_pppoe_rule_destroy_msg *nprd; |
| 193 | uint16_t *pppoe_remote_mac_uint16_t = (uint16_t *)pppoe_remote_mac; |
| 194 | int32_t status; |
| 195 | |
| 196 | /* |
| 197 | * TODO Remove this function once linux kernel directly calls nss_pppoe_tx() |
| 198 | */ |
| 199 | nss_info("%p: Destroy all PPPoE rules of session ID: %x remote MAC: %x:%x:%x:%x:%x:%x", nss_ctx, pppoe_session_id, |
| 200 | pppoe_remote_mac[0], pppoe_remote_mac[1], pppoe_remote_mac[2], |
| 201 | pppoe_remote_mac[3], pppoe_remote_mac[4], pppoe_remote_mac[5]); |
| 202 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 203 | nss_pppoe_msg_init(&npm, NSS_PPPOE_RX_INTERFACE, NSS_PPPOE_TX_CONN_RULE_DESTROY, |
Sundarajan Srinivasan | f1e5746 | 2014-09-17 15:24:01 -0700 | [diff] [blame] | 204 | sizeof(struct nss_pppoe_rule_destroy_msg), NULL, NULL); |
| 205 | |
| 206 | nprd = &npm.msg.pppoe_rule_destroy; |
| 207 | |
| 208 | nprd->pppoe_session_id = pppoe_session_id; |
| 209 | nprd->pppoe_remote_mac[0] = pppoe_remote_mac_uint16_t[0]; |
| 210 | nprd->pppoe_remote_mac[1] = pppoe_remote_mac_uint16_t[1]; |
| 211 | nprd->pppoe_remote_mac[2] = pppoe_remote_mac_uint16_t[2]; |
| 212 | |
| 213 | status = nss_pppoe_tx(nss_ctx, &npm); |
| 214 | if (status != NSS_TX_SUCCESS) { |
| 215 | nss_warning("%p: Not able to send destroy pppoe rule msg to NSS %x\n", nss_ctx, status); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 220 | * nss_pppoe_rule_create_success() |
| 221 | * Handle the PPPoE rule create success message. |
| 222 | */ |
| 223 | static void nss_pppoe_rule_create_success(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_rule_create_success_msg *pcs) |
| 224 | { |
Sundarajan Srinivasan | 4691ba6 | 2014-11-07 11:24:07 -0800 | [diff] [blame] | 225 | #if (NSS_PPP_SUPPORT == 1) |
Murat Sezgin | d80f3bd | 2014-12-10 15:38:06 -0800 | [diff] [blame] | 226 | struct net_device *ppp_dev = pppoe_get_and_hold_netdev_from_session_info(pcs->pppoe_session_id, pcs->pppoe_remote_mac); |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 227 | |
| 228 | if (!ppp_dev) { |
| 229 | nss_warning("%p: There is not any PPP devices with SID: %x remote MAC: %x:%x:%x:%x:%x:%x", nss_ctx, pcs->pppoe_session_id, |
| 230 | pcs->pppoe_remote_mac[0], pcs->pppoe_remote_mac[1], pcs->pppoe_remote_mac[2], |
| 231 | pcs->pppoe_remote_mac[3], pcs->pppoe_remote_mac[4], pcs->pppoe_remote_mac[5]); |
| 232 | |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * TODO Remove this registration once kernel directly calls nss_pppoe_tx(). |
| 238 | */ |
Sundarajan Srinivasan | f1e5746 | 2014-09-17 15:24:01 -0700 | [diff] [blame] | 239 | if (!ppp_register_destroy_method(ppp_dev, nss_pppoe_destroy_connection_rule, (void *)nss_ctx)) { |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 240 | nss_warning("%p: Failed to register destroy method", nss_ctx); |
| 241 | } |
| 242 | |
| 243 | dev_put(ppp_dev); |
Sundarajan Srinivasan | 4691ba6 | 2014-11-07 11:24:07 -0800 | [diff] [blame] | 244 | #endif |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * nss_pppoe_rx_msg_handler() |
| 249 | * Handle NSS -> HLOS messages for PPPoE |
| 250 | */ |
| 251 | static void nss_pppoe_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 252 | { |
| 253 | struct nss_pppoe_msg *nim = (struct nss_pppoe_msg *)ncm; |
| 254 | |
| 255 | BUG_ON(ncm->interface != NSS_PPPOE_RX_INTERFACE); |
| 256 | |
| 257 | /* |
| 258 | * Sanity check the message type |
| 259 | */ |
| 260 | if (ncm->type > NSS_PPPOE_MAX) { |
| 261 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (ncm->len > sizeof(struct nss_pppoe_msg)) { |
| 266 | nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Log failures |
| 272 | */ |
| 273 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 274 | |
| 275 | /* |
| 276 | * Handling PPPoE messages coming from NSS fw. |
| 277 | */ |
| 278 | switch (nim->cm.type) { |
| 279 | case NSS_PPPOE_RX_CONN_RULE_SUCCESS: |
| 280 | nss_pppoe_rule_create_success(nss_ctx, &nim->msg.pppoe_rule_create_success); |
| 281 | break; |
| 282 | |
| 283 | case NSS_PPPOE_RX_NODE_STATS_SYNC: |
| 284 | nss_pppoe_node_stats_sync(nss_ctx, &nim->msg.pppoe_node_stats_sync); |
| 285 | break; |
| 286 | |
| 287 | case NSS_PPPOE_RX_CONN_STATS_SYNC: |
| 288 | nss_pppoe_exception_stats_sync(nss_ctx, &nim->msg.pppoe_conn_stats_sync); |
| 289 | break; |
| 290 | |
| 291 | case NSS_PPPOE_TX_CONN_RULE_DESTROY: |
| 292 | if (ncm->response == NSS_CMN_RESPONSE_ACK) { |
Murat Sezgin | 524f90c | 2014-11-13 17:39:17 -0800 | [diff] [blame] | 293 | nss_pppoe_reset_session_stats(nss_ctx); |
Ankit Dhanuka | a1569ce | 2014-05-13 19:58:06 +0530 | [diff] [blame] | 294 | } |
| 295 | break; |
| 296 | |
| 297 | default: |
| 298 | nss_warning("%p: Received response %d for type %d, interface %d", |
| 299 | nss_ctx, ncm->response, ncm->type, ncm->interface); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * nss_pppoe_register_handler() |
| 305 | */ |
| 306 | void nss_pppoe_register_handler() |
| 307 | { |
| 308 | nss_core_register_handler(NSS_PPPOE_RX_INTERFACE, nss_pppoe_rx_msg_handler, NULL); |
| 309 | } |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * nss_pppoe_msg_init() |
| 313 | * Initialize pppoe message. |
| 314 | */ |
| 315 | void nss_pppoe_msg_init(struct nss_pppoe_msg *npm, uint16_t if_num, uint32_t type, uint32_t len, |
| 316 | void *cb, void *app_data) |
| 317 | { |
| 318 | nss_cmn_msg_init(&npm->cm, if_num, type, len, (void *)cb, app_data); |
| 319 | } |
| 320 | |