blob: 571056864b67817874913820ccb2fe805b1928eb [file] [log] [blame]
Yu Huang6b2f0be2017-10-18 16:11:49 -07001/*
2 **************************************************************************
3 * Copyright (c) 2017-2018, 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#include "nss_tx_rx_common.h"
Yu Huange189d8a2018-01-04 17:45:57 -080018#include "nss_qrfs_stats.h"
Yu Huang6b2f0be2017-10-18 16:11:49 -070019
20/*
21 * Notify data structure
22 */
23struct nss_qrfs_notify_data {
24 nss_qrfs_msg_callback_t qrfs_callback;
25 void *app_data;
26};
27
28static struct nss_qrfs_notify_data nss_qrfs_notify[NSS_CORE_MAX];
29
30/*
31 * nss_qrfs_verify_if_num()
32 * Verify if_num passed to us.
33 */
34static bool nss_qrfs_verify_if_num(uint32_t if_num)
35{
36 return if_num == NSS_QRFS_INTERFACE;
37}
38
39/*
40 * nss_qrfs_msg_handler()
41 * Handle NSS -> HLOS messages for QRFS
42 */
43static void nss_qrfs_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data)
44{
45 struct nss_qrfs_msg *nqm = (struct nss_qrfs_msg *)ncm;
46 nss_qrfs_msg_callback_t cb;
47
48 if (!nss_qrfs_verify_if_num(ncm->interface)) {
49 nss_warning("%p: invalid interface %d for QRFS\n", nss_ctx, ncm->interface);
50 return;
51 }
52
53 /*
54 * Is this a valid request/response?
55 */
56 if (ncm->type >= NSS_QRFS_MSG_MAX) {
57 nss_warning("%p: invalid message %d for QRFS\n", nss_ctx, ncm->type);
58 return;
59 }
60
61 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_qrfs_msg)) {
62 nss_warning("%p: message length is greater than required: %d\n", nss_ctx, nss_cmn_get_msg_len(ncm));
63 return;
64 }
65
66 /*
67 * Log failures
68 */
69 nss_core_log_msg_failures(nss_ctx, ncm);
70
Yu Huange189d8a2018-01-04 17:45:57 -080071 switch (ncm->type) {
72 case NSS_QRFS_MSG_STATS_SYNC:
73 /*
74 * Update QRFS statistics.
75 */
76 nss_qrfs_stats_sync(nss_ctx, &nqm->msg.stats_sync);
77 break;
78 }
79
Yu Huang6b2f0be2017-10-18 16:11:49 -070080 /*
81 * Update the callback and app_data for NOTIFY messages
82 */
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +053083 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Yu Huang6b2f0be2017-10-18 16:11:49 -070084 ncm->cb = (nss_ptr_t)nss_qrfs_notify[nss_ctx->id].qrfs_callback;
85 ncm->app_data = (nss_ptr_t)nss_qrfs_notify[nss_ctx->id].app_data;
86 }
87
88 /*
89 * Do we have a callback?
90 */
91 if (!ncm->cb) {
92 return;
93 }
94
95 /*
96 * callback
97 */
98 cb = (nss_qrfs_msg_callback_t)ncm->cb;
99 cb((void *)ncm->app_data, nqm);
100}
101
102/*
103 * nss_qrfs_get_ctx()
104 */
105static struct nss_ctx_instance *nss_qrfs_get_ctx(int core_id)
106{
107 return &nss_top_main.nss[core_id];
108}
109
110/*
111 * nss_qrfs_get_flow_keys()
112 * Get 5 tuple information from flow keys and set in flow rule message.
113 */
114#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
115static bool nss_qrfs_get_flow_keys(struct nss_ctx_instance *nss_ctx, struct sk_buff *skb,
116 struct nss_qrfs_flow_rule_msg *nqfrm)
117{
118 struct flow_keys keys;
119 uint16_t protocol = skb->protocol;
120 bool res;
121 struct ipv6hdr *ip6hdr;
122
123 res = skb_flow_dissect(skb, &keys);
124 if (!res) {
125 nss_warning("%p: failed to get flow keys\n", nss_ctx);
126 return res;
127 }
128
129 nqfrm->protocol = keys.ip_proto;
130 nqfrm->src_port = keys.port16[0];
131 nqfrm->dst_port = keys.port16[1];
132
133 if (protocol == htons(ETH_P_IP)) {
134 nqfrm->ip_version = 4;
135 nqfrm->src_addr[0] = keys.src;
136 nqfrm->dst_addr[0] = keys.dst;
137 return true;
138 }
139
140 nqfrm->ip_version = 6;
141 ip6hdr = (struct ipv6hdr *)skb_network_header(skb);
142 if (!ip6hdr) {
143 nss_warning("%p: failed to get IPv6 address\n", nss_ctx);
144 return false;
145 }
146
147 memcpy(nqfrm->src_addr, &ip6hdr->saddr, sizeof(struct in6_addr));
148 memcpy(nqfrm->dst_addr, &ip6hdr->daddr, sizeof(struct in6_addr));
149
150 return true;
151}
152#else
153static bool nss_qrfs_get_flow_keys(struct nss_ctx_instance *nss_ctx, struct sk_buff *skb,
154 struct nss_qrfs_flow_rule_msg *nqfrm)
155{
156 struct flow_keys keys;
157 bool res;
158
159 res = skb_flow_dissect_flow_keys(skb, &keys, 0);
160 if (!res) {
161 nss_warning("%p: failed to get flow keys\n", nss_ctx);
162 return res;
163 }
164
165 nqfrm->protocol = (uint16_t)keys.basic.ip_proto;
166 nqfrm->src_port = keys.ports.src;
167 nqfrm->dst_port = keys.ports.dst;
168
169 if (keys.basic.n_proto == htons(ETH_P_IP)) {
170 nqfrm->ip_version = 4;
171 nqfrm->src_addr[0] = keys.addrs.v4addrs.src;
172 nqfrm->dst_addr[0] = keys.addrs.v4addrs.dst;
173 return true;
174 }
175
176 nqfrm->ip_version = 6;
177 memcpy(nqfrm->src_addr, &keys.addrs.v6addrs.src, sizeof(struct in6_addr));
178 memcpy(nqfrm->dst_addr, &keys.addrs.v6addrs.dst, sizeof(struct in6_addr));
179
180 return true;
181}
182#endif
183
184/*
185 * nss_qrfs_flow_add_msg_callback()
186 * Callback function for receiving flow add response messages.
187 */
188static void nss_qrfs_flow_add_msg_callback(void *app_data, struct nss_qrfs_msg *nqm)
189{
190 struct nss_ctx_instance *nss_ctx __maybe_unused = (struct nss_ctx_instance *)app_data;
191 struct nss_qrfs_flow_rule_msg *nqfrm;
192
193 if (nqm->cm.type != NSS_QRFS_MSG_FLOW_ADD) {
194 nss_warning("%p: invalid flow response message %d\n", nss_ctx, nqm->cm.type);
195 return;
196 }
197
198 nqfrm = &nqm->msg.flow_add;
199
200 if ((nqfrm->ip_version != 4) && (nqfrm->ip_version != 6)) {
201 nss_warning("%p: invalid IP version %d\n", nss_ctx, nqfrm->ip_version);
202 return;
203 }
204
205 if (nqm->cm.response != NSS_CMN_RESPONSE_ACK) {
206 nss_warning("%p: flow add configuration error: %d for NSS core %d\n",
207 nss_ctx, nqm->cm.error, nss_ctx->id);
208 }
209}
210
211/*
212 * nss_qrfs_flow_delete_msg_callback()
213 * Callback function for receiving flow delete response messages.
214 */
215static void nss_qrfs_flow_delete_msg_callback(void *app_data, struct nss_qrfs_msg *nqm)
216{
217 struct nss_ctx_instance *nss_ctx __maybe_unused = (struct nss_ctx_instance *)app_data;
218 struct nss_qrfs_flow_rule_msg *nqfrm;
219
220 if (nqm->cm.type != NSS_QRFS_MSG_FLOW_DELETE) {
221 nss_warning("%p: invalid flow response message %d\n", nss_ctx, nqm->cm.type);
222 return;
223 }
224
225 nqfrm = &nqm->msg.flow_delete;
226
227 if ((nqfrm->ip_version != 4) && (nqfrm->ip_version != 6)) {
228 nss_warning("%p: invalid IP version %d\n", nss_ctx, nqfrm->ip_version);
229 return;
230 }
231
232 if (nqm->cm.response != NSS_CMN_RESPONSE_ACK) {
233 nss_warning("%p: flow delete configuration error: %d for NSS core %d\n",
234 nss_ctx, nqm->cm.error, nss_ctx->id);
235 }
236}
237
238/*
239 * nss_qrfs_msg_init()
240 * Initialize the common header of QRFS message
241 */
242static void nss_qrfs_msg_init(struct nss_qrfs_msg *nqm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
243{
244 nss_cmn_msg_init(&nqm->cm, if_num, type, len, cb, app_data);
245}
246
247/*
248 * nss_qrfs_tx_msg()
249 * Transmit a QRFS message to NSS firmware
250 */
251static nss_tx_status_t nss_qrfs_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_qrfs_msg *msg)
252{
Yu Huang6b2f0be2017-10-18 16:11:49 -0700253 struct nss_cmn_msg *ncm = &msg->cm;
Yu Huang6b2f0be2017-10-18 16:11:49 -0700254
255 /*
256 * Sanity check the message
257 */
258 if (!nss_qrfs_verify_if_num(ncm->interface)) {
259 nss_warning("%p: interface is not QRFS interface: %d\n", nss_ctx, ncm->interface);
260 return NSS_TX_FAILURE;
261 }
262
263 if (ncm->type >= NSS_QRFS_MSG_MAX) {
264 nss_warning("%p: message type is out of range: %d\n", nss_ctx, ncm->type);
265 return NSS_TX_FAILURE;
266 }
267
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700268 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Yu Huang6b2f0be2017-10-18 16:11:49 -0700269}
270
271/*
272 * nss_qrfs_add_flow_rule()
273 * Set a QRFS flow rule add message and transmit the message to NSS core.
274 */
275static nss_tx_status_t nss_qrfs_add_flow_rule(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
276 struct sk_buff *skb, uint32_t cpu, bool need_cb)
277{
278 struct nss_qrfs_msg nqm;
279 struct nss_qrfs_flow_rule_msg *nqfrm;
280 nss_tx_status_t status;
281 nss_qrfs_msg_callback_t cb = NULL;
282 void *app_data = NULL;
283 bool res;
284
285 memset(&nqm, 0, sizeof(struct nss_qrfs_msg));
286
287 if (need_cb) {
288 cb = nss_qrfs_flow_add_msg_callback;
289 app_data = (void *)nss_ctx;
290 }
291
292 /*
293 * Initialize common header of QRFS flow rule add message.
294 */
295 nss_qrfs_msg_init(&nqm, NSS_QRFS_INTERFACE, NSS_QRFS_MSG_FLOW_ADD,
296 sizeof(struct nss_qrfs_flow_rule_msg), cb, app_data);
297
298 /*
299 * Set flow rule of QRFS flow rule add message
300 */
301 nqfrm = &nqm.msg.flow_add;
302 res = nss_qrfs_get_flow_keys(nss_ctx, skb, nqfrm);
303 if (!res) {
304 return NSS_TX_FAILURE;
305 }
306
307 nqfrm->cpu = (uint16_t)cpu;
308 nqfrm->if_num = if_num;
309
310 /*
311 * Send QRFS flow rule add message to NSS core
312 */
313 status = nss_qrfs_tx_msg(nss_ctx, &nqm);
314 if (status == NSS_TX_SUCCESS) {
315 return status;
316 }
317
318 return NSS_TX_FAILURE;
319}
320
321/*
322 * nss_qrfs_delete_flow_rule()
323 * Set a QRFS delete flow rule message and transmit the message to all NSS core.
324 */
325static nss_tx_status_t nss_qrfs_delete_flow_rule(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
326 struct sk_buff *skb, uint32_t cpu, bool need_cb)
327{
328 struct nss_qrfs_msg nqm;
329 struct nss_qrfs_flow_rule_msg *nqfrm;
330 nss_tx_status_t status;
331 nss_qrfs_msg_callback_t cb = NULL;
332 void *app_data = NULL;
333 bool res;
334
335 memset(&nqm, 0, sizeof(struct nss_qrfs_msg));
336
337 if (need_cb) {
338 cb = nss_qrfs_flow_delete_msg_callback;
339 app_data = (void *)nss_ctx;
340 }
341
342 /*
343 * Initialize common header of QRFS flow rule delete message.
344 */
345 nss_qrfs_msg_init(&nqm, NSS_QRFS_INTERFACE, NSS_QRFS_MSG_FLOW_DELETE,
346 sizeof(struct nss_qrfs_flow_rule_msg), cb, app_data);
347
348 /*
349 * Set flow rule of QRFS flow rule delete message
350 */
351 nqfrm = &nqm.msg.flow_delete;
352 res = nss_qrfs_get_flow_keys(nss_ctx, skb, nqfrm);
353 if (!res) {
354 return NSS_TX_FAILURE;
355 }
356
357 nqfrm->cpu = (uint16_t)cpu;
358 nqfrm->if_num = if_num;
359
360 /*
361 * Send QRFS flow rule delete message to NSS core
362 */
363 status = nss_qrfs_tx_msg(nss_ctx, &nqm);
364 if (status == NSS_TX_SUCCESS) {
365 return status;
366 }
367
368 return NSS_TX_FAILURE;
369}
370
371/*
372 * nss_qrfs_set_flow_rule()
373 * Set a QRFS flow rule message and transmit the message to all NSS cores.
374 */
Jackson Bockusbd1080c2018-07-30 16:33:41 -0700375nss_tx_status_t nss_qrfs_set_flow_rule(struct sk_buff *skb, uint32_t cpu, uint32_t action)
Yu Huang6b2f0be2017-10-18 16:11:49 -0700376{
377 struct nss_ctx_instance *nss_ctx;
378 nss_tx_status_t status;
379 int i;
380
381 for (i = 0; i < NSS_CORE_MAX; i++) {
382 nss_ctx = nss_qrfs_get_ctx(i);
383
384 /*
385 * Set QRFS flow rule message and transmit the message to NSS core.
Jackson Bockusbd1080c2018-07-30 16:33:41 -0700386 *
387 * TODO: Remove if_num parameter from add_flow_rule() and
388 * delete_flow_rule(), since it is unused in firmware.
Yu Huang6b2f0be2017-10-18 16:11:49 -0700389 */
390 if (action == NSS_QRFS_MSG_FLOW_ADD) {
Jackson Bockusbd1080c2018-07-30 16:33:41 -0700391 status = nss_qrfs_add_flow_rule(nss_ctx, 0, skb, cpu, true);
Yu Huang6b2f0be2017-10-18 16:11:49 -0700392 } else {
Jackson Bockusbd1080c2018-07-30 16:33:41 -0700393 status = nss_qrfs_delete_flow_rule(nss_ctx, 0, skb, cpu, true);
Yu Huang6b2f0be2017-10-18 16:11:49 -0700394 }
395
396 if (status != NSS_TX_SUCCESS) {
397 nss_warning("%p: failed to send flow rule to NSS core %d\n", nss_ctx, i);
398 return NSS_TX_FAILURE;
399 }
400 }
401
402 return NSS_TX_SUCCESS;
403}
404EXPORT_SYMBOL(nss_qrfs_set_flow_rule);
405
406/*
407 * nss_qrfs_register_handler()
408 */
409void nss_qrfs_register_handler(struct nss_ctx_instance *nss_ctx)
410{
411 nss_core_register_handler(nss_ctx, NSS_QRFS_INTERFACE, nss_qrfs_msg_handler, NULL);
Yu Huange189d8a2018-01-04 17:45:57 -0800412 nss_qrfs_stats_dentry_create();
Yu Huang6b2f0be2017-10-18 16:11:49 -0700413}
414EXPORT_SYMBOL(nss_qrfs_register_handler);
415
416/*
417 * nss_qrfs_notify_register()
418 * Register to receive QRFS notify messages.
419 */
420struct nss_ctx_instance *nss_qrfs_notify_register(int core, nss_qrfs_msg_callback_t cb, void *app_data)
421{
422 if (core >= NSS_CORE_MAX) {
423 nss_warning("Input core number %d is wrong\n", core);
424 return NULL;
425 }
426
427 nss_qrfs_notify[core].qrfs_callback = cb;
428 nss_qrfs_notify[core].app_data = app_data;
429
430 return (struct nss_ctx_instance *)&nss_top_main.nss[core];
431}
432
433/*
434 * nss_qrfs_notify_unregister()
435 * Unregister to receive QRFS notify messages.
436 */
437void nss_qrfs_notify_unregister(int core)
438{
439 if (core >= NSS_CORE_MAX) {
440 nss_warning("Input core number %d is wrong\n", core);
441 return;
442 }
443
444 nss_qrfs_notify[core].qrfs_callback = NULL;
445 nss_qrfs_notify[core].app_data = NULL;
446}
447
448/*
449 * nss_qrfs_init()
450 */
451void nss_qrfs_init(void)
452{
453 int core;
454
455 for (core = 0; core < NSS_CORE_MAX; core++) {
456 nss_qrfs_notify_register(core, NULL, NULL);
457 }
458}