blob: 0f694dde4e9014b9159486ec0efbc5a8bc6ed8ea [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 */
375nss_tx_status_t nss_qrfs_set_flow_rule(struct net_device *netdev, uint32_t if_num,
376 struct sk_buff *skb, uint32_t cpu, uint32_t action)
377{
378 struct nss_ctx_instance *nss_ctx;
379 nss_tx_status_t status;
380 int i;
381
382 for (i = 0; i < NSS_CORE_MAX; i++) {
383 nss_ctx = nss_qrfs_get_ctx(i);
384
385 /*
386 * Set QRFS flow rule message and transmit the message to NSS core.
387 */
388 if (action == NSS_QRFS_MSG_FLOW_ADD) {
389 status = nss_qrfs_add_flow_rule(nss_ctx, if_num, skb, cpu, true);
390 } else {
391 status = nss_qrfs_delete_flow_rule(nss_ctx, if_num, skb, cpu, true);
392 }
393
394 if (status != NSS_TX_SUCCESS) {
395 nss_warning("%p: failed to send flow rule to NSS core %d\n", nss_ctx, i);
396 return NSS_TX_FAILURE;
397 }
398 }
399
400 return NSS_TX_SUCCESS;
401}
402EXPORT_SYMBOL(nss_qrfs_set_flow_rule);
403
404/*
405 * nss_qrfs_register_handler()
406 */
407void nss_qrfs_register_handler(struct nss_ctx_instance *nss_ctx)
408{
409 nss_core_register_handler(nss_ctx, NSS_QRFS_INTERFACE, nss_qrfs_msg_handler, NULL);
Yu Huange189d8a2018-01-04 17:45:57 -0800410 nss_qrfs_stats_dentry_create();
Yu Huang6b2f0be2017-10-18 16:11:49 -0700411}
412EXPORT_SYMBOL(nss_qrfs_register_handler);
413
414/*
415 * nss_qrfs_notify_register()
416 * Register to receive QRFS notify messages.
417 */
418struct nss_ctx_instance *nss_qrfs_notify_register(int core, nss_qrfs_msg_callback_t cb, void *app_data)
419{
420 if (core >= NSS_CORE_MAX) {
421 nss_warning("Input core number %d is wrong\n", core);
422 return NULL;
423 }
424
425 nss_qrfs_notify[core].qrfs_callback = cb;
426 nss_qrfs_notify[core].app_data = app_data;
427
428 return (struct nss_ctx_instance *)&nss_top_main.nss[core];
429}
430
431/*
432 * nss_qrfs_notify_unregister()
433 * Unregister to receive QRFS notify messages.
434 */
435void nss_qrfs_notify_unregister(int core)
436{
437 if (core >= NSS_CORE_MAX) {
438 nss_warning("Input core number %d is wrong\n", core);
439 return;
440 }
441
442 nss_qrfs_notify[core].qrfs_callback = NULL;
443 nss_qrfs_notify[core].app_data = NULL;
444}
445
446/*
447 * nss_qrfs_init()
448 */
449void nss_qrfs_init(void)
450{
451 int core;
452
453 for (core = 0; core < NSS_CORE_MAX; core++) {
454 nss_qrfs_notify_register(core, NULL, NULL);
455 }
456}