blob: fee2b674464d898f4e1b9f2ca950aab051866d22 [file] [log] [blame]
Thomas Wu68250352014-04-02 18:59:40 -07001/*
2 **************************************************************************
Suruchi Agarwalef8a8702016-01-08 12:40:08 -08003 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Thomas Wu68250352014-04-02 18:59:40 -07004 * 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 Sezgin5c8c7362014-09-02 17:58:21 -070018 * nss_ipv4.c
Thomas Wu68250352014-04-02 18:59:40 -070019 * NSS IPv4 APIs
20 */
Vijay Dewangan9db18752014-09-15 16:25:01 -070021#include <linux/sysctl.h>
Thomas Wu68250352014-04-02 18:59:40 -070022#include "nss_tx_rx_common.h"
Thomas Wu68250352014-04-02 18:59:40 -070023
Vijay Dewangan4861f4e2014-10-14 16:56:35 -070024int nss_ipv4_conn_cfg __read_mostly = NSS_DEFAULT_NUM_CONN;
25static struct nss_conn_cfg_pvt i4cfgp;
Vijay Dewangan9db18752014-09-15 16:25:01 -070026
Thomas Wu68250352014-04-02 18:59:40 -070027/*
Gareth Williams958aa822015-02-04 19:36:39 +000028 * nss_ipv4_max_conn_count()
29 * Return the maximum number of IPv4 connections that the NSS acceleration engine supports.
30 */
31int nss_ipv4_max_conn_count(void)
32{
Tushar Mathur55ba1302015-09-08 13:14:48 +053033 return nss_core_max_ipv4_conn_get();
Gareth Williams958aa822015-02-04 19:36:39 +000034}
35EXPORT_SYMBOL(nss_ipv4_max_conn_count);
36
37/*
Sol Kavy57d24b42014-04-05 13:45:36 -070038 * nss_ipv4_driver_conn_sync_update()
Thomas Wu68250352014-04-02 18:59:40 -070039 * Update driver specific information from the messsage.
40 */
Murat Sezgin0c0561d2014-04-09 18:55:58 -070041static void nss_ipv4_driver_conn_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs)
Thomas Wu68250352014-04-02 18:59:40 -070042{
Sol Kavy57d24b42014-04-05 13:45:36 -070043 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Sol Kavy57d24b42014-04-05 13:45:36 -070044
Thomas Wu68250352014-04-02 18:59:40 -070045 /*
46 * Update statistics maintained by NSS driver
47 */
48 spin_lock_bh(&nss_top->stats_lock);
Thomas Wu68250352014-04-02 18:59:40 -070049 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_PKTS] += nirs->flow_rx_packet_count + nirs->return_rx_packet_count;
50 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_BYTES] += nirs->flow_rx_byte_count + nirs->return_rx_byte_count;
51 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_PKTS] += nirs->flow_tx_packet_count + nirs->return_tx_packet_count;
52 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_BYTES] += nirs->flow_tx_byte_count + nirs->return_tx_byte_count;
Thomas Wu68250352014-04-02 18:59:40 -070053 spin_unlock_bh(&nss_top->stats_lock);
54}
Thomas Wu68250352014-04-02 18:59:40 -070055
56/*
Stephen Wang709f9b52015-07-07 14:48:35 -070057 * nss_ipv4_driver_conn_sync_many_update()
58 * Update driver specific information from the conn_sync_many messsage.
59 */
60static void nss_ipv4_driver_conn_sync_many_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync_many_msg *nicsm)
61{
62 int i;
63
64 /*
65 * Sanity check for the stats count
66 */
67 if (nicsm->count * sizeof(struct nss_ipv4_conn_sync) >= nicsm->size) {
68 nss_warning("%p: stats sync count %u exceeds the size of this msg %u", nss_ctx, nicsm->count, nicsm->size);
69 return;
70 }
71
72 for (i = 0; i < nicsm->count; i++) {
73 nss_ipv4_driver_conn_sync_update(nss_ctx, &nicsm->conn_sync[i]);
74 }
75}
76
77/*
Murat Sezgin0c0561d2014-04-09 18:55:58 -070078 * nss_ipv4_driver_node_sync_update)
79 * Update driver specific information from the messsage.
80 */
81static void nss_ipv4_driver_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_node_sync *nins)
82{
83 struct nss_top_instance *nss_top = nss_ctx->nss_top;
84 uint32_t i;
85
86 /*
87 * Update statistics maintained by NSS driver
88 */
89 spin_lock_bh(&nss_top->stats_lock);
90 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets;
91 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes;
92 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped;
93 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets;
94 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes;
95
Murat Sezgin0c0561d2014-04-09 18:55:58 -070096 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_REQUESTS] += nins->ipv4_connection_create_requests;
97 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_COLLISIONS] += nins->ipv4_connection_create_collisions;
98 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_connection_create_invalid_interface;
99 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_connection_destroy_requests;
100 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_MISSES] += nins->ipv4_connection_destroy_misses;
101 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_HITS] += nins->ipv4_connection_hash_hits;
102 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_REORDERS] += nins->ipv4_connection_hash_reorders;
103 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_FLUSHES] += nins->ipv4_connection_flushes;
104 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_EVICTIONS] += nins->ipv4_connection_evictions;
Selin Dag60ea2b22014-11-05 09:36:22 -0800105 nss_top->stats_ipv4[NSS_STATS_IPV4_FRAGMENTATIONS] += nins->ipv4_fragmentations;
Kiran Kumar C.S.Kfadf92d2015-06-26 12:48:33 +0530106 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_REQUESTS] += nins->ipv4_mc_connection_create_requests;
107 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_UPDATE_REQUESTS] += nins->ipv4_mc_connection_update_requests;
108 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_mc_connection_create_invalid_interface;
109 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_mc_connection_destroy_requests;
110 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_MISSES] += nins->ipv4_mc_connection_destroy_misses;
111 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_FLUSHES] += nins->ipv4_mc_connection_flushes;
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700112
113 for (i = 0; i < NSS_EXCEPTION_EVENT_IPV4_MAX; i++) {
114 nss_top->stats_if_exception_ipv4[i] += nins->exception_events[i];
115 }
116 spin_unlock_bh(&nss_top->stats_lock);
117}
118
119/*
Sol Kavy2783c072014-04-05 12:53:13 -0700120 * nss_ipv4_rx_msg_handler()
Thomas Wu68250352014-04-02 18:59:40 -0700121 * Handle NSS -> HLOS messages for IPv4 bridge/route
122 */
Sol Kavy2783c072014-04-05 12:53:13 -0700123static void nss_ipv4_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
Thomas Wu68250352014-04-02 18:59:40 -0700124{
125 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
Sol Kavy57d24b42014-04-05 13:45:36 -0700126 nss_ipv4_msg_callback_t cb;
Thomas Wu68250352014-04-02 18:59:40 -0700127
128 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
129
130 /*
131 * Sanity check the message type
132 */
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700133 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700134 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
135 return;
136 }
137
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800138 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
139 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700140 return;
141 }
142
Thomas Wu68250352014-04-02 18:59:40 -0700143 /*
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700144 * Trace messages.
Thomas Wu68250352014-04-02 18:59:40 -0700145 */
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700146 nss_ipv4_log_rx_msg(nim);
Thomas Wu68250352014-04-02 18:59:40 -0700147
Thomas Wu68250352014-04-02 18:59:40 -0700148 switch (nim->cm.type) {
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700149 case NSS_IPV4_RX_NODE_STATS_SYNC_MSG:
150 /*
151 * Update driver statistics on node sync.
152 */
153 nss_ipv4_driver_node_sync_update(nss_ctx, &nim->msg.node_stats);
154 break;
155
Thomas Wu68250352014-04-02 18:59:40 -0700156 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
Sol Kavy57d24b42014-04-05 13:45:36 -0700157 /*
158 * Update driver statistics on connection sync.
159 */
160 nss_ipv4_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats);
Sakthi Vignesh Radhakrishnan515f8c22014-06-21 15:04:19 -0700161 break;
Stephen Wang709f9b52015-07-07 14:48:35 -0700162
163 case NSS_IPV4_TX_CONN_STATS_SYNC_MANY_MSG:
164 /*
165 * Update driver statistics on connection sync many.
166 */
167 nss_ipv4_driver_conn_sync_many_update(nss_ctx, &nim->msg.conn_stats_many);
168 break;
Thomas Wu68250352014-04-02 18:59:40 -0700169 }
170
171 /*
Sol Kavy57d24b42014-04-05 13:45:36 -0700172 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
173 * to the same callback/app_data.
Thomas Wu68250352014-04-02 18:59:40 -0700174 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700175 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
176 ncm->cb = (uint32_t)nss_ctx->nss_top->ipv4_callback;
177 ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv4_ctx;
178 }
Sol Kavy2783c072014-04-05 12:53:13 -0700179
Thomas Wu68250352014-04-02 18:59:40 -0700180 /*
181 * Do we have a callback?
182 */
183 if (!ncm->cb) {
184 return;
185 }
186
187 /*
188 * Callback
189 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700190 cb = (nss_ipv4_msg_callback_t)ncm->cb;
Thomas Wu68250352014-04-02 18:59:40 -0700191 cb((void *)ncm->app_data, nim);
Thomas Wu68250352014-04-02 18:59:40 -0700192}
193
194/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700195 * nss_ipv4_tx_with_size()
196 * Transmit an ipv4 message to the FW with a specified size.
Thomas Wu68250352014-04-02 18:59:40 -0700197 */
Stephen Wang709f9b52015-07-07 14:48:35 -0700198nss_tx_status_t nss_ipv4_tx_with_size(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim, uint32_t size)
Thomas Wu68250352014-04-02 18:59:40 -0700199{
200 struct nss_ipv4_msg *nim2;
201 struct nss_cmn_msg *ncm = &nim->cm;
202 struct sk_buff *nbuf;
203 int32_t status;
204
205 NSS_VERIFY_CTX_MAGIC(nss_ctx);
206 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
207 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
208 return NSS_TX_FAILURE_NOT_READY;
209 }
210
211 /*
212 * Sanity check the message
213 */
214 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
215 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
216 return NSS_TX_FAILURE;
217 }
218
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700219 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700220 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
221 return NSS_TX_FAILURE;
222 }
223
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800224 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
225 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700226 return NSS_TX_FAILURE;
227 }
228
Stephen Wang709f9b52015-07-07 14:48:35 -0700229 if(size > PAGE_SIZE) {
230 nss_warning("%p: tx request size too large: %u", nss_ctx, size);
231 return NSS_TX_FAILURE;
232 }
233
234 nbuf = dev_alloc_skb(size);
Thomas Wu68250352014-04-02 18:59:40 -0700235 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800236 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Thomas Wu68250352014-04-02 18:59:40 -0700237 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
238 return NSS_TX_FAILURE;
239 }
240
241 /*
242 * Copy the message to our skb.
243 */
244 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, sizeof(struct nss_ipv4_msg));
245 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
246
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700247 /*
248 * Trace messages.
249 */
250 nss_ipv4_log_tx_msg(nim);
251
Thomas Wu68250352014-04-02 18:59:40 -0700252 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
253 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530254 dev_kfree_skb_any(nbuf);
Sol Kavycd1bd5c2014-04-04 11:09:44 -0700255 nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx);
Thomas Wu68250352014-04-02 18:59:40 -0700256 return NSS_TX_FAILURE;
257 }
258
259 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
260 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
261
262 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
263 return NSS_TX_SUCCESS;
264}
265
266/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700267 * nss_ipv4_tx()
268 * Transmit an ipv4 message to the FW.
269 */
270nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
271{
272 return nss_ipv4_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
273}
274
275/*
Thomas Wu68250352014-04-02 18:59:40 -0700276 **********************************
277 Register/Unregister/Miscellaneous APIs
278 **********************************
279 */
280
281/*
282 * nss_ipv4_notify_register()
283 * Register to received IPv4 events.
284 *
285 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
286 */
287struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
288{
289 /*
290 * TODO: We need to have a new array in support of the new API
291 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
292 */
293 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530294 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700295 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
296}
297
298/*
299 * nss_ipv4_notify_unregister()
300 * Unregister to received IPv4 events.
301 *
302 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
303 */
304void nss_ipv4_notify_unregister(void)
305{
306 nss_top_main.ipv4_callback = NULL;
307}
308
309/*
310 * nss_ipv4_get_mgr()
311 *
312 * TODO: This only suppports a single ipv4, do we ever want to support more?
313 */
314struct nss_ctx_instance *nss_ipv4_get_mgr(void)
315{
316 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
317}
318
319/*
320 * nss_ipv4_register_handler()
321 * Register our handler to receive messages for this interface
322 */
Sol Kavy2783c072014-04-05 12:53:13 -0700323void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700324{
Sol Kavy2783c072014-04-05 12:53:13 -0700325 if (nss_core_register_handler(NSS_IPV4_RX_INTERFACE, nss_ipv4_rx_msg_handler, NULL) != NSS_CORE_STATUS_SUCCESS) {
Thomas Wu68250352014-04-02 18:59:40 -0700326 nss_warning("IPv4 handler failed to register");
327 }
328}
329
Vijay Dewangan9db18752014-09-15 16:25:01 -0700330/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530331 * nss_ipv4_conn_cfg_process()
332 * Process request to configure number of ipv4 connections
333 */
334static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn,
335 void (*cfg_cb)(void *app_data, struct nss_ipv4_msg *nim))
336{
337 struct nss_ipv4_msg nim;
338 struct nss_ipv4_rule_conn_cfg_msg *nirccm;
339 nss_tx_status_t nss_tx_status;
340 uint32_t sum_of_conn;
341
342 /*
343 * The input should be multiple of 1024.
344 * Input for ipv4 and ipv6 sum together should not exceed 8k
345 * Min. value should be at least 256 connections. This is the
346 * minimum connections we will support for each of them.
347 */
348 sum_of_conn = conn + nss_ipv6_conn_cfg;
349 if ((conn & NSS_NUM_CONN_QUANTA_MASK) ||
350 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
351 (conn < NSS_MIN_NUM_CONN)) {
352 nss_warning("%p: input supported connections (%d) does not adhere\
353 specifications\n1) not multiple of 1024,\n2) is less than \
354 min val: %d, OR\n IPv4/6 total exceeds %d\n",
355 nss_ctx,
356 conn,
357 NSS_MIN_NUM_CONN,
358 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
359 return -EINVAL;
360 }
361
362 nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
363
364 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
365 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
366 sizeof(struct nss_ipv4_rule_conn_cfg_msg), cfg_cb, NULL);
367
368 nirccm = &nim.msg.rule_conn_cfg;
369 nirccm->num_conn = htonl(conn);
370 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
371
372 if (nss_tx_status != NSS_TX_SUCCESS) {
373 nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
374 nss_ctx,
375 conn);
376 return -EIO;
377 }
378
379 return 0;
380}
381
382/*
Vijay Dewangan9db18752014-09-15 16:25:01 -0700383 * nss_ipv4_conn_cfg_callback()
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700384 * call back function for the ipv4 connection configuration handler
Vijay Dewangan9db18752014-09-15 16:25:01 -0700385 */
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700386static void nss_ipv4_conn_cfg_callback(void *app_data, struct nss_ipv4_msg *nim)
Vijay Dewangan9db18752014-09-15 16:25:01 -0700387{
388
389 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
390 nss_warning("IPv4 connection configuration failed with error: %d\n", nim->cm.error);
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700391 /*
392 * Error, hence we are not updating the nss_ipv4_conn_cfg
393 * Restore the current_value to its previous state
394 */
Vijay Dewangan488e5372014-12-29 21:40:11 -0800395 i4cfgp.response = NSS_FAILURE;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700396 complete(&i4cfgp.complete);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700397 return;
398 }
399
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700400 /*
401 * Sucess at NSS FW, hence updating nss_ipv4_conn_cfg, with the valid value
402 * saved at the sysctl handler.
403 */
Vijay Dewangan9db18752014-09-15 16:25:01 -0700404 nss_info("IPv4 connection configuration success: %d\n", nim->cm.error);
Vijay Dewangan488e5372014-12-29 21:40:11 -0800405 i4cfgp.response = NSS_SUCCESS;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700406 complete(&i4cfgp.complete);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700407}
408
409/*
410 * nss_ipv4_conn_cfg_handler()
411 * Sets the number of connections for IPv4
412 */
Stephen Wang52e6d342016-03-29 15:02:33 -0700413static int nss_ipv4_conn_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
Vijay Dewangan9db18752014-09-15 16:25:01 -0700414{
415 struct nss_top_instance *nss_top = &nss_top_main;
416 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
Vijay Dewangan488e5372014-12-29 21:40:11 -0800417 int ret = NSS_FAILURE;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700418
419 /*
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700420 * Acquiring semaphore
421 */
422 down(&i4cfgp.sem);
423
424 /*
425 * Take snap shot of current value
426 */
427 i4cfgp.current_value = nss_ipv4_conn_cfg;
428
429 /*
430 * Write the variable with user input
431 */
432 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
433 if (ret || (!write)) {
434 up(&i4cfgp.sem);
435 return ret;
436 }
437
438 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530439 * Process request to change number of IPv4 connections
Vijay Dewangan9db18752014-09-15 16:25:01 -0700440 */
Tushar Mathura3c18932015-08-24 20:27:21 +0530441 ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg, nss_ipv4_conn_cfg_callback);
442 if (ret != 0) {
Stephen Wang06761022015-03-03 16:38:42 -0800443 goto failure;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700444 }
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700445
446 /*
447 * Blocking call, wait till we get ACK for this msg.
448 */
449 ret = wait_for_completion_timeout(&i4cfgp.complete, msecs_to_jiffies(NSS_CONN_CFG_TIMEOUT));
450 if (ret == 0) {
451 nss_warning("%p: Waiting for ack timed out\n", nss_ctx);
Stephen Wang06761022015-03-03 16:38:42 -0800452 goto failure;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700453 }
454
455 /*
456 * ACK/NACK received from NSS FW
457 * If ACK: Callback function will update nss_ipv4_conn_cfg with
458 * i4cfgp.num_conn_valid, which holds the user input
459 */
Vijay Dewangan488e5372014-12-29 21:40:11 -0800460 if (NSS_FAILURE == i4cfgp.response) {
Stephen Wang06761022015-03-03 16:38:42 -0800461 goto failure;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700462 }
463
464 up(&i4cfgp.sem);
Thomas Wu651b3902015-05-12 11:21:09 -0700465 return 0;
Stephen Wang06761022015-03-03 16:38:42 -0800466
467failure:
468 /*
469 * Restore the current_value to its previous state
470 */
471 nss_ipv4_conn_cfg = i4cfgp.current_value;
472 up(&i4cfgp.sem);
Thomas Wu651b3902015-05-12 11:21:09 -0700473 return -EINVAL;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700474}
475
Tushar Mathura3c18932015-08-24 20:27:21 +0530476/*
477 * nss_ipv4_update_conn_count_cb()
478 * call back function for the ipv4 connection count update handler
479 */
480static void nss_ipv4_update_conn_count_cb(void *app_data, struct nss_ipv4_msg *nim)
481{
482 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
483 nss_warning("IPv4 connection count update failed with error: %d\n", nim->cm.error);
484 return;
485 }
486
487 nss_warning("IPv4 connection count update success: %d\n", nim->cm.error);
488}
489
490/*
491 * nss_ipv4_update_conn_count()
492 * Sets the maximum number of connections for IPv4
493 */
494int nss_ipv4_update_conn_count(int ipv4_num_conn)
495{
496 struct nss_top_instance *nss_top = &nss_top_main;
497 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
498 int saved_nss_ipv4_conn_cfg = nss_ipv4_conn_cfg;
499 int ret = 0;
500
501 nss_ipv4_conn_cfg = ipv4_num_conn;
502
503 /*
504 * Process request to change number of IPv4 connections
505 */
506 ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg,
507 nss_ipv4_update_conn_count_cb);
508 if (ret != 0) {
509 nss_ipv4_conn_cfg = saved_nss_ipv4_conn_cfg;
510 return ret;
511 }
512
513 return 0;
514}
515
Stephen Wang52e6d342016-03-29 15:02:33 -0700516static struct ctl_table nss_ipv4_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700517 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700518 .procname = "ipv4_conn",
519 .data = &nss_ipv4_conn_cfg,
520 .maxlen = sizeof(int),
521 .mode = 0644,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700522 .proc_handler = &nss_ipv4_conn_cfg_handler,
523 },
524 { }
525};
526
Stephen Wang52e6d342016-03-29 15:02:33 -0700527static struct ctl_table nss_ipv4_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700528 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700529 .procname = "ipv4cfg",
530 .mode = 0555,
531 .child = nss_ipv4_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700532 },
533 { }
534};
535
536
Stephen Wang52e6d342016-03-29 15:02:33 -0700537static struct ctl_table nss_ipv4_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700538 {
539 .procname = "nss",
540 .mode = 0555,
541 .child = nss_ipv4_dir,
542 },
543 { }
544};
545
Stephen Wang52e6d342016-03-29 15:02:33 -0700546static struct ctl_table nss_ipv4_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700547 {
548 .procname = "dev",
549 .mode = 0555,
550 .child = nss_ipv4_root_dir,
551 },
552 { }
553};
554
555static struct ctl_table_header *nss_ipv4_header;
556
557/*
558 * nss_ipv4_register_sysctl()
559 * Register sysctl specific to ipv4
560 */
561void nss_ipv4_register_sysctl(void)
562{
Stephen Wang49b474b2016-03-25 10:40:30 -0700563 sema_init(&i4cfgp.sem, 1);
564 init_completion(&i4cfgp.complete);
565 i4cfgp.current_value = nss_ipv4_conn_cfg;
566
Vijay Dewangan9db18752014-09-15 16:25:01 -0700567 /*
568 * Register sysctl table.
569 */
570 nss_ipv4_header = register_sysctl_table(nss_ipv4_root);
571}
572
573/*
574 * nss_ipv4_unregister_sysctl()
575 * Unregister sysctl specific to ipv4
576 */
577void nss_ipv4_unregister_sysctl(void)
578{
579 /*
580 * Unregister sysctl table.
581 */
582 if (nss_ipv4_header) {
583 unregister_sysctl_table(nss_ipv4_header);
584 }
585}
586
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700587/*
588 * nss_ipv4_msg_init()
589 * Initialize IPv4 message.
590 */
591void nss_ipv4_msg_init(struct nss_ipv4_msg *nim, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800592 nss_ipv4_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700593{
594 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
595}
596
Thomas Wu68250352014-04-02 18:59:40 -0700597EXPORT_SYMBOL(nss_ipv4_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700598EXPORT_SYMBOL(nss_ipv4_tx_with_size);
Thomas Wu68250352014-04-02 18:59:40 -0700599EXPORT_SYMBOL(nss_ipv4_notify_register);
600EXPORT_SYMBOL(nss_ipv4_notify_unregister);
601EXPORT_SYMBOL(nss_ipv4_get_mgr);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700602EXPORT_SYMBOL(nss_ipv4_register_sysctl);
603EXPORT_SYMBOL(nss_ipv4_unregister_sysctl);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700604EXPORT_SYMBOL(nss_ipv4_msg_init);
Tushar Mathura3c18932015-08-24 20:27:21 +0530605EXPORT_SYMBOL(nss_ipv4_update_conn_count);