blob: 03fad802320cc70356ee939ce2b0771d7c41c748 [file] [log] [blame]
Thomas Wu68250352014-04-02 18:59:40 -07001/*
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/*
18 * nss_tx_rx_ipv4.c
19 * NSS IPv4 APIs
20 */
21#include <linux/ppp_channel.h>
22#include "nss_tx_rx_common.h"
Thomas Wu68250352014-04-02 18:59:40 -070023
24extern void nss_rx_metadata_ipv4_rule_establish(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_rule_establish *nire);
25extern void nss_rx_ipv4_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs);
26
27/*
Sol Kavy57d24b42014-04-05 13:45:36 -070028 * nss_ipv4_driver_conn_sync_update()
Thomas Wu68250352014-04-02 18:59:40 -070029 * Update driver specific information from the messsage.
30 */
Murat Sezgin0c0561d2014-04-09 18:55:58 -070031static 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 -070032{
Sol Kavy57d24b42014-04-05 13:45:36 -070033 struct nss_top_instance *nss_top = nss_ctx->nss_top;
34 struct net_device *pppoe_dev = NULL;
35
Thomas Wu68250352014-04-02 18:59:40 -070036 /*
37 * Update statistics maintained by NSS driver
38 */
39 spin_lock_bh(&nss_top->stats_lock);
Thomas Wu68250352014-04-02 18:59:40 -070040 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_PKTS] += nirs->flow_rx_packet_count + nirs->return_rx_packet_count;
41 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_BYTES] += nirs->flow_rx_byte_count + nirs->return_rx_byte_count;
42 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_PKTS] += nirs->flow_tx_packet_count + nirs->return_tx_packet_count;
43 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 -070044 spin_unlock_bh(&nss_top->stats_lock);
Sol Kavy57d24b42014-04-05 13:45:36 -070045
46 /*
47 * Update the PPPoE interface stats, if there is any PPPoE session on the interfaces.
48 */
49 if (nirs->flow_pppoe_session_id) {
50 pppoe_dev = ppp_session_to_netdev(nirs->flow_pppoe_session_id, (uint8_t *)nirs->flow_pppoe_remote_mac);
51 if (pppoe_dev) {
52 ppp_update_stats(pppoe_dev, nirs->flow_rx_packet_count, nirs->flow_rx_byte_count,
53 nirs->flow_tx_packet_count, nirs->flow_tx_byte_count);
54 dev_put(pppoe_dev);
55 }
56 }
57
58 if (nirs->return_pppoe_session_id) {
59 pppoe_dev = ppp_session_to_netdev(nirs->return_pppoe_session_id, (uint8_t *)nirs->return_pppoe_remote_mac);
60 if (pppoe_dev) {
61 ppp_update_stats(pppoe_dev, nirs->return_rx_packet_count, nirs->return_rx_byte_count,
62 nirs->return_tx_packet_count, nirs->return_tx_byte_count);
63 dev_put(pppoe_dev);
64 }
65 }
Thomas Wu68250352014-04-02 18:59:40 -070066}
Thomas Wu68250352014-04-02 18:59:40 -070067
68/*
Murat Sezgin0c0561d2014-04-09 18:55:58 -070069 * nss_ipv4_driver_node_sync_update)
70 * Update driver specific information from the messsage.
71 */
72static void nss_ipv4_driver_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_node_sync *nins)
73{
74 struct nss_top_instance *nss_top = nss_ctx->nss_top;
75 uint32_t i;
76
77 /*
78 * Update statistics maintained by NSS driver
79 */
80 spin_lock_bh(&nss_top->stats_lock);
81 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets;
82 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes;
83 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped;
84 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets;
85 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes;
86
Murat Sezgin0c0561d2014-04-09 18:55:58 -070087 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_REQUESTS] += nins->ipv4_connection_create_requests;
88 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_COLLISIONS] += nins->ipv4_connection_create_collisions;
89 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_connection_create_invalid_interface;
90 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_connection_destroy_requests;
91 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_MISSES] += nins->ipv4_connection_destroy_misses;
92 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_HITS] += nins->ipv4_connection_hash_hits;
93 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_REORDERS] += nins->ipv4_connection_hash_reorders;
94 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_FLUSHES] += nins->ipv4_connection_flushes;
95 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_EVICTIONS] += nins->ipv4_connection_evictions;
96
97 for (i = 0; i < NSS_EXCEPTION_EVENT_IPV4_MAX; i++) {
98 nss_top->stats_if_exception_ipv4[i] += nins->exception_events[i];
99 }
100 spin_unlock_bh(&nss_top->stats_lock);
101}
102
103/*
Sol Kavy2783c072014-04-05 12:53:13 -0700104 * nss_ipv4_rx_msg_handler()
Thomas Wu68250352014-04-02 18:59:40 -0700105 * Handle NSS -> HLOS messages for IPv4 bridge/route
106 */
Sol Kavy2783c072014-04-05 12:53:13 -0700107static 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 -0700108{
109 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
Sol Kavy57d24b42014-04-05 13:45:36 -0700110 nss_ipv4_msg_callback_t cb;
Thomas Wu68250352014-04-02 18:59:40 -0700111
112 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
113
114 /*
115 * Sanity check the message type
116 */
117 if (ncm->type > NSS_IPV4_MAX_MSG_TYPES) {
118 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
119 return;
120 }
121
122 if (ncm->len > sizeof(struct nss_ipv4_msg)) {
123 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
124 return;
125 }
126
Thomas Wu68250352014-04-02 18:59:40 -0700127 /*
Thomas Wu68250352014-04-02 18:59:40 -0700128 * Log failures
129 */
130 nss_core_log_msg_failures(nss_ctx, ncm);
131
132 /*
133 * Handle deprecated messages. Eventually these messages should be removed.
134 */
135 switch (nim->cm.type) {
136 case NSS_IPV4_RX_ESTABLISH_RULE_MSG:
137 return nss_rx_metadata_ipv4_rule_establish(nss_ctx, &nim->msg.rule_establish);
138 break;
139
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700140 case NSS_IPV4_RX_NODE_STATS_SYNC_MSG:
141 /*
142 * Update driver statistics on node sync.
143 */
144 nss_ipv4_driver_node_sync_update(nss_ctx, &nim->msg.node_stats);
145 break;
146
Thomas Wu68250352014-04-02 18:59:40 -0700147 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
Sol Kavy57d24b42014-04-05 13:45:36 -0700148 /*
149 * Update driver statistics on connection sync.
150 */
151 nss_ipv4_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats);
Sol Kavy4013e282014-04-06 15:57:00 -0700152 nss_rx_ipv4_sync(nss_ctx, &nim->msg.conn_stats);
Thomas Wu68250352014-04-02 18:59:40 -0700153 break;
154 }
155
156 /*
Sol Kavy57d24b42014-04-05 13:45:36 -0700157 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
158 * to the same callback/app_data.
Thomas Wu68250352014-04-02 18:59:40 -0700159 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700160 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
161 ncm->cb = (uint32_t)nss_ctx->nss_top->ipv4_callback;
162 ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv4_ctx;
163 }
Sol Kavy2783c072014-04-05 12:53:13 -0700164
Thomas Wu68250352014-04-02 18:59:40 -0700165 /*
166 * Do we have a callback?
167 */
168 if (!ncm->cb) {
169 return;
170 }
171
172 /*
173 * Callback
174 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700175 cb = (nss_ipv4_msg_callback_t)ncm->cb;
Thomas Wu68250352014-04-02 18:59:40 -0700176 cb((void *)ncm->app_data, nim);
Thomas Wu68250352014-04-02 18:59:40 -0700177}
178
179/*
180 * nss_ipv4_tx()
181 * Transmit an ipv4 message to the FW.
182 */
183nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
184{
185 struct nss_ipv4_msg *nim2;
186 struct nss_cmn_msg *ncm = &nim->cm;
187 struct sk_buff *nbuf;
188 int32_t status;
189
190 NSS_VERIFY_CTX_MAGIC(nss_ctx);
191 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
192 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
193 return NSS_TX_FAILURE_NOT_READY;
194 }
195
196 /*
197 * Sanity check the message
198 */
199 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
200 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
201 return NSS_TX_FAILURE;
202 }
203
204 if (ncm->type > NSS_IPV4_MAX_MSG_TYPES) {
205 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
206 return NSS_TX_FAILURE;
207 }
208
209 if (ncm->len > sizeof(struct nss_ipv4_msg)) {
210 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
211 return NSS_TX_FAILURE;
212 }
213
214 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
215 if (unlikely(!nbuf)) {
216 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
217 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
218 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
219 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
220 return NSS_TX_FAILURE;
221 }
222
223 /*
224 * Copy the message to our skb.
225 */
226 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, sizeof(struct nss_ipv4_msg));
227 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
228
229 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
230 if (status != NSS_CORE_STATUS_SUCCESS) {
231 dev_kfree_skb_any(nbuf);
Sol Kavycd1bd5c2014-04-04 11:09:44 -0700232 nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx);
Thomas Wu68250352014-04-02 18:59:40 -0700233 return NSS_TX_FAILURE;
234 }
235
236 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
237 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
238
239 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
240 return NSS_TX_SUCCESS;
241}
242
243/*
244 **********************************
245 Register/Unregister/Miscellaneous APIs
246 **********************************
247 */
248
249/*
250 * nss_ipv4_notify_register()
251 * Register to received IPv4 events.
252 *
253 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
254 */
255struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
256{
257 /*
258 * TODO: We need to have a new array in support of the new API
259 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
260 */
261 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530262 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700263 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
264}
265
266/*
267 * nss_ipv4_notify_unregister()
268 * Unregister to received IPv4 events.
269 *
270 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
271 */
272void nss_ipv4_notify_unregister(void)
273{
274 nss_top_main.ipv4_callback = NULL;
275}
276
277/*
278 * nss_ipv4_get_mgr()
279 *
280 * TODO: This only suppports a single ipv4, do we ever want to support more?
281 */
282struct nss_ctx_instance *nss_ipv4_get_mgr(void)
283{
284 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
285}
286
287/*
288 * nss_ipv4_register_handler()
289 * Register our handler to receive messages for this interface
290 */
Sol Kavy2783c072014-04-05 12:53:13 -0700291void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700292{
Sol Kavy2783c072014-04-05 12:53:13 -0700293 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 -0700294 nss_warning("IPv4 handler failed to register");
295 }
296}
297
298EXPORT_SYMBOL(nss_ipv4_tx);
299EXPORT_SYMBOL(nss_ipv4_notify_register);
300EXPORT_SYMBOL(nss_ipv4_notify_unregister);
301EXPORT_SYMBOL(nss_ipv4_get_mgr);