blob: f2c012bd1d905ab5d1063c63e51e7f860895f4dc [file] [log] [blame]
ratheesh kannoth7af985d2015-06-24 15:08:40 +05301/*
2 **************************************************************************
Stephen Wang3e2dbd12018-03-14 17:28:17 -07003 * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
ratheesh kannoth7af985d2015-06-24 15:08:40 +05304 * 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 <linux/l2tp.h>
18#include <net/sock.h>
19#include "nss_tx_rx_common.h"
Yu Huang8c107082017-07-24 14:58:26 -070020#include "nss_l2tpv2_stats.h"
Sachin Shashidhar015c28a2018-08-06 19:11:00 -070021#include "nss_l2tpv2_log.h"
ratheesh kannoth7af985d2015-06-24 15:08:40 +053022
23/*
24 * Data structures to store l2tpv2 nss debug stats
25 */
26static DEFINE_SPINLOCK(nss_l2tpv2_session_debug_stats_lock);
Yu Huang8c107082017-07-24 14:58:26 -070027static struct nss_l2tpv2_stats_session_debug nss_l2tpv2_session_debug_stats[NSS_MAX_L2TPV2_DYNAMIC_INTERFACES];
ratheesh kannoth7af985d2015-06-24 15:08:40 +053028
29/*
30 * nss_l2tpv2_session_debug_stats_sync
31 * Per session debug stats for l2tpv2
32 */
33void nss_l2tpv2_session_debug_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_l2tpv2_sync_session_stats_msg *stats_msg, uint16_t if_num)
34{
35 int i;
36 spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock);
37 for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) {
38 if (nss_l2tpv2_session_debug_stats[i].if_num == if_num) {
Yu Huang8c107082017-07-24 14:58:26 -070039 nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_RX_PPP_LCP_PKTS] += stats_msg->debug_stats.rx_ppp_lcp_pkts;
40 nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_RX_EXP_DATA_PKTS] += stats_msg->debug_stats.rx_exception_data_pkts;
41 nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_ENCAP_PBUF_ALLOC_FAIL_PKTS] += stats_msg->debug_stats.encap_pbuf_alloc_fail;
42 nss_l2tpv2_session_debug_stats[i].stats[NSS_L2TPV2_STATS_SESSION_DECAP_PBUF_ALLOC_FAIL_PKTS] += stats_msg->debug_stats.decap_pbuf_alloc_fail;
ratheesh kannoth7af985d2015-06-24 15:08:40 +053043 break;
44 }
45 }
46 spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock);
47}
48
49/*
50 * nss_l2tpv2_global_session_stats_get()
51 * Get session l2tpv2 statitics.
52 */
53void nss_l2tpv2_session_debug_stats_get(void *stats_mem)
54{
Yu Huang8c107082017-07-24 14:58:26 -070055 struct nss_l2tpv2_stats_session_debug *stats = (struct nss_l2tpv2_stats_session_debug *)stats_mem;
ratheesh kannoth7af985d2015-06-24 15:08:40 +053056 int i;
57
58 if (!stats) {
59 nss_warning("No memory to copy l2tpv2 session stats");
60 return;
61 }
62
63 spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock);
64 for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) {
65 if (nss_l2tpv2_session_debug_stats[i].valid) {
Yu Huang8c107082017-07-24 14:58:26 -070066 memcpy(stats, &nss_l2tpv2_session_debug_stats[i], sizeof(struct nss_l2tpv2_stats_session_debug));
ratheesh kannoth7af985d2015-06-24 15:08:40 +053067 stats++;
68 }
69 }
70 spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock);
71}
72
73/*
74 * nss_l2tpv2_handler()
75 * Handle NSS -> HLOS messages for l2tpv2 tunnel
76 */
77
78static void nss_l2tpv2_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
79{
80 struct nss_l2tpv2_msg *ntm = (struct nss_l2tpv2_msg *)ncm;
81 void *ctx;
82
83 nss_l2tpv2_msg_callback_t cb;
84
85 BUG_ON(!(nss_is_dynamic_interface(ncm->interface) || ncm->interface == NSS_L2TPV2_INTERFACE));
86
87 /*
Sachin Shashidhar015c28a2018-08-06 19:11:00 -070088 * Trace Messages
89 */
90 nss_l2tpv2_log_rx_msg(ntm);
91
92 /*
ratheesh kannoth7af985d2015-06-24 15:08:40 +053093 * Is this a valid request/response packet?
94 */
95 if (ncm->type >= NSS_L2TPV2_MSG_MAX) {
96 nss_warning("%p: received invalid message %d for L2TP interface", nss_ctx, ncm->type);
97 return;
98 }
99
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800100 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_l2tpv2_msg)) {
101 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530102 return;
103 }
104
105 switch (ntm->cm.type) {
106
107 case NSS_L2TPV2_MSG_SYNC_STATS:
108 /*
109 * session debug stats embeded in session stats msg
110 */
111 nss_l2tpv2_session_debug_stats_sync(nss_ctx, &ntm->msg.stats, ncm->interface);
112 break;
113 }
114
115 /*
116 * Update the callback and app_data for NOTIFY messages, l2tpv2 sends all notify messages
117 * to the same callback/app_data.
118 */
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +0530119 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -0800120 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->l2tpv2_msg_callback;
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530121 }
122
123 /*
124 * Log failures
125 */
126 nss_core_log_msg_failures(nss_ctx, ncm);
127
128 /*
129 * Do we have a call back
130 */
131 if (!ncm->cb) {
132 return;
133 }
134
135 /*
136 * callback
137 */
138 cb = (nss_l2tpv2_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -0700139 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530140
141 /*
142 * call l2tpv2 tunnel callback
143 */
144 if (!ctx) {
145 nss_warning("%p: Event received for l2tpv2 tunnel interface %d before registration", nss_ctx, ncm->interface);
146 return;
147 }
148
149 cb(ctx, ntm);
150}
151
152/*
153 * nss_l2tpv2_tx()
154 * Transmit a l2tpv2 message to NSS firmware
155 */
156nss_tx_status_t nss_l2tpv2_tx(struct nss_ctx_instance *nss_ctx, struct nss_l2tpv2_msg *msg)
157{
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530158 struct nss_cmn_msg *ncm = &msg->cm;
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530159
160 /*
Sachin Shashidhar015c28a2018-08-06 19:11:00 -0700161 * Trace Messages
162 */
163 nss_l2tpv2_log_tx_msg(msg);
164
165 /*
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530166 * Sanity check the message
167 */
168 if (!nss_is_dynamic_interface(ncm->interface)) {
169 nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface);
170 return NSS_TX_FAILURE;
171 }
172
173 if (ncm->type > NSS_L2TPV2_MSG_MAX) {
174 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
175 return NSS_TX_FAILURE;
176 }
177
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700178 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530179}
180
181/*
182 ***********************************
183 * Register/Unregister/Miscellaneous APIs
184 ***********************************
185 */
186
187/*
188 * nss_register_l2tpv2_if()
189 */
190struct nss_ctx_instance *nss_register_l2tpv2_if(uint32_t if_num, nss_l2tpv2_callback_t l2tpv2_callback,
191 nss_l2tpv2_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
192{
Stephen Wang84e0e992016-09-07 12:31:40 -0700193 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id];
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530194 int i = 0;
Stephen Wang84e0e992016-09-07 12:31:40 -0700195
196 nss_assert(nss_ctx);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530197 nss_assert(nss_is_dynamic_interface(if_num));
198
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700199 nss_core_register_subsys_dp(nss_ctx, if_num, l2tpv2_callback, NULL, NULL, netdev, features);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530200
201 nss_top_main.l2tpv2_msg_callback = event_callback;
202
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700203 nss_core_register_handler(nss_ctx, if_num, nss_l2tpv2_handler, NULL);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530204
205 spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock);
206 for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) {
207 if (!nss_l2tpv2_session_debug_stats[i].valid) {
208 nss_l2tpv2_session_debug_stats[i].valid = true;
209 nss_l2tpv2_session_debug_stats[i].if_num = if_num;
210 nss_l2tpv2_session_debug_stats[i].if_index = netdev->ifindex;
211 break;
212 }
213 }
214 spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock);
215
Stephen Wang84e0e992016-09-07 12:31:40 -0700216 return nss_ctx;
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530217}
218
219/*
220 * nss_unregister_l2tpv2_if()
221 */
222void nss_unregister_l2tpv2_if(uint32_t if_num)
223{
Stephen Wang84e0e992016-09-07 12:31:40 -0700224 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id];
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530225 int i;
Stephen Wang84e0e992016-09-07 12:31:40 -0700226
227 nss_assert(nss_ctx);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530228 nss_assert(nss_is_dynamic_interface(if_num));
229
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700230 nss_core_unregister_subsys_dp(nss_ctx, if_num);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530231
232 nss_top_main.l2tpv2_msg_callback = NULL;
233
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700234 nss_core_unregister_handler(nss_ctx, if_num);
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530235
236 spin_lock_bh(&nss_l2tpv2_session_debug_stats_lock);
237 for (i = 0; i < NSS_MAX_L2TPV2_DYNAMIC_INTERFACES; i++) {
238 if (nss_l2tpv2_session_debug_stats[i].if_num == if_num) {
Yu Huang8c107082017-07-24 14:58:26 -0700239 memset(&nss_l2tpv2_session_debug_stats[i], 0, sizeof(struct nss_l2tpv2_stats_session_debug));
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530240 break;
241 }
242 }
243 spin_unlock_bh(&nss_l2tpv2_session_debug_stats_lock);
244}
245
246/*
247 * nss_get_l2tpv2_context()
248 */
249struct nss_ctx_instance *nss_l2tpv2_get_context()
250{
251 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.l2tpv2_handler_id];
252}
253
254/*
255 * nss_l2tpv2_msg_init()
Yu Huang8c107082017-07-24 14:58:26 -0700256 * Initialize nss_l2tpv2 msg.
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530257 */
258void nss_l2tpv2_msg_init(struct nss_l2tpv2_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
259{
260 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
261}
262
263/* nss_l2tpv2_register_handler()
264 * debugfs stats msg handler received on static l2tpv2 interface
265 */
266void nss_l2tpv2_register_handler(void)
267{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700268 struct nss_ctx_instance *nss_ctx = nss_l2tpv2_get_context();
269
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530270 nss_info("nss_l2tpv2_register_handler");
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700271 nss_core_register_handler(nss_ctx, NSS_L2TPV2_INTERFACE, nss_l2tpv2_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700272
273 nss_l2tpv2_stats_dentry_create();
ratheesh kannoth7af985d2015-06-24 15:08:40 +0530274}
275
276EXPORT_SYMBOL(nss_l2tpv2_get_context);
277EXPORT_SYMBOL(nss_l2tpv2_tx);
278EXPORT_SYMBOL(nss_unregister_l2tpv2_if);
279EXPORT_SYMBOL(nss_l2tpv2_msg_init);
280EXPORT_SYMBOL(nss_register_l2tpv2_if);