blob: bf4020cc9b6aaa89049a170d3c054964ba8fbc11 [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"
23#include "nss_ipv4.h"
24
25extern void nss_rx_metadata_ipv4_rule_establish(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_rule_establish *nire);
26extern void nss_rx_ipv4_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs);
27
28/*
29 * nss_ipv4_driver_update()
30 * Update driver specific information from the messsage.
31 */
32#if 0
33static void nss_ipv4_driver_update(void)
34{
35 /*
36 * Update statistics maintained by NSS driver
37 */
38 spin_lock_bh(&nss_top->stats_lock);
39
40 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;
44
45 /*
46 * Update the PPPoE interface stats, if there is any PPPoE session on the interfaces.
47 */
48 if (nirs->flow_pppoe_session_id) {
49 pppoe_dev = ppp_session_to_netdev(nirs->flow_pppoe_session_id, (uint8_t *)nirs->flow_pppoe_remote_mac);
50 if (pppoe_dev) {
51 ppp_update_stats(pppoe_dev, nirs->flow_rx_packet_count, nirs->flow_rx_byte_count,
52 nirs->flow_tx_packet_count, nirs->flow_tx_byte_count);
53 dev_put(pppoe_dev);
54 }
55 }
56
57 if (nirs->return_pppoe_session_id) {
58 pppoe_dev = ppp_session_to_netdev(nirs->return_pppoe_session_id, (uint8_t *)nirs->return_pppoe_remote_mac);
59 if (pppoe_dev) {
60 ppp_update_stats(pppoe_dev, nirs->return_rx_packet_count, nirs->return_rx_byte_count,
61 nirs->return_tx_packet_count, nirs->return_tx_byte_count);
62 dev_put(pppoe_dev);
63 }
64 }
65
66 /*
67 * TODO: Update per dev accelerated statistics
68 */
69 spin_unlock_bh(&nss_top->stats_lock);
70}
71#endif
72
73/*
74 * nss_ipv4_handler()
75 * Handle NSS -> HLOS messages for IPv4 bridge/route
76 */
77static void nss_ipv4_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
78{
79 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
80
81/*
82 * TODO: Turn back on for new APIs
83 */
84// nss_ipv4_msg_callback_t cb;
85
86 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
87
88 /*
89 * Sanity check the message type
90 */
91 if (ncm->type > NSS_IPV4_MAX_MSG_TYPES) {
92 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
93 return;
94 }
95
96 if (ncm->len > sizeof(struct nss_ipv4_msg)) {
97 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
98 return;
99 }
100
101
102 /*
103 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
104 * to the same callback/app_data.
105 */
106 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
107 ncm->cb = (uint32_t)nss_ctx->nss_top->ipv4_callback;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530108 ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv4_ctx;
Thomas Wu68250352014-04-02 18:59:40 -0700109 }
110
111 /*
112 * Log failures
113 */
114 nss_core_log_msg_failures(nss_ctx, ncm);
115
116 /*
117 * Handle deprecated messages. Eventually these messages should be removed.
118 */
119 switch (nim->cm.type) {
120 case NSS_IPV4_RX_ESTABLISH_RULE_MSG:
121 return nss_rx_metadata_ipv4_rule_establish(nss_ctx, &nim->msg.rule_establish);
122 break;
123
124 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
125 return nss_rx_ipv4_sync(nss_ctx, &nim->msg.conn_stats);
126 break;
127 }
128
129 /*
130 * Local driver updates for ipv4.
131 */
132#if 0
133 /*
134 * Do we have a callback?
135 */
136 if (!ncm->cb) {
137 return;
138 }
139
140 /*
141 * Callback
142 */
143 cb = (nss_ipv4_msg_callback_t)ncm->cb;
144 cb((void *)ncm->app_data, nim);
145#endif
146}
147
148/*
149 * nss_ipv4_tx()
150 * Transmit an ipv4 message to the FW.
151 */
152nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
153{
154 struct nss_ipv4_msg *nim2;
155 struct nss_cmn_msg *ncm = &nim->cm;
156 struct sk_buff *nbuf;
157 int32_t status;
158
159 NSS_VERIFY_CTX_MAGIC(nss_ctx);
160 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
161 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
162 return NSS_TX_FAILURE_NOT_READY;
163 }
164
165 /*
166 * Sanity check the message
167 */
168 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
169 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
170 return NSS_TX_FAILURE;
171 }
172
173 if (ncm->type > NSS_IPV4_MAX_MSG_TYPES) {
174 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
175 return NSS_TX_FAILURE;
176 }
177
178 if (ncm->len > sizeof(struct nss_ipv4_msg)) {
179 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
180 return NSS_TX_FAILURE;
181 }
182
183 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
184 if (unlikely(!nbuf)) {
185 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
186 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
187 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
188 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
189 return NSS_TX_FAILURE;
190 }
191
192 /*
193 * Copy the message to our skb.
194 */
195 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, sizeof(struct nss_ipv4_msg));
196 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
197
198 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
199 if (status != NSS_CORE_STATUS_SUCCESS) {
200 dev_kfree_skb_any(nbuf);
201 nss_warning("%p: Unable to enqueue 'Destroy IPv4' rule\n", nss_ctx);
202 return NSS_TX_FAILURE;
203 }
204
205 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
206 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
207
208 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
209 return NSS_TX_SUCCESS;
210}
211
212/*
213 **********************************
214 Register/Unregister/Miscellaneous APIs
215 **********************************
216 */
217
218/*
219 * nss_ipv4_notify_register()
220 * Register to received IPv4 events.
221 *
222 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
223 */
224struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
225{
226 /*
227 * TODO: We need to have a new array in support of the new API
228 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
229 */
230 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530231 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700232 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
233}
234
235/*
236 * nss_ipv4_notify_unregister()
237 * Unregister to received IPv4 events.
238 *
239 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
240 */
241void nss_ipv4_notify_unregister(void)
242{
243 nss_top_main.ipv4_callback = NULL;
244}
245
246/*
247 * nss_ipv4_get_mgr()
248 *
249 * TODO: This only suppports a single ipv4, do we ever want to support more?
250 */
251struct nss_ctx_instance *nss_ipv4_get_mgr(void)
252{
253 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
254}
255
256/*
257 * nss_ipv4_register_handler()
258 * Register our handler to receive messages for this interface
259 */
260void nss_ipv4_register_handler()
261{
262 if (nss_core_register_handler(NSS_IPV4_RX_INTERFACE, nss_ipv4_handler, NULL) != NSS_CORE_STATUS_SUCCESS) {
263 nss_warning("IPv4 handler failed to register");
264 }
265}
266
267EXPORT_SYMBOL(nss_ipv4_tx);
268EXPORT_SYMBOL(nss_ipv4_notify_register);
269EXPORT_SYMBOL(nss_ipv4_notify_unregister);
270EXPORT_SYMBOL(nss_ipv4_get_mgr);
271EXPORT_SYMBOL(nss_ipv4_register_handler);