remove version in all files
[dpdk.git] / app / dump_cfg / dump_cfg_main.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <getopt.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <sys/mman.h>
44 #include <sys/queue.h>
45 #include <limits.h>
46
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_string_fns.h>
52
53
54 /* some functions used for printing out the memory segments and memory zones information */
55
56 #define PRINT_STR_FIELD(structname, field) do{ \
57         count+= rte_snprintf(buf + count, len-count, " %s='%s',", \
58                         #field, (const char *)structname->field);\
59 } while(0)
60
61 #define PRINT_PTR_FIELD(structname, field) do{ \
62         count+= rte_snprintf(buf + count, len-count, " %s=%p,", \
63                         #field, (void *)structname->field);\
64 } while(0)
65
66 #define PRINT_UINT_FIELD(structname, field) do{ \
67         count+= rte_snprintf(buf + count, len-count, " %s=%llu,", \
68                         #field, (unsigned long long)structname->field);\
69 } while(0)
70
71 #define PRINT_INT_FIELD(structname, field) do{ \
72         count+= rte_snprintf(buf + count, len-count, " %s=%lld,", \
73                         #field, (long long)structname->field);\
74 } while(0)
75
76 #define PRINT_CUSTOM_FIELD(structname, field, print_fn) do{ \
77         char buf2[1024]; \
78         count+= rte_snprintf(buf + count, len-count, " %s=%s,", \
79                         #field, print_fn(structname->field, buf2, sizeof(buf2)));\
80 } while(0)
81
82 static inline const char *
83 memseg_to_str(const struct rte_memseg *seg, char *buf, size_t len)
84 {
85         int count = 0;
86         count += rte_snprintf(buf + count, len - count, "{");
87         PRINT_UINT_FIELD(seg, phys_addr);
88         PRINT_PTR_FIELD(seg, addr);
89         PRINT_UINT_FIELD(seg, len);
90         PRINT_INT_FIELD(seg, socket_id);
91         PRINT_UINT_FIELD(seg, hugepage_sz);
92         PRINT_UINT_FIELD(seg, nchannel);
93         PRINT_UINT_FIELD(seg, nrank);
94         rte_snprintf(buf + count - 1, len - count + 1, " }");
95         return buf;
96 }
97
98 static inline const char *
99 memzone_to_str(const struct rte_memzone *zone, char *buf, size_t len)
100 {
101         int count = 0;
102         count += rte_snprintf(buf + count, len - count, "{");
103         PRINT_STR_FIELD(zone, name);
104         PRINT_UINT_FIELD(zone, phys_addr);
105         PRINT_PTR_FIELD(zone, addr);
106         PRINT_UINT_FIELD(zone, len);
107         PRINT_INT_FIELD(zone, socket_id);
108         PRINT_UINT_FIELD(zone, flags);
109         rte_snprintf(buf + count - 1, len - count + 1, " }");
110         return buf;
111 }
112
113 static inline const char *
114 tailq_to_str(const struct rte_tailq_head *tailq, char *buf, size_t len)
115 {
116         int count = 0;
117         count += rte_snprintf(buf + count, len - count, "{");
118         PRINT_STR_FIELD(tailq, qname);
119         const struct rte_dummy_head *head = &tailq->tailq_head;
120         PRINT_PTR_FIELD(head, tqh_first);
121         PRINT_PTR_FIELD(head, tqh_last);
122         rte_snprintf(buf + count - 1, len - count + 1, " }");
123         return buf;
124 }
125
126 #define PREFIX    "prefix"
127 static const char *directory = "/var/run";
128 static const char *pre = "rte";
129
130 static void
131 usage(const char *prgname)
132 {
133         printf("%s --prefix <prefix>\n\n"
134                         "dump_config option list:\n"
135                         "\t--"PREFIX": filename prefix\n",
136                         prgname);
137 }
138
139 static int
140 dmp_cfg_parse_args(int argc, char **argv)
141 {
142         const char *prgname = argv[0];
143         const char *home_dir = getenv("HOME");
144         int opt;
145         int option_index;
146         static struct option lgopts[] = {
147                         {PREFIX, 1, 0, 0},
148                         {0, 0, 0, 0}
149         };
150
151         if (getuid() != 0 && home_dir != NULL)
152                 directory = home_dir;
153
154         while ((opt = getopt_long(argc, argv, "",
155                         lgopts, &option_index)) != EOF) {
156                 switch (opt) {
157                 case 0:
158                         if (!strcmp(lgopts[option_index].name, PREFIX))
159                                 pre = optarg;
160                         else{
161                                 usage(prgname);
162                                 return -1;
163                         }
164                         break;
165
166                 default:
167                         usage(prgname);
168                         return -1;
169                 }
170         }
171         return 0;
172 }
173
174 int
175 main(int argc, char **argv)
176 {
177         char buffer[1024];
178         char path[PATH_MAX];
179         int i;
180         int fd = 0;
181
182         dmp_cfg_parse_args(argc, argv);
183         rte_snprintf(path, sizeof(path), "%s/.%s_config",  directory, pre);
184         printf("Path to mem_config: %s\n\n", path);
185
186         fd = open(path, O_RDWR);
187         if (fd < 0){
188                 printf("Error with config open\n");
189                 return 1;
190         }
191         struct rte_mem_config *cfg = mmap(NULL, sizeof(*cfg), PROT_READ, \
192                         MAP_SHARED, fd, 0);
193         if (cfg == NULL){
194                 printf("Error with config mmap\n");
195                 close(fd);
196         return 1;
197         }
198         close(fd);
199
200         printf("----------- MEMORY_SEGMENTS -------------\n");
201         for (i = 0; i < RTE_MAX_MEMSEG; i++){
202                 if (cfg->memseg[i].addr == NULL) break;
203                 printf("Segment %d: ", i);
204                 printf("%s\n", memseg_to_str(&cfg->memseg[i], buffer, sizeof(buffer)));
205         }
206         printf("--------- END_MEMORY_SEGMENTS -----------\n");
207
208         printf("------------ MEMORY_ZONES ---------------\n");
209         for (i = 0; i < RTE_MAX_MEMZONE; i++){
210                 if (cfg->memzone[i].addr == NULL) break;
211                 printf("Zone %d: ", i);
212                 printf("%s\n", memzone_to_str(&cfg->memzone[i], buffer, sizeof(buffer)));
213
214         }
215         printf("---------- END_MEMORY_ZONES -------------\n");
216
217         printf("------------- TAIL_QUEUES ---------------\n");
218         for (i = 0; i < RTE_MAX_TAILQ; i++){
219                 if (cfg->tailq_head[i].qname[0] == '\0') break;
220                 printf("Tailq %d: ", i);
221                 printf("%s\n", tailq_to_str(&cfg->tailq_head[i], buffer, sizeof(buffer)));
222
223         }
224         printf("----------- END_TAIL_QUEUES -------------\n");
225
226         return 0;
227 }
228