blob: 432b1546b28751fc6a749edbe9a7e0d9094ad92b [file] [log] [blame]
Wayne Tanb45933c2020-01-06 17:19:25 -08001/*
2 **************************************************************************
3 * Copyright (c) 2020, 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_strings.c
19 * NSS driver strings APIs.
20 */
21
22#include "nss_strings.h"
23#include "nss_core.h"
Wayne Tand5058cb2020-01-07 14:39:16 -080024#include "nss_drv_strings.h"
Wayne Tanb45933c2020-01-06 17:19:25 -080025
26/*
27 * common stats
28 */
29struct nss_stats_info nss_strings_stats_node[NSS_STATS_NODE_MAX] = {
30 {"rx_pkts" , NSS_STATS_TYPE_COMMON},
31 {"rx_byts" , NSS_STATS_TYPE_COMMON},
32 {"tx_pkts" , NSS_STATS_TYPE_COMMON},
33 {"tx_byts" , NSS_STATS_TYPE_COMMON},
34 {"rx_queue[0]_drops" , NSS_STATS_TYPE_DROP},
35 {"rx_queue[1]_drops" , NSS_STATS_TYPE_DROP},
36 {"rx_queue[2]_drops" , NSS_STATS_TYPE_DROP},
37 {"rx_queue[3]_drops" , NSS_STATS_TYPE_DROP}
38};
39
40/*
41 * nss_strings_print()
42 * Helper API to print stats names
43 */
44size_t nss_strings_print(char __user *ubuf, size_t sz, loff_t *ppos, struct nss_stats_info *stats_info, uint16_t max)
45{
46 int32_t i;
47 size_t size_al = (NSS_STATS_MAX_STR_LENGTH + 12) * max;
48 size_t size_wr = 0;
49 ssize_t bytes_read = 0;
50
51 char *lbuf = kzalloc(size_al, GFP_KERNEL);
52 if (!lbuf) {
53 nss_warning("Could not allocate memory for local statistics buffer");
54 return 0;
55 }
56
57 for (i = 0; i < max; i++) {
58 /*
59 * Print what we have but don't exceed the buffer.
60 */
61 if (size_wr >= size_al) {
62 nss_info_always("Buffer overflowed.\n");
63 break;
64 }
65 size_wr += scnprintf(lbuf + size_wr, size_al - size_wr,
66 "\t%d , %s\n", stats_info[i].stats_type, stats_info[i].stats_name);
67 }
68
69 bytes_read = simple_read_from_buffer(ubuf, sz, ppos, lbuf, strlen(lbuf));
70 kfree(lbuf);
71
72 return bytes_read;
73}
74
75/*
76 * nss_strings_create_dentry()
77 * Create strings debug entry for subsystem.
78 */
79void nss_strings_create_dentry(char *name, const struct file_operations *ops)
80{
81 if (!nss_top_main.strings_dentry || !debugfs_create_file(name, 0400, nss_top_main.strings_dentry, &nss_top_main, ops)) {
82 nss_warning("Failed to create debug entry for subsystem %s\n", name);
83 }
84}
85
86/*
87 * nss_strings_open()
88 */
89int nss_strings_open(struct inode *inode, struct file *filp)
90{
91 struct nss_strings_data *data = NULL;
92
93 data = kzalloc(sizeof(struct nss_strings_data), GFP_KERNEL);
94 if (!data) {
95 return -ENOMEM;
96 }
97 data->if_num = NSS_DYNAMIC_IF_START;
98 data->nss_ctx = (struct nss_ctx_instance *)(inode->i_private);
99 filp->private_data = data;
100
101 return 0;
102}
103
104/*
105 * nss_strings_release()
106 */
107int nss_strings_release(struct inode *inode, struct file *filp)
108{
109 struct nss_strings_data *data = filp->private_data;
110
111 if (data) {
112 kfree(data);
113 }
114
115 return 0;
116}
117
118/*
119 * nss_common_node_stats_strings_read()
120 * Read common node statistics names.
121 */
122static ssize_t nss_common_node_stats_strings_read(struct file *fp, char __user *ubuf, size_t sz, loff_t *ppos)
123{
124 return nss_strings_print(ubuf, sz, ppos, nss_strings_stats_node, NSS_STATS_NODE_MAX);
125}
126
127/*
128 * nss_common_node_stats_strings_ops
129 */
130NSS_STRINGS_DECLARE_FILE_OPERATIONS(common_node_stats);
131
132/*
133 * nss_strings_init()
134 * Enable NSS statistics
135 */
136void nss_strings_init(void)
137{
138 nss_top_main.strings_dentry = debugfs_create_dir("strings", nss_top_main.top_dentry);
139 if (unlikely(nss_top_main.strings_dentry == NULL)) {
140 nss_warning("Failed to create strings directory in debugfs/qca-nss-drv");
141 return;
142 }
143
144 /*
145 * Common node statistics
146 */
147 nss_strings_create_dentry("common_node_stats", &nss_common_node_stats_strings_ops);
148}