Return packet mmap size with "dabba capture list"
[dabba.git] / dabba / capture.c
blob777d4836db53b2a465dfc921f287409dd30532e5
1 /* __LICENSE_HEADER_BEGIN__ */
3 /*
4 * Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22 /* __LICENSE_HEADER_END__ */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <getopt.h>
29 #include <inttypes.h>
30 #include <errno.h>
32 #include <dabbacore/macros.h>
33 #include <dabbacore/strlcpy.h>
34 #include <dabbacore/packet_mmap.h>
35 #include <dabba/dabba.h>
36 #include <dabba/help.h>
37 #include <dabba/ipc.h>
38 #include <dabbad/dabbad.h>
40 enum capture_start_option {
41 OPT_CAPTURE_DEVICE,
42 OPT_CAPTURE_PCAP,
43 OPT_CAPTURE_SIZE
46 static struct option *capture_start_options_get(void)
48 static struct option capture_start_option[] = {
49 {"device", required_argument, NULL, OPT_CAPTURE_DEVICE},
50 {"pcap", required_argument, NULL, OPT_CAPTURE_PCAP},
51 {"size", required_argument, NULL, OPT_CAPTURE_SIZE},
52 {NULL, 0, NULL, 0},
55 return capture_start_option;
58 static int prepare_capture_start_query(int argc, char **argv,
59 struct dabba_ipc_msg *msg)
61 struct dabba_capture *capture_start_msg = msg->msg_body.msg.capture;
62 int ret = 0;
63 int rc = 0;
65 assert(msg);
66 msg->mtype = 1;
67 msg->msg_body.type = DABBA_CAPTURE_START;
69 while ((ret =
70 getopt_long_only(argc, argv, "", capture_start_options_get(),
71 NULL)) != EOF) {
72 switch (ret) {
73 case OPT_CAPTURE_DEVICE:
74 strlcpy(capture_start_msg->dev_name, optarg,
75 sizeof(capture_start_msg->dev_name));
76 break;
78 case OPT_CAPTURE_PCAP:
79 strlcpy(capture_start_msg->pcap_name, optarg,
80 sizeof(capture_start_msg->pcap_name));
81 break;
82 case OPT_CAPTURE_SIZE:
83 capture_start_msg->size = strtoull(optarg, NULL, 10);
84 break;
85 default:
86 show_usage(capture_start_options_get());
87 rc = -1;
88 break;
92 /* Assume conservative values for now */
93 capture_start_msg->page_order = 10;
94 capture_start_msg->frame_size = PACKET_MMAP_ETH_FRAME_LEN;
96 return rc;
99 static void prepare_capture_list_query(struct dabba_ipc_msg *msg)
101 assert(msg);
102 msg->mtype = 1;
103 msg->msg_body.type = DABBA_CAPTURE_LIST;
106 static void display_capture_list_msg_header(void)
108 printf("---\n");
109 printf(" captures:\n");
112 static void display_capture_list(const struct dabba_ipc_msg const *msg)
114 size_t a;
116 assert(msg);
117 assert(msg->msg_body.elem_nr <= ARRAY_SIZE(msg->msg_body.msg.capture));
119 for (a = 0; a < msg->msg_body.elem_nr; a++) {
120 printf(" - id: %u\n",
121 (uint32_t) msg->msg_body.msg.capture[a].thread_id);
122 printf(" packet mmap size: %" PRIu64 "\n",
123 msg->msg_body.msg.capture[a].size);
127 int cmd_capture_start(int argc, const char **argv)
129 struct dabba_ipc_msg msg;
130 int rc;
132 assert(argc >= 0);
133 assert(argv);
135 memset(&msg, 0, sizeof(msg));
137 rc = prepare_capture_start_query(argc, (char **)argv, &msg);
139 if (rc)
140 return rc;
142 /* For now, just one capture request at a time */
143 msg.msg_body.elem_nr = 1;
145 return dabba_ipc_msg(&msg);
148 int cmd_capture_list(int argc, const char **argv)
150 int rc;
151 struct dabba_ipc_msg msg;
153 assert(argc >= 0);
154 assert(argv);
156 memset(&msg, 0, sizeof(msg));
157 prepare_capture_list_query(&msg);
158 display_capture_list_msg_header();
160 do {
161 msg.msg_body.offset += msg.msg_body.elem_nr;
162 msg.msg_body.elem_nr = 0;
164 rc = dabba_ipc_msg(&msg);
166 if (rc)
167 break;
169 display_capture_list(&msg);
170 } while (msg.msg_body.elem_nr);
172 return rc;
175 int cmd_capture(int argc, const char **argv)
177 const char *cmd = argv[0];
178 size_t i;
179 static struct cmd_struct capture_commands[] = {
180 {"list", cmd_capture_list, 0},
181 {"start", cmd_capture_start, 0}
184 /* TODO --help handling here */
186 for (i = 0; ARRAY_SIZE(capture_commands); i++) {
187 struct cmd_struct *p = capture_commands + i;
188 if (strcmp(p->cmd, cmd))
189 continue;
190 return (run_builtin(p, argc, argv));
193 return ENOSYS;