Debian Codegraph @

src/main.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2009, Neil Horman <[email protected]>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6 /*
7 * Opens our netlink socket. Returns the socket descriptor or < 0 on error
8 */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <signal.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <getopt.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/socket.h>
22 #include <sys/queue.h>
23 #include <readline/readline.h>
24 #include <readline/history.h>
25 #include <asm/types.h>
26 #include <netlink/netlink.h>
27 #include <netlink/genl/genl.h>
28 #include <netlink/genl/ctrl.h>
29
30 #include "net_dropmon.h"
31 #include "lookup.h"
32
33 /*
34 * This is just in place until the kernel changes get committed
35 */
36 #ifndef NETLINK_DRPMON
37 #define NETLINK_DRPMON 20
38 #endif
39
40 struct netlink_message {
41 void *msg;
42 struct nl_msg *nlbuf;
43 int refcnt;
44 LIST_ENTRY(netlink_message) ack_list_element;
45 int seq;
46 void (*ack_cb)(struct netlink_message *amsg, struct netlink_message *msg, int err);
47 };
48
49 LIST_HEAD(ack_list, netlink_message);
50
51 struct ack_list ack_list_head = {NULL};
52
53 unsigned long alimit = 0;
54 unsigned long acount = 0;
55 unsigned long trunc_len = 0;
56 unsigned long queue_len = 0;
57 bool monitor_sw = false;
58 bool monitor_hw = false;
59
60 void handle_dm_alert_msg(struct netlink_message *msg, int err);
61 void handle_dm_packet_alert_msg(struct netlink_message *msg, int err);
62 void handle_dm_config_new_msg(struct netlink_message *msg, int err);
63 void handle_dm_stats_new_msg(struct netlink_message *msg, int err);
64 void handle_dm_config_msg(struct netlink_message *amsg, struct netlink_message *msg, int err);
65 void handle_dm_start_msg(struct netlink_message *amsg, struct netlink_message *msg, int err);
66 void handle_dm_stop_msg(struct netlink_message *amsg, struct netlink_message *msg, int err);
67 int disable_drop_monitor();
68
69 static void(*type_cb[_NET_DM_CMD_MAX])(struct netlink_message *, int err) = {
70 NULL,
71 handle_dm_alert_msg,
72 NULL,
73 NULL,
74 NULL,
75 handle_dm_packet_alert_msg,
76 NULL,
77 handle_dm_config_new_msg,
78 NULL,
79 handle_dm_stats_new_msg,
80 };
81
82 static struct nl_sock *nsd;
83 static int nsf;
84
85 enum {
86 STATE_IDLE = 0,
87 STATE_ACTIVATING,
88 STATE_RECEIVING,
89 STATE_RQST_DEACTIVATE,
90 STATE_RQST_ACTIVATE,
91 STATE_DEACTIVATING,
92 STATE_FAILED,
93 STATE_EXIT,
94 STATE_RQST_ALERT_MODE_SUMMARY,
95 STATE_RQST_ALERT_MODE_PACKET,
96 STATE_ALERT_MODE_SETTING,
97 STATE_RQST_TRUNC_LEN,
98 STATE_TRUNC_LEN_SETTING,
99 STATE_RQST_QUEUE_LEN,
100 STATE_QUEUE_LEN_SETTING,
101 STATE_RQST_CONFIG,
102 STATE_CONFIG_GETTING,
103 STATE_RQST_STATS,
104 STATE_STATS_GETTING,
105 };
106
107 static int state = STATE_IDLE;
108
109 static struct nla_policy net_dm_policy[NET_DM_ATTR_MAX + 1] = {
110 [NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
111 [NET_DM_ATTR_PC] = { .type = NLA_U64 },
112 [NET_DM_ATTR_SYMBOL] = { .type = NLA_STRING },
113 [NET_DM_ATTR_IN_PORT] = { .type = NLA_NESTED },
114 [NET_DM_ATTR_TIMESTAMP] = { .type = NLA_U64 },
115 [NET_DM_ATTR_PROTO] = { .type = NLA_U16 },
116 [NET_DM_ATTR_PAYLOAD] = { .type = NLA_UNSPEC },
117 [NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
118 [NET_DM_ATTR_ORIG_LEN] = { .type = NLA_U32 },
119 [NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
120 [NET_DM_ATTR_STATS] = { .type = NLA_NESTED },
121 [NET_DM_ATTR_HW_STATS] = { .type = NLA_NESTED },
122 [NET_DM_ATTR_ORIGIN] = { .type = NLA_U16 },
123 [NET_DM_ATTR_HW_TRAP_GROUP_NAME] = { .type = NLA_STRING },
124 [NET_DM_ATTR_HW_TRAP_NAME] = { .type = NLA_STRING },
125 [NET_DM_ATTR_HW_ENTRIES] = { .type = NLA_NESTED },
126 [NET_DM_ATTR_HW_ENTRY] = { .type = NLA_NESTED },
127 [NET_DM_ATTR_HW_TRAP_COUNT] = { .type = NLA_U32 },
128 };
129
130 static struct nla_policy net_dm_port_policy[NET_DM_ATTR_PORT_MAX + 1] = {
131 [NET_DM_ATTR_PORT_NETDEV_IFINDEX] = { .type = NLA_U32 },
132 [NET_DM_ATTR_PORT_NETDEV_NAME] = { .type = NLA_STRING },
133 };
134
135 static struct nla_policy net_dm_stats_policy[NET_DM_ATTR_STATS_MAX + 1] = {
136 [NET_DM_ATTR_STATS_DROPPED] = { .type = NLA_U64 },
137 };
138
139 int strtobool(const char *str, bool *p_val)
140 {
141 bool val;
142
143 if (!strcmp(str, "true") || !strcmp(str, "1"))
144 val = true;
145 else if (!strcmp(str, "false") || !strcmp(str, "0"))
146 val = false;
147 else
148 return -EINVAL;
149 *p_val = val;
150 return 0;
151 }
152
153 void sigint_handler(int signum)
154 {
155 if ((state == STATE_RECEIVING) ||
156 (state == STATE_RQST_DEACTIVATE)) {
157 disable_drop_monitor();
158 state = STATE_DEACTIVATING;
159 } else {
160 printf("Got a sigint while not receiving\n");
161 }
162 return;
163 }
164
165 struct nl_sock *setup_netlink_socket()
166 {
167 struct nl_sock *sd;
168 int family;
169
170 sd = nl_socket_alloc();
171
172 genl_connect(sd);
173
174 family = genl_ctrl_resolve(sd, "NET_DM");
175
176 if (family < 0) {
177 printf("Unable to find NET_DM family, dropwatch can't work\n");
178 goto out_close;
179 }
180
181 nsf = family;
182
183 nl_close(sd);
184 nl_socket_free(sd);
185
186 sd = nl_socket_alloc();
187 nl_join_groups(sd, NET_DM_GRP_ALERT);
188
189 nl_connect(sd, NETLINK_GENERIC);
190
191 return sd;
192
193 out_close:
194 nl_close(sd);
195 nl_socket_free(sd);
196 return NULL;
197 }
198
199 struct netlink_message *alloc_netlink_msg(uint32_t type, uint16_t flags, size_t size)
200 {
201 struct netlink_message *msg;
202 static uint32_t seq = 0;
203
204 msg = (struct netlink_message *)malloc(sizeof(struct netlink_message));
205
206 if (!msg)
207 return NULL;
208
209 msg->refcnt = 1;
210 msg->nlbuf = nlmsg_alloc();
211 msg->msg = genlmsg_put(msg->nlbuf, 0, seq, nsf, size, flags, type, 1);
212
213 msg->ack_cb = NULL;
214 msg->seq = seq++;
215
216 return msg;
217 }
218
219 void set_ack_cb(struct netlink_message *msg,
220 void (*cb)(struct netlink_message *, struct netlink_message *, int))
221 {
222 if (msg->ack_cb)
223 return;
224
225 msg->ack_cb = cb;
226 msg->refcnt++;
227 LIST_INSERT_HEAD(&ack_list_head, msg, ack_list_element);
228 }
229
230 struct netlink_message *wrap_netlink_msg(struct nlmsghdr *buf)
231 {
232 struct netlink_message *msg;
233
234 msg = (struct netlink_message *)malloc(sizeof(struct netlink_message));
235 if (msg) {
236 msg->refcnt = 1;
237 msg->msg = buf;
238 msg->nlbuf = NULL;
239 }
240
241 return msg;
242 }
243
244 int free_netlink_msg(struct netlink_message *msg)
245 {
246 int refcnt;
247
248 msg->refcnt--;
249
250 refcnt = msg->refcnt;
251
252 if (!refcnt) {
253 if (msg->nlbuf)
254 nlmsg_free(msg->nlbuf);
255 else
256 free(msg->msg);
257 free(msg);
258 }
259
260 return refcnt;
261 }
262
263 int send_netlink_message(struct netlink_message *msg)
264 {
265 return nl_send(nsd, msg->nlbuf);
266 }
267
268 struct netlink_message *recv_netlink_message(int *err)
269 {
270 static struct nlmsghdr *buf;
271 struct netlink_message *msg;
272 struct genlmsghdr *glm;
273 struct sockaddr_nl nla;
274 int type;
275 int rc;
276
277 *err = 0;
278
279 do {
280 rc = nl_recv(nsd, &nla, (unsigned char **)&buf, NULL);
281 if (rc < 0) {
282 switch (errno) {
283 case EINTR:
284 /*
285 * Take a pass through the state loop
286 */
287 return NULL;
288 break;
289 default:
290 perror("Receive operation failed:");
291 return NULL;
292 break;
293 }
294 }
295 } while (rc == 0);
296
297 msg = wrap_netlink_msg(buf);
298
299 type = ((struct nlmsghdr *)msg->msg)->nlmsg_type;
300
301 /*
302 * Note the NLMSG_ERROR is overloaded
303 * Its also used to deliver ACKs
304 */
305 if (type == NLMSG_ERROR) {
306 struct netlink_message *am;
307 struct nlmsgerr *errm = nlmsg_data(msg->msg);
308 LIST_FOREACH(am, &ack_list_head, ack_list_element) {
309 if (am->seq == errm->msg.nlmsg_seq)
310 break;
311 }
312
313 if (am) {
314 LIST_REMOVE(am, ack_list_element);
315 am->ack_cb(msg, am, errm->error);
316 free_netlink_msg(am);
317 } else {
318 printf("Got an unexpected ack for sequence %d\n", errm->msg.nlmsg_seq);
319 }
320
321 free_netlink_msg(msg);
322 return NULL;
323 }
324
325 glm = nlmsg_data(msg->msg);
326 type = glm->cmd;
327
328 if ((type > NET_DM_CMD_MAX) ||
329 (type <= NET_DM_CMD_UNSPEC)) {
330 printf("Received message of unknown type %d\n",
331 type);
332 free_netlink_msg(msg);
333 return NULL;
334 }
335
336 return msg;
337 }
338
339 void process_rx_message(void)
340 {
341 struct netlink_message *msg;
342 int err;
343 int type;
344 sigset_t bs;
345
346 sigemptyset(&bs);
347 sigaddset(&bs, SIGINT);
348 sigprocmask(SIG_UNBLOCK, &bs, NULL);
349 msg = recv_netlink_message(&err);
350 sigprocmask(SIG_BLOCK, &bs, NULL);
351
352 if (msg) {
353 struct nlmsghdr *nlh = msg->msg;
354 struct genlmsghdr *glh = nlmsg_data(nlh);
355 type = glh->cmd;
356 type_cb[type](msg, err);
357 }
358 return;
359 }
360
361 void print_nested_hw_entry(struct nlattr *hw_entry)
362 {
363 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
364 int err;
365
366 err = nla_parse_nested(attrs, NET_DM_ATTR_MAX, hw_entry, net_dm_policy);
367 if (err)
368 return;
369
370 if (!attrs[NET_DM_ATTR_HW_TRAP_NAME] ||
371 !attrs[NET_DM_ATTR_HW_TRAP_COUNT])
372 return;
373
374 printf("%d drops at %s [hardware]\n",
375 nla_get_u32(attrs[NET_DM_ATTR_HW_TRAP_COUNT]),
376 nla_get_string(attrs[NET_DM_ATTR_HW_TRAP_NAME]));
377 }
378
379 void print_nested_hw_entries(struct nlattr *hw_entries)
380 {
381 struct nlattr *attr;
382 int rem;
383
384 nla_for_each_nested(attr, hw_entries, rem) {
385 if (nla_type(attr) != NET_DM_ATTR_HW_ENTRY)
386 continue;
387 print_nested_hw_entry(attr);
388
389 acount++;
390 if (alimit && (acount == alimit)) {
391 printf("Alert limit reached, deactivating!\n");
392 state = STATE_RQST_DEACTIVATE;
393 }
394 }
395 }
396
397 /*
398 * These are the received message handlers
399 */
400 void handle_dm_alert_msg(struct netlink_message *msg, int err)
401 {
402 int i;
403 struct nlmsghdr *nlh = msg->msg;
404 struct genlmsghdr *glh = nlmsg_data(nlh);
405 struct loc_result res;
406 struct net_dm_alert_msg *alert = nla_data(genlmsg_data(glh));
407 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
408
409 if (state != STATE_RECEIVING)
410 goto out_free;
411
412 err = genlmsg_parse(msg->msg, 0, attrs, NET_DM_ATTR_MAX, net_dm_policy);
413 if (err)
414 goto out_free;
415
416 for (i=0; i < alert->entries; i++) {
417 void *location;
418 memcpy(&location, alert->points[i].pc, sizeof(void *));
419 if (lookup_symbol(location, &res))
420 printf ("%d drops at location %p [software]\n", alert->points[i].count, location);
421 else
422 printf ("%d drops at %s+%llx (%p) [software]\n",
423 alert->points[i].count, res.symbol, (unsigned long long)res.offset, location);
424 acount++;
425 if (alimit && (acount == alimit)) {
426 printf("Alert limit reached, deactivating!\n");
427 state = STATE_RQST_DEACTIVATE;
428 }
429 }
430
431 if (attrs[NET_DM_ATTR_HW_ENTRIES])
432 print_nested_hw_entries(attrs[NET_DM_ATTR_HW_ENTRIES]);
433
434 out_free:
435 free_netlink_msg(msg);
436 }
437
438 void print_nested_port(struct nlattr *attr, const char *dir)
439 {
440 struct nlattr *attrs[NET_DM_ATTR_PORT_MAX + 1];
441 int err;
442
443 err = nla_parse_nested(attrs, NET_DM_ATTR_PORT_MAX, attr,
444 net_dm_port_policy);
445 if (err)
446 return;
447
448 if (attrs[NET_DM_ATTR_PORT_NETDEV_IFINDEX])
449 printf("%s port ifindex: %d\n", dir,
450 nla_get_u32(attrs[NET_DM_ATTR_PORT_NETDEV_IFINDEX]));
451
452 if (attrs[NET_DM_ATTR_PORT_NETDEV_NAME])
453 printf("%s port name: %s\n", dir,
454 nla_get_string(attrs[NET_DM_ATTR_PORT_NETDEV_NAME]));
455 }
456
457 void print_packet_origin(struct nlattr *attr)
458 {
459 const char *origin;
460 uint16_t val;
461
462 val = nla_get_u16(attr);
463 switch (val) {
464 case NET_DM_ORIGIN_SW:
465 origin = "software";
466 break;
467 case NET_DM_ORIGIN_HW:
468 origin = "hardware";
469 break;
470 default:
471 origin = "unknown";
472 break;
473 }
474
475 printf("origin: %s\n", origin);
476 }
477
478 void handle_dm_packet_alert_msg(struct netlink_message *msg, int err)
479 {
480 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
481
482 if (state != STATE_RECEIVING)
483 goto out_free;
484
485 err = genlmsg_parse(msg->msg, 0, attrs, NET_DM_ATTR_MAX, net_dm_policy);
486 if (err)
487 goto out_free;
488
489 if (attrs[NET_DM_ATTR_PC] && attrs[NET_DM_ATTR_SYMBOL])
490 printf("drop at: %s (0x%" PRIx64 ")\n",
491 nla_get_string(attrs[NET_DM_ATTR_SYMBOL]),
492 nla_get_u64(attrs[NET_DM_ATTR_PC]));
493 else if (attrs[NET_DM_ATTR_HW_TRAP_GROUP_NAME] &&
494 attrs[NET_DM_ATTR_HW_TRAP_NAME])
495 printf("drop at: %s (%s)\n",
496 nla_get_string(attrs[NET_DM_ATTR_HW_TRAP_NAME]),
497 nla_get_string(attrs[NET_DM_ATTR_HW_TRAP_GROUP_NAME]));
498
499 if (attrs[NET_DM_ATTR_ORIGIN])
500 print_packet_origin(attrs[NET_DM_ATTR_ORIGIN]);
501
502 if (attrs[NET_DM_ATTR_IN_PORT])
503 print_nested_port(attrs[NET_DM_ATTR_IN_PORT], "input");
504
505 if (attrs[NET_DM_ATTR_FLOW_ACTION_COOKIE]) {
506 unsigned char *cookie = nla_data(attrs[NET_DM_ATTR_FLOW_ACTION_COOKIE]);
507 int cookie_len = nla_len(attrs[NET_DM_ATTR_FLOW_ACTION_COOKIE]);
508 int i;
509
510 printf("cookie: ");
511 for (i = 0; i < cookie_len; i++)
512 printf("%02x", cookie[i]);
513 printf("\n");
514 }
515
516 if (attrs[NET_DM_ATTR_TIMESTAMP]) {
517 time_t tv_sec;
518 struct tm *tm;
519 uint64_t ts;
520 char *tstr;
521
522 ts = nla_get_u64(attrs[NET_DM_ATTR_TIMESTAMP]);
523 tv_sec = ts / 1000000000;
524 tm = localtime(&tv_sec);
525
526 tstr = asctime(tm);
527 tstr[strlen(tstr) - 1] = 0;
528 printf("timestamp: %s %09" PRId64 " nsec\n", tstr, ts % 1000000000);
529 }
530
531 if (attrs[NET_DM_ATTR_PROTO])
532 printf("protocol: 0x%x\n",
533 nla_get_u16(attrs[NET_DM_ATTR_PROTO]));
534
535 if (attrs[NET_DM_ATTR_PAYLOAD])
536 printf("length: %u\n", nla_len(attrs[NET_DM_ATTR_PAYLOAD]));
537
538 if (attrs[NET_DM_ATTR_ORIG_LEN])
539 printf("original length: %u\n",
540 nla_get_u32(attrs[NET_DM_ATTR_ORIG_LEN]));
541
542 if (attrs[NET_DM_ATTR_REASON])
543 printf("drop reason: %s\n",
544 nla_get_string(attrs[NET_DM_ATTR_REASON]));
545
546 printf("\n");
547
548 acount++;
549 if (alimit && (acount == alimit)) {
550 printf("Alert limit reached, deactivating!\n");
551 state = STATE_RQST_DEACTIVATE;
552 }
553
554 out_free:
555 free_netlink_msg(msg);
556 }
557
558 void handle_dm_config_new_msg(struct netlink_message *msg, int err)
559 {
560 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
561
562 if (state != STATE_CONFIG_GETTING)
563 goto out_free;
564
565 err = genlmsg_parse(msg->msg, 0, attrs, NET_DM_ATTR_MAX, net_dm_policy);
566 if (err)
567 goto out_free;
568
569 if (!attrs[NET_DM_ATTR_ALERT_MODE] || !attrs[NET_DM_ATTR_TRUNC_LEN] ||
570 !attrs[NET_DM_ATTR_QUEUE_LEN])
571 goto out_free;
572
573 printf("Alert mode: ");
574 switch (nla_get_u8(attrs[NET_DM_ATTR_ALERT_MODE])) {
575 case NET_DM_ALERT_MODE_SUMMARY:
576 printf("Summary\n");
577 break;
578 case NET_DM_ALERT_MODE_PACKET:
579 printf("Packet\n");
580 break;
581 default:
582 printf("Invalid alert mode\n");
583 break;
584 }
585
586 printf("Truncation length: %u\n",
587 nla_get_u32(attrs[NET_DM_ATTR_TRUNC_LEN]));
588
589 printf("Queue length: %u\n", nla_get_u32(attrs[NET_DM_ATTR_QUEUE_LEN]));
590
591 out_free:
592 state = STATE_IDLE;
593 free_netlink_msg(msg);
594 }
595
596 void print_nested_stats(struct nlattr *attr)
597 {
598 struct nlattr *attrs[NET_DM_ATTR_STATS_MAX + 1];
599 int err;
600
601 err = nla_parse_nested(attrs, NET_DM_ATTR_STATS_MAX, attr,
602 net_dm_stats_policy);
603 if (err)
604 return;
605
606 if (attrs[NET_DM_ATTR_STATS_DROPPED])
607 printf("Tail dropped: %" PRIu64 "\n",
608 nla_get_u64(attrs[NET_DM_ATTR_STATS_DROPPED]));
609 }
610
611 void handle_dm_stats_new_msg(struct netlink_message *msg, int err)
612 {
613 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
614
615 if (state != STATE_STATS_GETTING)
616 goto out_free;
617
618 err = genlmsg_parse(msg->msg, 0, attrs, NET_DM_ATTR_MAX, net_dm_policy);
619 if (err)
620 goto out_free;
621
622 if (attrs[NET_DM_ATTR_STATS]) {
623 printf("Software statistics:\n");
624 print_nested_stats(attrs[NET_DM_ATTR_STATS]);
625 }
626
627 if (attrs[NET_DM_ATTR_HW_STATS]) {
628 printf("Hardware statistics:\n");
629 print_nested_stats(attrs[NET_DM_ATTR_HW_STATS]);
630 }
631
632 out_free:
633 state = STATE_IDLE;
634 free_netlink_msg(msg);
635 }
636
637 void handle_dm_config_msg(struct netlink_message *amsg, struct netlink_message *msg, int err)
638 {
639 if (err != 0) {
640 char *erm = strerror(-err);
641
642 printf("Failed config request, error: %s\n", erm);
643 state = STATE_FAILED;
644 return;
645 }
646
647 switch (state) {
648 case STATE_ALERT_MODE_SETTING:
649 printf("Alert mode successfully set\n");
650 state = STATE_IDLE;
651 break;
652 case STATE_TRUNC_LEN_SETTING:
653 printf("Truncation length successfully set\n");
654 state = STATE_IDLE;
655 break;
656 case STATE_QUEUE_LEN_SETTING:
657 printf("Queue length successfully set\n");
658 state = STATE_IDLE;
659 break;
660 default:
661 printf("Received acknowledgement for non-solicited config request\n");
662 state = STATE_FAILED;
663 }
664 }
665
666 void handle_dm_start_msg(struct netlink_message *amsg, struct netlink_message *msg, int err)
667 {
668 if (err != 0) {
669 char *erm = strerror(err*-1);
670 printf("Failed activation request, error: %s\n", erm);
671 state = STATE_FAILED;
672 goto out;
673 }
674
675 if (state == STATE_ACTIVATING) {
676 struct sigaction act;
677 memset(&act, 0, sizeof(struct sigaction));
678 act.sa_handler = sigint_handler;
679 act.sa_flags = SA_RESETHAND;
680
681 printf("Kernel monitoring activated.\n");
682 printf("Issue Ctrl-C to stop monitoring\n");
683 sigaction(SIGINT, &act, NULL);
684
685 state = STATE_RECEIVING;
686 } else {
687 printf("Odd, the kernel told us that it activated and we didn't ask\n");
688 state = STATE_FAILED;
689 }
690 out:
691 return;
692 }
693
694 void handle_dm_stop_msg(struct netlink_message *amsg, struct netlink_message *msg, int err)
695 {
696 char *erm;
697
698 if ((err == 0) || (err == -EAGAIN)) {
699 printf("Got a stop message\n");
700 state = STATE_IDLE;
701 } else {
702 erm = strerror(err*-1);
703 printf("Stop request failed, error: %s\n", erm);
704 }
705 }
706
707 int enable_drop_monitor()
708 {
709 struct netlink_message *msg;
710
711 msg = alloc_netlink_msg(NET_DM_CMD_START, NLM_F_REQUEST|NLM_F_ACK, 0);
712
713 if (monitor_sw && nla_put_flag(msg->nlbuf, NET_DM_ATTR_SW_DROPS))
714 goto nla_put_failure;
715
716 if (monitor_hw && nla_put_flag(msg->nlbuf, NET_DM_ATTR_HW_DROPS))
717 goto nla_put_failure;
718
719 set_ack_cb(msg, handle_dm_start_msg);
720
721 return send_netlink_message(msg);
722
723 nla_put_failure:
724 free_netlink_msg(msg);
725 return -EMSGSIZE;
726 }
727
728 int disable_drop_monitor()
729 {
730 struct netlink_message *msg;
731
732 msg = alloc_netlink_msg(NET_DM_CMD_STOP, NLM_F_REQUEST|NLM_F_ACK, 0);
733
734 if (monitor_sw && nla_put_flag(msg->nlbuf, NET_DM_ATTR_SW_DROPS))
735 goto nla_put_failure;
736
737 if (monitor_hw && nla_put_flag(msg->nlbuf, NET_DM_ATTR_HW_DROPS))
738 goto nla_put_failure;
739
740 set_ack_cb(msg, handle_dm_stop_msg);
741
742 return send_netlink_message(msg);
743
744 nla_put_failure:
745 free_netlink_msg(msg);
746 return -EMSGSIZE;
747 }
748
749 int set_alert_mode()
750 {
751 enum net_dm_alert_mode alert_mode;
752 struct netlink_message *msg;
753
754 switch (state) {
755 case STATE_RQST_ALERT_MODE_SUMMARY:
756 alert_mode = NET_DM_ALERT_MODE_SUMMARY;
757 break;
758 case STATE_RQST_ALERT_MODE_PACKET:
759 alert_mode = NET_DM_ALERT_MODE_PACKET;
760 break;
761 default:
762 return -EINVAL;
763 }
764
765 msg = alloc_netlink_msg(NET_DM_CMD_CONFIG, NLM_F_REQUEST|NLM_F_ACK, 0);
766 if (!msg)
767 return -ENOMEM;
768
769 if (nla_put_u8(msg->nlbuf, NET_DM_ATTR_ALERT_MODE, alert_mode))
770 goto nla_put_failure;
771
772 set_ack_cb(msg, handle_dm_config_msg);
773
774 return send_netlink_message(msg);
775
776 nla_put_failure:
777 free_netlink_msg(msg);
778 return -EMSGSIZE;
779 }
780
781 int set_trunc_len()
782 {
783 struct netlink_message *msg;
784
785 msg = alloc_netlink_msg(NET_DM_CMD_CONFIG, NLM_F_REQUEST|NLM_F_ACK, 0);
786 if (!msg)
787 return -ENOMEM;
788
789 if (nla_put_u32(msg->nlbuf, NET_DM_ATTR_TRUNC_LEN, trunc_len))
790 goto nla_put_failure;
791
792 set_ack_cb(msg, handle_dm_config_msg);
793
794 return send_netlink_message(msg);
795
796 nla_put_failure:
797 free_netlink_msg(msg);
798 return -EMSGSIZE;
799 }
800
801 int set_queue_len()
802 {
803 struct netlink_message *msg;
804
805 msg = alloc_netlink_msg(NET_DM_CMD_CONFIG, NLM_F_REQUEST|NLM_F_ACK, 0);
806 if (!msg)
807 return -ENOMEM;
808
809 if (nla_put_u32(msg->nlbuf, NET_DM_ATTR_QUEUE_LEN, queue_len))
810 goto nla_put_failure;
811
812 set_ack_cb(msg, handle_dm_config_msg);
813
814 return send_netlink_message(msg);
815
816 nla_put_failure:
817 free_netlink_msg(msg);
818 return -EMSGSIZE;
819 }
820
821 int get_config()
822 {
823 struct netlink_message *msg;
824
825 msg = alloc_netlink_msg(NET_DM_CMD_CONFIG_GET, NLM_F_REQUEST, 0);
826 if (!msg)
827 return -ENOMEM;
828
829 return send_netlink_message(msg);
830 }
831
832 int get_stats()
833 {
834 struct netlink_message *msg;
835
836 msg = alloc_netlink_msg(NET_DM_CMD_STATS_GET, NLM_F_REQUEST, 0);
837 if (!msg)
838 return -ENOMEM;
839
840 return send_netlink_message(msg);
841 }
842
843 void display_help()
844 {
845 printf("Command Syntax:\n");
846 printf("exit\t\t\t\t - Quit dropwatch\n");
847 printf("help\t\t\t\t - Display this message\n");
848 printf("set:\n");
849 printf("\talertlimit <number>\t - capture only this many alert packets\n");
850 printf("\talertmode <mode>\t - set mode to \"summary\" or \"packet\"\n");
851 printf("\ttrunc <len>\t\t - truncate packets to this length. ");
852 printf("Only applicable when \"alertmode\" is set to \"packet\"\n");
853 printf("\tqueue <len>\t\t - queue up to this many packets in the kernel. ");
854 printf("Only applicable when \"alertmode\" is set to \"packet\"\n");
855 printf("\tsw <true | false>\t - monitor software drops\n");
856 printf("\thw <true | false>\t - monitor hardware drops\n");
857 printf("start\t\t\t\t - start capture\n");
858 printf("stop\t\t\t\t - stop capture\n");
859 printf("show\t\t\t\t - show existing configuration\n");
860 printf("stats\t\t\t\t - show statistics\n");
861 }
862
863 void enter_command_line_mode()
864 {
865 char *input;
866 int err;
867
868 do {
869 input = readline("dropwatch> ");
870
871 if (input == NULL) {
872 /* Someone closed stdin on us */
873 printf("Terminating dropwatch...\n");
874 state = STATE_EXIT;
875 break;
876 }
877
878 if (!strcmp(input,"start")) {
879 state = STATE_RQST_ACTIVATE;
880 break;
881 }
882
883 if (!strcmp(input, "stop")) {
884 state = STATE_RQST_DEACTIVATE;
885 break;
886 }
887
888 if (!strcmp(input, "exit")) {
889 state = STATE_EXIT;
890 break;
891 }
892
893 if (!strcmp (input, "help")) {
894 display_help();
895 goto next_input;
896 }
897
898 if (!strncmp(input, "set", 3)) {
899 char *ninput = input+4;
900 if (!strncmp(ninput, "alertlimit", 10)) {
901 alimit = strtoul(ninput+10, NULL, 10);
902 printf("setting alert capture limit to %lu\n",
903 alimit);
904 goto next_input;
905 } else if (!strncmp(ninput, "alertmode", 9)) {
906 ninput = ninput + 10;
907 if (!strncmp(ninput, "summary", 7)) {
908 state = STATE_RQST_ALERT_MODE_SUMMARY;
909 break;
910 } else if (!strncmp(ninput, "packet", 6)) {
911 state = STATE_RQST_ALERT_MODE_PACKET;
912 break;
913 }
914 } else if (!strncmp(ninput, "trunc", 5)) {
915 trunc_len = strtoul(ninput + 6, NULL, 10);
916 state = STATE_RQST_TRUNC_LEN;
917 break;
918 } else if (!strncmp(ninput, "queue", 5)) {
919 queue_len = strtoul(ninput + 6, NULL, 10);
920 state = STATE_RQST_QUEUE_LEN;
921 break;
922 } else if (!strncmp(ninput, "sw", 2)) {
923 err = strtobool(ninput + 3, &monitor_sw);
924 if (err) {
925 printf("invalid boolean value\n");
926 state = STATE_FAILED;
927 break;
928 }
929 printf("setting software drops monitoring to %d\n",
930 monitor_sw);
931 goto next_input;
932 } else if (!strncmp(ninput, "hw", 2)) {
933 err = strtobool(ninput + 3, &monitor_hw);
934 if (err) {
935 printf("invalid boolean value\n");
936 state = STATE_FAILED;
937 break;
938 }
939 printf("setting hardware drops monitoring to %d\n",
940 monitor_hw);
941 goto next_input;
942 }
943 }
944
945 if (!strncmp(input, "show", 4)) {
946 state = STATE_RQST_CONFIG;
947 break;
948 }
949
950 if (!strncmp(input, "stats", 5)) {
951 state = STATE_RQST_STATS;
952 break;
953 }
954 next_input:
955 free(input);
956 } while(1);
957
958 free(input);
959 }
960
961 void enter_state_loop(void)
962 {
963 int should_rx = 0;
964
965 while (1) {
966 switch(state) {
967
968 case STATE_IDLE:
969 should_rx = 0;
970 enter_command_line_mode();
971 break;
972 case STATE_RQST_ACTIVATE:
973 printf("Enabling monitoring...\n");
974 if (enable_drop_monitor() < 0) {
975 perror("Unable to send activation msg:");
976 state = STATE_FAILED;
977 } else {
978 state = STATE_ACTIVATING;
979 should_rx = 1;
980 }
981 break;
982 case STATE_ACTIVATING:
983 printf("Waiting for activation ack....\n");
984 break;
985 case STATE_RECEIVING:
986 break;
987 case STATE_RQST_DEACTIVATE:
988 printf("Deactivation requested, turning off monitoring\n");
989 if (disable_drop_monitor() < 0) {
990 perror("Unable to send deactivation msg:");
991 state = STATE_FAILED;
992 } else
993 state = STATE_DEACTIVATING;
994 should_rx = 1;
995 break;
996 case STATE_DEACTIVATING:
997 printf("Waiting for deactivation ack...\n");
998 break;
999 case STATE_EXIT:
1000 case STATE_FAILED:
1001 should_rx = 0;
1002 return;
1003 case STATE_RQST_ALERT_MODE_SUMMARY:
1004 case STATE_RQST_ALERT_MODE_PACKET:
1005 printf("Setting alert mode\n");
1006 if (set_alert_mode() < 0) {
1007 perror("Failed to set alert mode");
1008 state = STATE_FAILED;
1009 } else {
1010 state = STATE_ALERT_MODE_SETTING;
1011 should_rx = 1;
1012 }
1013 break;
1014 case STATE_ALERT_MODE_SETTING:
1015 printf("Waiting for alert mode setting ack...\n");
1016 break;
1017 case STATE_RQST_TRUNC_LEN:
1018 printf("Setting truncation length to %lu\n",
1019 trunc_len);
1020 if (set_trunc_len() < 0) {
1021 perror("Failed to set truncation length");
1022 state = STATE_FAILED;
1023 } else {
1024 state = STATE_TRUNC_LEN_SETTING;
1025 should_rx = 1;
1026 }
1027 break;
1028 case STATE_TRUNC_LEN_SETTING:
1029 printf("Waiting for truncation length setting ack...\n");
1030 break;
1031 case STATE_RQST_QUEUE_LEN:
1032 printf("Setting queue length to %lu\n", queue_len);
1033 if (set_queue_len() < 0) {
1034 perror("Failed to set queue length");
1035 state = STATE_FAILED;
1036 } else {
1037 state = STATE_QUEUE_LEN_SETTING;
1038 should_rx = 1;
1039 }
1040 break;
1041 case STATE_QUEUE_LEN_SETTING:
1042 printf("Waiting for queue length setting ack...\n");
1043 break;
1044 case STATE_RQST_CONFIG:
1045 printf("Getting existing configuration\n");
1046 if (get_config() < 0) {
1047 perror("Failed to get existing configuration");
1048 state = STATE_FAILED;
1049 } else {
1050 state = STATE_CONFIG_GETTING;
1051 should_rx = 1;
1052 }
1053 break;
1054 case STATE_CONFIG_GETTING:
1055 printf("Waiting for existing configuration query response\n");
1056 break;
1057 case STATE_RQST_STATS:
1058 printf("Getting statistics\n");
1059 if (get_stats() < 0) {
1060 perror("Failed to get statistics");
1061 state = STATE_FAILED;
1062 } else {
1063 state = STATE_STATS_GETTING;
1064 should_rx = 1;
1065 }
1066 break;
1067 case STATE_STATS_GETTING:
1068 printf("Waiting for statistics query response\n");
1069 break;
1070 default:
1071 printf("Unknown state received! exiting!\n");
1072 state = STATE_FAILED;
1073 should_rx = 0;
1074 break;
1075 }
1076
1077 /*
1078 * After we process our state loop, look to see if we have messages
1079 */
1080 if (should_rx)
1081 process_rx_message();
1082 }
1083 }
1084
1085 struct option options[] = {
1086 {"lmethod", 1, 0, 'l'},
1087 {0, 0, 0, 0}
1088 };
1089
1090 void usage()
1091 {
1092 printf("dropwatch [-l|--lmethod <method | list>]\n");
1093 }
1094
1095 int main (int argc, char **argv)
1096 {
1097 int c, optind;
1098 lookup_init_method_t meth = METHOD_NULL;
1099 /*
1100 * parse the options
1101 */
1102 for(;;) {
1103 c = getopt_long(argc, argv, "l:", options, &optind);
1104
1105 /* are we done parsing ? */
1106 if (c == -1)
1107 break;
1108
1109 switch(c) {
1110
1111 case '?':
1112 usage();
1113 exit(1);
1114 /* NOTREACHED */
1115 case 'l':
1116 /* select the lookup method we want to use */
1117 if (!strncmp(optarg, "list", 4)) {
1118 printf("Available lookup methods:\n");
1119 printf("kas - use /proc/kallsyms\n");
1120 exit(0);
1121 } else if (!strncmp(optarg, "kas", 3)) {
1122 meth = METHOD_KALLSYMS;
1123 } else {
1124 printf("Unknown lookup method %s\n", optarg);
1125 exit(1);
1126 }
1127 break;
1128 default:
1129 printf("Unknown option\n");
1130 usage();
1131 exit(1);
1132 /* NOTREACHED */
1133 }
1134 }
1135
1136 /*
1137 * open up the netlink socket that we need to talk to our dropwatch socket
1138 */
1139 nsd = setup_netlink_socket();
1140
1141 if (nsd == NULL) {
1142 printf("Cleaning up on socket creation error\n");
1143 goto out;
1144 }
1145
1146
1147 /*
1148 * Initialize our lookup library
1149 */
1150 init_lookup(meth);
1151
1152 enter_state_loop();
1153 printf("Shutting down ...\n");
1154
1155 nl_close(nsd);
1156 exit(0);
1157 out:
1158 exit(1);
1159 }