Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
| 3 | * Copyright (c) 2013, 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 | /* |
Murat Sezgin | 5c8c736 | 2014-09-02 17:58:21 -0700 | [diff] [blame^] | 18 | * nss_ipv4.c |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 19 | * NSS IPv4 APIs |
| 20 | */ |
| 21 | #include <linux/ppp_channel.h> |
| 22 | #include "nss_tx_rx_common.h" |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 23 | |
| 24 | extern void nss_rx_metadata_ipv4_rule_establish(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_rule_establish *nire); |
Sakthi Vignesh Radhakrishnan | 515f8c2 | 2014-06-21 15:04:19 -0700 | [diff] [blame] | 25 | extern void nss_rx_metadata_ipv4_create_response(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 26 | extern void nss_rx_ipv4_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs); |
| 27 | |
| 28 | /* |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 29 | * nss_ipv4_driver_conn_sync_update() |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 30 | * Update driver specific information from the messsage. |
| 31 | */ |
Murat Sezgin | 0c0561d | 2014-04-09 18:55:58 -0700 | [diff] [blame] | 32 | static void nss_ipv4_driver_conn_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs) |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 33 | { |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 34 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 35 | struct net_device *pppoe_dev = NULL; |
| 36 | |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Update statistics maintained by NSS driver |
| 39 | */ |
| 40 | spin_lock_bh(&nss_top->stats_lock); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 41 | nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_PKTS] += nirs->flow_rx_packet_count + nirs->return_rx_packet_count; |
| 42 | nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_BYTES] += nirs->flow_rx_byte_count + nirs->return_rx_byte_count; |
| 43 | nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_PKTS] += nirs->flow_tx_packet_count + nirs->return_tx_packet_count; |
| 44 | nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_BYTES] += nirs->flow_tx_byte_count + nirs->return_tx_byte_count; |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 45 | spin_unlock_bh(&nss_top->stats_lock); |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Update the PPPoE interface stats, if there is any PPPoE session on the interfaces. |
| 49 | */ |
| 50 | if (nirs->flow_pppoe_session_id) { |
| 51 | pppoe_dev = ppp_session_to_netdev(nirs->flow_pppoe_session_id, (uint8_t *)nirs->flow_pppoe_remote_mac); |
| 52 | if (pppoe_dev) { |
| 53 | ppp_update_stats(pppoe_dev, nirs->flow_rx_packet_count, nirs->flow_rx_byte_count, |
| 54 | nirs->flow_tx_packet_count, nirs->flow_tx_byte_count); |
| 55 | dev_put(pppoe_dev); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (nirs->return_pppoe_session_id) { |
| 60 | pppoe_dev = ppp_session_to_netdev(nirs->return_pppoe_session_id, (uint8_t *)nirs->return_pppoe_remote_mac); |
| 61 | if (pppoe_dev) { |
| 62 | ppp_update_stats(pppoe_dev, nirs->return_rx_packet_count, nirs->return_rx_byte_count, |
| 63 | nirs->return_tx_packet_count, nirs->return_tx_byte_count); |
| 64 | dev_put(pppoe_dev); |
| 65 | } |
| 66 | } |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 67 | } |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
Murat Sezgin | 0c0561d | 2014-04-09 18:55:58 -0700 | [diff] [blame] | 70 | * nss_ipv4_driver_node_sync_update) |
| 71 | * Update driver specific information from the messsage. |
| 72 | */ |
| 73 | static void nss_ipv4_driver_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_node_sync *nins) |
| 74 | { |
| 75 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 76 | uint32_t i; |
| 77 | |
| 78 | /* |
| 79 | * Update statistics maintained by NSS driver |
| 80 | */ |
| 81 | spin_lock_bh(&nss_top->stats_lock); |
| 82 | nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets; |
| 83 | nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes; |
| 84 | nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped; |
| 85 | nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets; |
| 86 | nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes; |
| 87 | |
Murat Sezgin | 0c0561d | 2014-04-09 18:55:58 -0700 | [diff] [blame] | 88 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_REQUESTS] += nins->ipv4_connection_create_requests; |
| 89 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_COLLISIONS] += nins->ipv4_connection_create_collisions; |
| 90 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_connection_create_invalid_interface; |
| 91 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_connection_destroy_requests; |
| 92 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_MISSES] += nins->ipv4_connection_destroy_misses; |
| 93 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_HITS] += nins->ipv4_connection_hash_hits; |
| 94 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_REORDERS] += nins->ipv4_connection_hash_reorders; |
| 95 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_FLUSHES] += nins->ipv4_connection_flushes; |
| 96 | nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_EVICTIONS] += nins->ipv4_connection_evictions; |
| 97 | |
| 98 | for (i = 0; i < NSS_EXCEPTION_EVENT_IPV4_MAX; i++) { |
| 99 | nss_top->stats_if_exception_ipv4[i] += nins->exception_events[i]; |
| 100 | } |
| 101 | spin_unlock_bh(&nss_top->stats_lock); |
| 102 | } |
| 103 | |
| 104 | /* |
Sol Kavy | 2783c07 | 2014-04-05 12:53:13 -0700 | [diff] [blame] | 105 | * nss_ipv4_rx_msg_handler() |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 106 | * Handle NSS -> HLOS messages for IPv4 bridge/route |
| 107 | */ |
Sol Kavy | 2783c07 | 2014-04-05 12:53:13 -0700 | [diff] [blame] | 108 | static void nss_ipv4_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 109 | { |
| 110 | struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm; |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 111 | nss_ipv4_msg_callback_t cb; |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 112 | |
| 113 | BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE); |
| 114 | |
| 115 | /* |
| 116 | * Sanity check the message type |
| 117 | */ |
Murat Sezgin | 5c8c736 | 2014-09-02 17:58:21 -0700 | [diff] [blame^] | 118 | if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) { |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 119 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (ncm->len > sizeof(struct nss_ipv4_msg)) { |
| 124 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 125 | return; |
| 126 | } |
| 127 | |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 128 | /* |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 129 | * Log failures |
| 130 | */ |
| 131 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 132 | |
| 133 | /* |
| 134 | * Handle deprecated messages. Eventually these messages should be removed. |
| 135 | */ |
| 136 | switch (nim->cm.type) { |
| 137 | case NSS_IPV4_RX_ESTABLISH_RULE_MSG: |
| 138 | return nss_rx_metadata_ipv4_rule_establish(nss_ctx, &nim->msg.rule_establish); |
| 139 | break; |
| 140 | |
Murat Sezgin | 0c0561d | 2014-04-09 18:55:58 -0700 | [diff] [blame] | 141 | case NSS_IPV4_RX_NODE_STATS_SYNC_MSG: |
| 142 | /* |
| 143 | * Update driver statistics on node sync. |
| 144 | */ |
| 145 | nss_ipv4_driver_node_sync_update(nss_ctx, &nim->msg.node_stats); |
| 146 | break; |
| 147 | |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 148 | case NSS_IPV4_RX_CONN_STATS_SYNC_MSG: |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Update driver statistics on connection sync. |
| 151 | */ |
| 152 | nss_ipv4_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats); |
Sol Kavy | 4013e28 | 2014-04-06 15:57:00 -0700 | [diff] [blame] | 153 | nss_rx_ipv4_sync(nss_ctx, &nim->msg.conn_stats); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 154 | break; |
Sakthi Vignesh Radhakrishnan | 515f8c2 | 2014-06-21 15:04:19 -0700 | [diff] [blame] | 155 | |
| 156 | case NSS_IPV4_TX_CREATE_RULE_MSG: |
| 157 | nss_rx_metadata_ipv4_create_response(nss_ctx, nim); |
| 158 | break; |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 162 | * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages |
| 163 | * to the same callback/app_data. |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 164 | */ |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 165 | if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) { |
| 166 | ncm->cb = (uint32_t)nss_ctx->nss_top->ipv4_callback; |
| 167 | ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv4_ctx; |
| 168 | } |
Sol Kavy | 2783c07 | 2014-04-05 12:53:13 -0700 | [diff] [blame] | 169 | |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Do we have a callback? |
| 172 | */ |
| 173 | if (!ncm->cb) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Callback |
| 179 | */ |
Sol Kavy | 57d24b4 | 2014-04-05 13:45:36 -0700 | [diff] [blame] | 180 | cb = (nss_ipv4_msg_callback_t)ncm->cb; |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 181 | cb((void *)ncm->app_data, nim); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * nss_ipv4_tx() |
| 186 | * Transmit an ipv4 message to the FW. |
| 187 | */ |
| 188 | nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim) |
| 189 | { |
| 190 | struct nss_ipv4_msg *nim2; |
| 191 | struct nss_cmn_msg *ncm = &nim->cm; |
| 192 | struct sk_buff *nbuf; |
| 193 | int32_t status; |
| 194 | |
| 195 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 196 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 197 | nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx); |
| 198 | return NSS_TX_FAILURE_NOT_READY; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Sanity check the message |
| 203 | */ |
| 204 | if (ncm->interface != NSS_IPV4_RX_INTERFACE) { |
| 205 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 206 | return NSS_TX_FAILURE; |
| 207 | } |
| 208 | |
Murat Sezgin | 5c8c736 | 2014-09-02 17:58:21 -0700 | [diff] [blame^] | 209 | if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) { |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 210 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 211 | return NSS_TX_FAILURE; |
| 212 | } |
| 213 | |
| 214 | if (ncm->len > sizeof(struct nss_ipv4_msg)) { |
| 215 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 216 | return NSS_TX_FAILURE; |
| 217 | } |
| 218 | |
Pamidipati, Vijay | ce98bbe | 2014-08-19 11:21:00 +0530 | [diff] [blame] | 219 | nbuf = nss_skb_alloc(NSS_NBUF_PAYLOAD_SIZE); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 220 | if (unlikely(!nbuf)) { |
| 221 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 222 | nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++; |
| 223 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 224 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 225 | return NSS_TX_FAILURE; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Copy the message to our skb. |
| 230 | */ |
| 231 | nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, sizeof(struct nss_ipv4_msg)); |
| 232 | memcpy(nim2, nim, sizeof(struct nss_ipv4_msg)); |
| 233 | |
| 234 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 235 | if (status != NSS_CORE_STATUS_SUCCESS) { |
Pamidipati, Vijay | ce98bbe | 2014-08-19 11:21:00 +0530 | [diff] [blame] | 236 | nss_skb_free(nbuf); |
Sol Kavy | cd1bd5c | 2014-04-04 11:09:44 -0700 | [diff] [blame] | 237 | nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 238 | return NSS_TX_FAILURE; |
| 239 | } |
| 240 | |
| 241 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 242 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 243 | |
| 244 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 245 | return NSS_TX_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ********************************** |
| 250 | Register/Unregister/Miscellaneous APIs |
| 251 | ********************************** |
| 252 | */ |
| 253 | |
| 254 | /* |
| 255 | * nss_ipv4_notify_register() |
| 256 | * Register to received IPv4 events. |
| 257 | * |
| 258 | * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core? |
| 259 | */ |
| 260 | struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data) |
| 261 | { |
| 262 | /* |
| 263 | * TODO: We need to have a new array in support of the new API |
| 264 | * TODO: If we use a per-context array, we would move the array into nss_ctx based. |
| 265 | */ |
| 266 | nss_top_main.ipv4_callback = cb; |
Abhishek Rastogi | e11f47b | 2014-04-04 18:43:32 +0530 | [diff] [blame] | 267 | nss_top_main.ipv4_ctx = app_data; |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 268 | return &nss_top_main.nss[nss_top_main.ipv4_handler_id]; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * nss_ipv4_notify_unregister() |
| 273 | * Unregister to received IPv4 events. |
| 274 | * |
| 275 | * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core? |
| 276 | */ |
| 277 | void nss_ipv4_notify_unregister(void) |
| 278 | { |
| 279 | nss_top_main.ipv4_callback = NULL; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * nss_ipv4_get_mgr() |
| 284 | * |
| 285 | * TODO: This only suppports a single ipv4, do we ever want to support more? |
| 286 | */ |
| 287 | struct nss_ctx_instance *nss_ipv4_get_mgr(void) |
| 288 | { |
| 289 | return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id]; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * nss_ipv4_register_handler() |
| 294 | * Register our handler to receive messages for this interface |
| 295 | */ |
Sol Kavy | 2783c07 | 2014-04-05 12:53:13 -0700 | [diff] [blame] | 296 | void nss_ipv4_register_handler(void) |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 297 | { |
Sol Kavy | 2783c07 | 2014-04-05 12:53:13 -0700 | [diff] [blame] | 298 | if (nss_core_register_handler(NSS_IPV4_RX_INTERFACE, nss_ipv4_rx_msg_handler, NULL) != NSS_CORE_STATUS_SUCCESS) { |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 299 | nss_warning("IPv4 handler failed to register"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | EXPORT_SYMBOL(nss_ipv4_tx); |
| 304 | EXPORT_SYMBOL(nss_ipv4_notify_register); |
| 305 | EXPORT_SYMBOL(nss_ipv4_notify_unregister); |
| 306 | EXPORT_SYMBOL(nss_ipv4_get_mgr); |