blob: f991919637a48416c826a10c8c2330ec4640d5a6 [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/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -070028 * Callback for conn_sync_many request message.
29 */
30nss_ipv4_msg_callback_t nss_ipv4_conn_sync_many_msg_cb = NULL;
31
32/*
Gareth Williams958aa822015-02-04 19:36:39 +000033 * nss_ipv4_max_conn_count()
34 * Return the maximum number of IPv4 connections that the NSS acceleration engine supports.
35 */
36int nss_ipv4_max_conn_count(void)
37{
Tushar Mathur55ba1302015-09-08 13:14:48 +053038 return nss_core_max_ipv4_conn_get();
Gareth Williams958aa822015-02-04 19:36:39 +000039}
40EXPORT_SYMBOL(nss_ipv4_max_conn_count);
41
42/*
Sol Kavy57d24b42014-04-05 13:45:36 -070043 * nss_ipv4_driver_conn_sync_update()
Thomas Wu68250352014-04-02 18:59:40 -070044 * Update driver specific information from the messsage.
45 */
Murat Sezgin0c0561d2014-04-09 18:55:58 -070046static 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 -070047{
Sol Kavy57d24b42014-04-05 13:45:36 -070048 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Sol Kavy57d24b42014-04-05 13:45:36 -070049
Thomas Wu68250352014-04-02 18:59:40 -070050 /*
51 * Update statistics maintained by NSS driver
52 */
53 spin_lock_bh(&nss_top->stats_lock);
Thomas Wu68250352014-04-02 18:59:40 -070054 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_PKTS] += nirs->flow_rx_packet_count + nirs->return_rx_packet_count;
55 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_BYTES] += nirs->flow_rx_byte_count + nirs->return_rx_byte_count;
56 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_PKTS] += nirs->flow_tx_packet_count + nirs->return_tx_packet_count;
57 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 -070058 spin_unlock_bh(&nss_top->stats_lock);
59}
Thomas Wu68250352014-04-02 18:59:40 -070060
61/*
Stephen Wang709f9b52015-07-07 14:48:35 -070062 * nss_ipv4_driver_conn_sync_many_update()
63 * Update driver specific information from the conn_sync_many messsage.
64 */
65static void nss_ipv4_driver_conn_sync_many_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync_many_msg *nicsm)
66{
67 int i;
68
69 /*
70 * Sanity check for the stats count
71 */
72 if (nicsm->count * sizeof(struct nss_ipv4_conn_sync) >= nicsm->size) {
73 nss_warning("%p: stats sync count %u exceeds the size of this msg %u", nss_ctx, nicsm->count, nicsm->size);
74 return;
75 }
76
77 for (i = 0; i < nicsm->count; i++) {
78 nss_ipv4_driver_conn_sync_update(nss_ctx, &nicsm->conn_sync[i]);
79 }
80}
81
82/*
Murat Sezgin0c0561d2014-04-09 18:55:58 -070083 * nss_ipv4_driver_node_sync_update)
84 * Update driver specific information from the messsage.
85 */
86static void nss_ipv4_driver_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_node_sync *nins)
87{
88 struct nss_top_instance *nss_top = nss_ctx->nss_top;
89 uint32_t i;
90
91 /*
92 * Update statistics maintained by NSS driver
93 */
94 spin_lock_bh(&nss_top->stats_lock);
95 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets;
96 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes;
97 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped;
98 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets;
99 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes;
100
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700101 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_REQUESTS] += nins->ipv4_connection_create_requests;
102 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_COLLISIONS] += nins->ipv4_connection_create_collisions;
103 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_connection_create_invalid_interface;
104 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_connection_destroy_requests;
105 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_MISSES] += nins->ipv4_connection_destroy_misses;
106 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_HITS] += nins->ipv4_connection_hash_hits;
107 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_REORDERS] += nins->ipv4_connection_hash_reorders;
108 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_FLUSHES] += nins->ipv4_connection_flushes;
109 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_EVICTIONS] += nins->ipv4_connection_evictions;
Selin Dag60ea2b22014-11-05 09:36:22 -0800110 nss_top->stats_ipv4[NSS_STATS_IPV4_FRAGMENTATIONS] += nins->ipv4_fragmentations;
Kiran Kumar C.S.Kfadf92d2015-06-26 12:48:33 +0530111 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_REQUESTS] += nins->ipv4_mc_connection_create_requests;
112 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_UPDATE_REQUESTS] += nins->ipv4_mc_connection_update_requests;
113 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_mc_connection_create_invalid_interface;
114 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_mc_connection_destroy_requests;
115 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_MISSES] += nins->ipv4_mc_connection_destroy_misses;
116 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_FLUSHES] += nins->ipv4_mc_connection_flushes;
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700117
118 for (i = 0; i < NSS_EXCEPTION_EVENT_IPV4_MAX; i++) {
119 nss_top->stats_if_exception_ipv4[i] += nins->exception_events[i];
120 }
121 spin_unlock_bh(&nss_top->stats_lock);
122}
123
124/*
Sol Kavy2783c072014-04-05 12:53:13 -0700125 * nss_ipv4_rx_msg_handler()
Thomas Wu68250352014-04-02 18:59:40 -0700126 * Handle NSS -> HLOS messages for IPv4 bridge/route
127 */
Sol Kavy2783c072014-04-05 12:53:13 -0700128static 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 -0700129{
130 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
Sol Kavy57d24b42014-04-05 13:45:36 -0700131 nss_ipv4_msg_callback_t cb;
Thomas Wu68250352014-04-02 18:59:40 -0700132
133 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
134
135 /*
136 * Sanity check the message type
137 */
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700138 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700139 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
140 return;
141 }
142
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800143 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
144 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700145 return;
146 }
147
Thomas Wu68250352014-04-02 18:59:40 -0700148 /*
Thomas Wu68250352014-04-02 18:59:40 -0700149 * Log failures
150 */
151 nss_core_log_msg_failures(nss_ctx, ncm);
152
Thomas Wu68250352014-04-02 18:59:40 -0700153 switch (nim->cm.type) {
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700154 case NSS_IPV4_RX_NODE_STATS_SYNC_MSG:
155 /*
156 * Update driver statistics on node sync.
157 */
158 nss_ipv4_driver_node_sync_update(nss_ctx, &nim->msg.node_stats);
159 break;
160
Thomas Wu68250352014-04-02 18:59:40 -0700161 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
Sol Kavy57d24b42014-04-05 13:45:36 -0700162 /*
163 * Update driver statistics on connection sync.
164 */
165 nss_ipv4_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats);
Sakthi Vignesh Radhakrishnan515f8c22014-06-21 15:04:19 -0700166 break;
Stephen Wang709f9b52015-07-07 14:48:35 -0700167
168 case NSS_IPV4_TX_CONN_STATS_SYNC_MANY_MSG:
169 /*
170 * Update driver statistics on connection sync many.
171 */
172 nss_ipv4_driver_conn_sync_many_update(nss_ctx, &nim->msg.conn_stats_many);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700173 ncm->cb = (uint32_t)nss_ipv4_conn_sync_many_msg_cb;
Stephen Wang709f9b52015-07-07 14:48:35 -0700174 break;
Thomas Wu68250352014-04-02 18:59:40 -0700175 }
176
177 /*
Sol Kavy57d24b42014-04-05 13:45:36 -0700178 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
179 * to the same callback/app_data.
Thomas Wu68250352014-04-02 18:59:40 -0700180 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700181 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
182 ncm->cb = (uint32_t)nss_ctx->nss_top->ipv4_callback;
183 ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv4_ctx;
184 }
Sol Kavy2783c072014-04-05 12:53:13 -0700185
Thomas Wu68250352014-04-02 18:59:40 -0700186 /*
187 * Do we have a callback?
188 */
189 if (!ncm->cb) {
190 return;
191 }
192
193 /*
194 * Callback
195 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700196 cb = (nss_ipv4_msg_callback_t)ncm->cb;
Thomas Wu68250352014-04-02 18:59:40 -0700197 cb((void *)ncm->app_data, nim);
Thomas Wu68250352014-04-02 18:59:40 -0700198}
199
200/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700201 * nss_ipv4_tx_with_size()
202 * Transmit an ipv4 message to the FW with a specified size.
Thomas Wu68250352014-04-02 18:59:40 -0700203 */
Stephen Wang709f9b52015-07-07 14:48:35 -0700204nss_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 -0700205{
206 struct nss_ipv4_msg *nim2;
207 struct nss_cmn_msg *ncm = &nim->cm;
208 struct sk_buff *nbuf;
209 int32_t status;
210
211 NSS_VERIFY_CTX_MAGIC(nss_ctx);
212 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
213 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
214 return NSS_TX_FAILURE_NOT_READY;
215 }
216
217 /*
218 * Sanity check the message
219 */
220 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
221 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
222 return NSS_TX_FAILURE;
223 }
224
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700225 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700226 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
227 return NSS_TX_FAILURE;
228 }
229
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800230 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
231 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700232 return NSS_TX_FAILURE;
233 }
234
Stephen Wang709f9b52015-07-07 14:48:35 -0700235 if(size > PAGE_SIZE) {
236 nss_warning("%p: tx request size too large: %u", nss_ctx, size);
237 return NSS_TX_FAILURE;
238 }
239
240 nbuf = dev_alloc_skb(size);
Thomas Wu68250352014-04-02 18:59:40 -0700241 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800242 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 -0700243 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
244 return NSS_TX_FAILURE;
245 }
246
247 /*
248 * Copy the message to our skb.
249 */
250 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, sizeof(struct nss_ipv4_msg));
251 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
252
253 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
254 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530255 dev_kfree_skb_any(nbuf);
Sol Kavycd1bd5c2014-04-04 11:09:44 -0700256 nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx);
Thomas Wu68250352014-04-02 18:59:40 -0700257 return NSS_TX_FAILURE;
258 }
259
260 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
261 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
262
263 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
264 return NSS_TX_SUCCESS;
265}
266
267/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700268 * nss_ipv4_tx()
269 * Transmit an ipv4 message to the FW.
270 */
271nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
272{
273 return nss_ipv4_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
274}
275
276/*
Thomas Wu68250352014-04-02 18:59:40 -0700277 **********************************
278 Register/Unregister/Miscellaneous APIs
279 **********************************
280 */
281
282/*
283 * nss_ipv4_notify_register()
284 * Register to received IPv4 events.
285 *
286 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
287 */
288struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
289{
290 /*
291 * TODO: We need to have a new array in support of the new API
292 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
293 */
294 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530295 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700296 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
297}
298
299/*
300 * nss_ipv4_notify_unregister()
301 * Unregister to received IPv4 events.
302 *
303 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
304 */
305void nss_ipv4_notify_unregister(void)
306{
307 nss_top_main.ipv4_callback = NULL;
308}
309
310/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700311 * nss_ipv4_conn_sync_many_notify_register()
312 * Register to receive IPv4 conn_sync_many message response.
313 */
314void nss_ipv4_conn_sync_many_notify_register(nss_ipv4_msg_callback_t cb)
315{
316 nss_ipv4_conn_sync_many_msg_cb = cb;
317}
318
319/*
320 * nss_ipv4_conn_sync_many_notify_unregister()
321 * Unregister to receive IPv4 conn_sync_many message response.
322 */
323void nss_ipv4_conn_sync_many_notify_unregister(void)
324{
325 nss_ipv4_conn_sync_many_msg_cb = NULL;
326}
327
328/*
Thomas Wu68250352014-04-02 18:59:40 -0700329 * nss_ipv4_get_mgr()
330 *
331 * TODO: This only suppports a single ipv4, do we ever want to support more?
332 */
333struct nss_ctx_instance *nss_ipv4_get_mgr(void)
334{
335 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
336}
337
338/*
339 * nss_ipv4_register_handler()
340 * Register our handler to receive messages for this interface
341 */
Sol Kavy2783c072014-04-05 12:53:13 -0700342void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700343{
Sol Kavy2783c072014-04-05 12:53:13 -0700344 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 -0700345 nss_warning("IPv4 handler failed to register");
346 }
347}
348
Vijay Dewangan9db18752014-09-15 16:25:01 -0700349/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530350 * nss_ipv4_conn_cfg_process()
351 * Process request to configure number of ipv4 connections
352 */
353static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn,
354 void (*cfg_cb)(void *app_data, struct nss_ipv4_msg *nim))
355{
356 struct nss_ipv4_msg nim;
357 struct nss_ipv4_rule_conn_cfg_msg *nirccm;
358 nss_tx_status_t nss_tx_status;
359 uint32_t sum_of_conn;
360
361 /*
362 * The input should be multiple of 1024.
363 * Input for ipv4 and ipv6 sum together should not exceed 8k
364 * Min. value should be at least 256 connections. This is the
365 * minimum connections we will support for each of them.
366 */
367 sum_of_conn = conn + nss_ipv6_conn_cfg;
368 if ((conn & NSS_NUM_CONN_QUANTA_MASK) ||
369 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
370 (conn < NSS_MIN_NUM_CONN)) {
371 nss_warning("%p: input supported connections (%d) does not adhere\
372 specifications\n1) not multiple of 1024,\n2) is less than \
373 min val: %d, OR\n IPv4/6 total exceeds %d\n",
374 nss_ctx,
375 conn,
376 NSS_MIN_NUM_CONN,
377 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
378 return -EINVAL;
379 }
380
381 nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
382
383 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
384 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
385 sizeof(struct nss_ipv4_rule_conn_cfg_msg), cfg_cb, NULL);
386
387 nirccm = &nim.msg.rule_conn_cfg;
388 nirccm->num_conn = htonl(conn);
389 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
390
391 if (nss_tx_status != NSS_TX_SUCCESS) {
392 nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
393 nss_ctx,
394 conn);
395 return -EIO;
396 }
397
398 return 0;
399}
400
401/*
Vijay Dewangan9db18752014-09-15 16:25:01 -0700402 * nss_ipv4_conn_cfg_callback()
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700403 * call back function for the ipv4 connection configuration handler
Vijay Dewangan9db18752014-09-15 16:25:01 -0700404 */
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700405static void nss_ipv4_conn_cfg_callback(void *app_data, struct nss_ipv4_msg *nim)
Vijay Dewangan9db18752014-09-15 16:25:01 -0700406{
407
408 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
409 nss_warning("IPv4 connection configuration failed with error: %d\n", nim->cm.error);
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700410 /*
411 * Error, hence we are not updating the nss_ipv4_conn_cfg
412 * Restore the current_value to its previous state
413 */
Vijay Dewangan488e5372014-12-29 21:40:11 -0800414 i4cfgp.response = NSS_FAILURE;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700415 complete(&i4cfgp.complete);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700416 return;
417 }
418
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700419 /*
420 * Sucess at NSS FW, hence updating nss_ipv4_conn_cfg, with the valid value
421 * saved at the sysctl handler.
422 */
Vijay Dewangan9db18752014-09-15 16:25:01 -0700423 nss_info("IPv4 connection configuration success: %d\n", nim->cm.error);
Vijay Dewangan488e5372014-12-29 21:40:11 -0800424 i4cfgp.response = NSS_SUCCESS;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700425 complete(&i4cfgp.complete);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700426}
427
428/*
429 * nss_ipv4_conn_cfg_handler()
430 * Sets the number of connections for IPv4
431 */
Stephen Wang52e6d342016-03-29 15:02:33 -0700432static 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 -0700433{
434 struct nss_top_instance *nss_top = &nss_top_main;
435 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
Vijay Dewangan488e5372014-12-29 21:40:11 -0800436 int ret = NSS_FAILURE;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700437
438 /*
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700439 * Acquiring semaphore
440 */
441 down(&i4cfgp.sem);
442
443 /*
444 * Take snap shot of current value
445 */
446 i4cfgp.current_value = nss_ipv4_conn_cfg;
447
448 /*
449 * Write the variable with user input
450 */
451 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
452 if (ret || (!write)) {
453 up(&i4cfgp.sem);
454 return ret;
455 }
456
457 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530458 * Process request to change number of IPv4 connections
Vijay Dewangan9db18752014-09-15 16:25:01 -0700459 */
Tushar Mathura3c18932015-08-24 20:27:21 +0530460 ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg, nss_ipv4_conn_cfg_callback);
461 if (ret != 0) {
Stephen Wang06761022015-03-03 16:38:42 -0800462 goto failure;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700463 }
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700464
465 /*
466 * Blocking call, wait till we get ACK for this msg.
467 */
468 ret = wait_for_completion_timeout(&i4cfgp.complete, msecs_to_jiffies(NSS_CONN_CFG_TIMEOUT));
469 if (ret == 0) {
470 nss_warning("%p: Waiting for ack timed out\n", nss_ctx);
Stephen Wang06761022015-03-03 16:38:42 -0800471 goto failure;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700472 }
473
474 /*
475 * ACK/NACK received from NSS FW
476 * If ACK: Callback function will update nss_ipv4_conn_cfg with
477 * i4cfgp.num_conn_valid, which holds the user input
478 */
Vijay Dewangan488e5372014-12-29 21:40:11 -0800479 if (NSS_FAILURE == i4cfgp.response) {
Stephen Wang06761022015-03-03 16:38:42 -0800480 goto failure;
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700481 }
482
483 up(&i4cfgp.sem);
Thomas Wu651b3902015-05-12 11:21:09 -0700484 return 0;
Stephen Wang06761022015-03-03 16:38:42 -0800485
486failure:
487 /*
488 * Restore the current_value to its previous state
489 */
490 nss_ipv4_conn_cfg = i4cfgp.current_value;
491 up(&i4cfgp.sem);
Thomas Wu651b3902015-05-12 11:21:09 -0700492 return -EINVAL;
Vijay Dewangan9db18752014-09-15 16:25:01 -0700493}
494
Tushar Mathura3c18932015-08-24 20:27:21 +0530495/*
496 * nss_ipv4_update_conn_count_cb()
497 * call back function for the ipv4 connection count update handler
498 */
499static void nss_ipv4_update_conn_count_cb(void *app_data, struct nss_ipv4_msg *nim)
500{
501 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
502 nss_warning("IPv4 connection count update failed with error: %d\n", nim->cm.error);
503 return;
504 }
505
506 nss_warning("IPv4 connection count update success: %d\n", nim->cm.error);
507}
508
509/*
510 * nss_ipv4_update_conn_count()
511 * Sets the maximum number of connections for IPv4
512 */
513int nss_ipv4_update_conn_count(int ipv4_num_conn)
514{
515 struct nss_top_instance *nss_top = &nss_top_main;
516 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
517 int saved_nss_ipv4_conn_cfg = nss_ipv4_conn_cfg;
518 int ret = 0;
519
520 nss_ipv4_conn_cfg = ipv4_num_conn;
521
522 /*
523 * Process request to change number of IPv4 connections
524 */
525 ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg,
526 nss_ipv4_update_conn_count_cb);
527 if (ret != 0) {
528 nss_ipv4_conn_cfg = saved_nss_ipv4_conn_cfg;
529 return ret;
530 }
531
532 return 0;
533}
534
Stephen Wang52e6d342016-03-29 15:02:33 -0700535static struct ctl_table nss_ipv4_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700536 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700537 .procname = "ipv4_conn",
538 .data = &nss_ipv4_conn_cfg,
539 .maxlen = sizeof(int),
540 .mode = 0644,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700541 .proc_handler = &nss_ipv4_conn_cfg_handler,
542 },
543 { }
544};
545
Stephen Wang52e6d342016-03-29 15:02:33 -0700546static struct ctl_table nss_ipv4_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700547 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700548 .procname = "ipv4cfg",
549 .mode = 0555,
550 .child = nss_ipv4_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700551 },
552 { }
553};
554
555
Stephen Wang52e6d342016-03-29 15:02:33 -0700556static struct ctl_table nss_ipv4_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700557 {
558 .procname = "nss",
559 .mode = 0555,
560 .child = nss_ipv4_dir,
561 },
562 { }
563};
564
Stephen Wang52e6d342016-03-29 15:02:33 -0700565static struct ctl_table nss_ipv4_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700566 {
567 .procname = "dev",
568 .mode = 0555,
569 .child = nss_ipv4_root_dir,
570 },
571 { }
572};
573
574static struct ctl_table_header *nss_ipv4_header;
575
576/*
577 * nss_ipv4_register_sysctl()
578 * Register sysctl specific to ipv4
579 */
580void nss_ipv4_register_sysctl(void)
581{
Stephen Wang49b474b2016-03-25 10:40:30 -0700582 sema_init(&i4cfgp.sem, 1);
583 init_completion(&i4cfgp.complete);
584 i4cfgp.current_value = nss_ipv4_conn_cfg;
585
Vijay Dewangan9db18752014-09-15 16:25:01 -0700586 /*
587 * Register sysctl table.
588 */
589 nss_ipv4_header = register_sysctl_table(nss_ipv4_root);
590}
591
592/*
593 * nss_ipv4_unregister_sysctl()
594 * Unregister sysctl specific to ipv4
595 */
596void nss_ipv4_unregister_sysctl(void)
597{
598 /*
599 * Unregister sysctl table.
600 */
601 if (nss_ipv4_header) {
602 unregister_sysctl_table(nss_ipv4_header);
603 }
604}
605
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700606/*
607 * nss_ipv4_msg_init()
608 * Initialize IPv4 message.
609 */
610void 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 -0800611 nss_ipv4_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700612{
613 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
614}
615
Thomas Wu68250352014-04-02 18:59:40 -0700616EXPORT_SYMBOL(nss_ipv4_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700617EXPORT_SYMBOL(nss_ipv4_tx_with_size);
Thomas Wu68250352014-04-02 18:59:40 -0700618EXPORT_SYMBOL(nss_ipv4_notify_register);
619EXPORT_SYMBOL(nss_ipv4_notify_unregister);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700620EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_register);
621EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_unregister);
Thomas Wu68250352014-04-02 18:59:40 -0700622EXPORT_SYMBOL(nss_ipv4_get_mgr);
Vijay Dewangan9db18752014-09-15 16:25:01 -0700623EXPORT_SYMBOL(nss_ipv4_register_sysctl);
624EXPORT_SYMBOL(nss_ipv4_unregister_sysctl);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700625EXPORT_SYMBOL(nss_ipv4_msg_init);
Tushar Mathura3c18932015-08-24 20:27:21 +0530626EXPORT_SYMBOL(nss_ipv4_update_conn_count);