first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <getopt.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <sys/mman.h>
45 #include <sys/queue.h>
46 #include <limits.h>
47
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_string_fns.h>
53
54
55 /* some functions used for printing out the memory segments and memory zones information */
56
57 #define PRINT_STR_FIELD(structname, field) do{ \
58         count+= rte_snprintf(buf + count, len-count, " %s='%s',", \
59                         #field, (const char *)structname->field);\
60 } while(0)
61
62 #define PRINT_PTR_FIELD(structname, field) do{ \
63         count+= rte_snprintf(buf + count, len-count, " %s=%p,", \
64                         #field, (void *)structname->field);\
65 } while(0)
66
67 #define PRINT_UINT_FIELD(structname, field) do{ \
68         count+= rte_snprintf(buf + count, len-count, " %s=%llu,", \
69                         #field, (unsigned long long)structname->field);\
70 } while(0)
71
72 #define PRINT_INT_FIELD(structname, field) do{ \
73         count+= rte_snprintf(buf + count, len-count, " %s=%lld,", \
74                         #field, (long long)structname->field);\
75 } while(0)
76
77 #define PRINT_CUSTOM_FIELD(structname, field, print_fn) do{ \
78         char buf2[1024]; \
79         count+= rte_snprintf(buf + count, len-count, " %s=%s,", \
80                         #field, print_fn(structname->field, buf2, sizeof(buf2)));\
81 } while(0)
82
83 static inline const char *
84 memseg_to_str(const struct rte_memseg *seg, char *buf, size_t len)
85 {
86         int count = 0;
87         count += rte_snprintf(buf + count, len - count, "{");
88         PRINT_UINT_FIELD(seg, phys_addr);
89         PRINT_PTR_FIELD(seg, addr);
90         PRINT_UINT_FIELD(seg, len);
91         PRINT_INT_FIELD(seg, socket_id);
92         PRINT_UINT_FIELD(seg, hugepage_sz);
93         PRINT_UINT_FIELD(seg, nchannel);
94         PRINT_UINT_FIELD(seg, nrank);
95         rte_snprintf(buf + count - 1, len - count + 1, " }");
96         return buf;
97 }
98
99 static inline const char *
100 memzone_to_str(const struct rte_memzone *zone, char *buf, size_t len)
101 {
102         int count = 0;
103         count += rte_snprintf(buf + count, len - count, "{");
104         PRINT_STR_FIELD(zone, name);
105         PRINT_UINT_FIELD(zone, phys_addr);
106         PRINT_PTR_FIELD(zone, addr);
107         PRINT_UINT_FIELD(zone, len);
108         PRINT_INT_FIELD(zone, socket_id);
109         PRINT_UINT_FIELD(zone, flags);
110         rte_snprintf(buf + count - 1, len - count + 1, " }");
111         return buf;
112 }
113
114 static inline const char *
115 tailq_to_str(const struct rte_tailq_head *tailq, char *buf, size_t len)
116 {
117         int count = 0;
118         count += rte_snprintf(buf + count, len - count, "{");
119         PRINT_STR_FIELD(tailq, qname);
120         const struct rte_dummy_head *head = &tailq->tailq_head;
121         PRINT_PTR_FIELD(head, tqh_first);
122         PRINT_PTR_FIELD(head, tqh_last);
123         rte_snprintf(buf + count - 1, len - count + 1, " }");
124         return buf;
125 }
126
127 #define PREFIX    "prefix"
128 static const char *directory = "/var/run";
129 static const char *pre = "rte";
130
131 static void
132 usage(const char *prgname)
133 {
134         printf("%s --prefix <prefix>\n\n"
135                         "dump_config option list:\n"
136                         "\t--"PREFIX": filename prefix\n",
137                         prgname);
138 }
139
140 static int
141 dmp_cfg_parse_args(int argc, char **argv)
142 {
143         const char *prgname = argv[0];
144         const char *home_dir = getenv("HOME");
145         int opt;
146         int option_index;
147         static struct option lgopts[] = {
148                         {PREFIX, 1, 0, 0},
149                         {0, 0, 0, 0}
150         };
151
152         if (getuid() != 0 && home_dir != NULL)
153                 directory = home_dir;
154
155         while ((opt = getopt_long(argc, argv, "",
156                         lgopts, &option_index)) != EOF) {
157                 switch (opt) {
158                 case 0:
159                         if (!strcmp(lgopts[option_index].name, PREFIX))
160                                 pre = optarg;
161                         else{
162                                 usage(prgname);
163                                 return -1;
164                         }
165                         break;
166
167                 default:
168                         usage(prgname);
169                         return -1;
170                 }
171         }
172         return 0;
173 }
174
175 int
176 main(int argc, char **argv)
177 {
178         char buffer[1024];
179         char path[PATH_MAX];
180         int i;
181         int fd = 0;
182
183         dmp_cfg_parse_args(argc, argv);
184         rte_snprintf(path, sizeof(path), "%s/.%s_config",  directory, pre);
185         printf("Path to mem_config: %s\n\n", path);
186
187         fd = open(path, O_RDWR);
188         if (fd < 0){
189                 printf("Error with config open\n");
190                 return 1;
191         }
192         struct rte_mem_config *cfg = mmap(NULL, sizeof(*cfg), PROT_READ, \
193                         MAP_SHARED, fd, 0);
194         if (cfg == NULL){
195                 printf("Error with config mmap\n");
196                 close(fd);
197         return 1;
198         }
199         close(fd);
200
201         printf("----------- MEMORY_SEGMENTS -------------\n");
202         for (i = 0; i < RTE_MAX_MEMSEG; i++){
203                 if (cfg->memseg[i].addr == NULL) break;
204                 printf("Segment %d: ", i);
205                 printf("%s\n", memseg_to_str(&cfg->memseg[i], buffer, sizeof(buffer)));
206         }
207         printf("--------- END_MEMORY_SEGMENTS -----------\n");
208
209         printf("------------ MEMORY_ZONES ---------------\n");
210         for (i = 0; i < RTE_MAX_MEMZONE; i++){
211                 if (cfg->memzone[i].addr == NULL) break;
212                 printf("Zone %d: ", i);
213                 printf("%s\n", memzone_to_str(&cfg->memzone[i], buffer, sizeof(buffer)));
214
215         }
216         printf("---------- END_MEMORY_ZONES -------------\n");
217
218         printf("------------- TAIL_QUEUES ---------------\n");
219         for (i = 0; i < RTE_MAX_TAILQ; i++){
220                 if (cfg->tailq_head[i].qname[0] == '\0') break;
221                 printf("Tailq %d: ", i);
222                 printf("%s\n", tailq_to_str(&cfg->tailq_head[i], buffer, sizeof(buffer)));
223
224         }
225         printf("----------- END_TAIL_QUEUES -------------\n");
226
227         return 0;
228 }
229