1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2017 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #define _GNU_SOURCE |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | #include "common.h" |
---|
32 | #include "libtrace.h" |
---|
33 | #include "libtrace_int.h" |
---|
34 | #include "format_helper.h" |
---|
35 | #include "format_erf.h" |
---|
36 | |
---|
37 | #include <errno.h> |
---|
38 | #include <fcntl.h> |
---|
39 | #include <stdio.h> |
---|
40 | #include <string.h> |
---|
41 | #include <unistd.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <net/if.h> |
---|
44 | #include <sys/types.h> |
---|
45 | #include <sys/socket.h> |
---|
46 | #include <netdb.h> |
---|
47 | |
---|
48 | #include "format_ndag.h" |
---|
49 | |
---|
50 | #define NDAG_IDLE_TIMEOUT (600) |
---|
51 | #define ENCAP_BUFSIZE (10000) |
---|
52 | #define CTRL_BUF_SIZE (10000) |
---|
53 | #define ENCAP_BUFFERS (1000) |
---|
54 | |
---|
55 | #define RECV_BATCH_SIZE (50) |
---|
56 | |
---|
57 | #define FORMAT_DATA ((ndag_format_data_t *)libtrace->format_data) |
---|
58 | |
---|
59 | static struct libtrace_format_t ndag; |
---|
60 | |
---|
61 | volatile int ndag_paused = 0; |
---|
62 | |
---|
63 | typedef struct monitor { |
---|
64 | uint16_t monitorid; |
---|
65 | uint64_t laststart; |
---|
66 | } ndag_monitor_t; |
---|
67 | |
---|
68 | |
---|
69 | typedef struct streamsource { |
---|
70 | uint16_t monitor; |
---|
71 | char *groupaddr; |
---|
72 | char *localiface; |
---|
73 | uint16_t port; |
---|
74 | } streamsource_t; |
---|
75 | |
---|
76 | typedef struct streamsock { |
---|
77 | char *groupaddr; |
---|
78 | int sock; |
---|
79 | struct addrinfo *srcaddr; |
---|
80 | uint16_t port; |
---|
81 | uint32_t expectedseq; |
---|
82 | ndag_monitor_t *monitorptr; |
---|
83 | char **saved; |
---|
84 | char *nextread; |
---|
85 | int nextreadind; |
---|
86 | int nextwriteind; |
---|
87 | int savedsize[ENCAP_BUFFERS]; |
---|
88 | uint64_t nextts; |
---|
89 | uint32_t startidle; |
---|
90 | uint64_t recordcount; |
---|
91 | |
---|
92 | int bufavail; |
---|
93 | int bufwaiting; |
---|
94 | |
---|
95 | #if HAVE_DECL_RECVMMSG |
---|
96 | struct mmsghdr mmsgbufs[RECV_BATCH_SIZE]; |
---|
97 | #else |
---|
98 | struct msghdr singlemsg; |
---|
99 | #endif |
---|
100 | |
---|
101 | } streamsock_t; |
---|
102 | |
---|
103 | typedef struct recvstream { |
---|
104 | streamsock_t *sources; |
---|
105 | uint16_t sourcecount; |
---|
106 | libtrace_message_queue_t mqueue; |
---|
107 | int threadindex; |
---|
108 | ndag_monitor_t *knownmonitors; |
---|
109 | uint16_t monitorcount; |
---|
110 | |
---|
111 | uint64_t dropped_upstream; |
---|
112 | uint64_t missing_records; |
---|
113 | uint64_t received_packets; |
---|
114 | |
---|
115 | int maxfd; |
---|
116 | } recvstream_t; |
---|
117 | |
---|
118 | typedef struct ndag_format_data { |
---|
119 | char *multicastgroup; |
---|
120 | char *portstr; |
---|
121 | char *localiface; |
---|
122 | uint16_t nextthreadid; |
---|
123 | recvstream_t *receivers; |
---|
124 | |
---|
125 | pthread_t controlthread; |
---|
126 | libtrace_message_queue_t controlqueue; |
---|
127 | int consterfframing; |
---|
128 | } ndag_format_data_t; |
---|
129 | |
---|
130 | enum { |
---|
131 | NDAG_CLIENT_HALT = 0x01, |
---|
132 | NDAG_CLIENT_RESTARTED = 0x02, // redundant |
---|
133 | NDAG_CLIENT_NEWGROUP = 0x03 |
---|
134 | }; |
---|
135 | |
---|
136 | typedef struct ndagreadermessage { |
---|
137 | uint8_t type; |
---|
138 | streamsource_t contents; |
---|
139 | } ndag_internal_message_t; |
---|
140 | |
---|
141 | |
---|
142 | static inline int seq_cmp(uint32_t seq_a, uint32_t seq_b) { |
---|
143 | |
---|
144 | /* Calculate seq_a - seq_b, taking wraparound into account */ |
---|
145 | if (seq_a == seq_b) return 0; |
---|
146 | |
---|
147 | if (seq_a > seq_b) { |
---|
148 | return (int) (seq_a - seq_b); |
---|
149 | } |
---|
150 | |
---|
151 | /* -1 for the wrap and another -1 because we don't use zero */ |
---|
152 | return (int) (0xffffffff - ((seq_b - seq_a) - 2)); |
---|
153 | } |
---|
154 | |
---|
155 | static uint8_t check_ndag_header(char *msgbuf, uint32_t msgsize) { |
---|
156 | ndag_common_t *header = (ndag_common_t *)msgbuf; |
---|
157 | |
---|
158 | if (msgsize < sizeof(ndag_common_t)) { |
---|
159 | fprintf(stderr, |
---|
160 | "nDAG message does not have a complete nDAG header.\n"); |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | if (ntohl(header->magic) != NDAG_MAGIC_NUMBER) { |
---|
165 | fprintf(stderr, |
---|
166 | "nDAG message does not have a valid magic number.\n"); |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | if (header->version > NDAG_EXPORT_VERSION || header->version == 0) { |
---|
171 | fprintf(stderr, |
---|
172 | "nDAG message has an invalid header version: %u\n", |
---|
173 | header->version); |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | return header->type; |
---|
178 | } |
---|
179 | |
---|
180 | static int join_multicast_group(char *groupaddr, char *localiface, |
---|
181 | char *portstr, uint16_t portnum, struct addrinfo **srcinfo) { |
---|
182 | |
---|
183 | struct addrinfo hints; |
---|
184 | struct addrinfo *gotten; |
---|
185 | struct addrinfo *group; |
---|
186 | unsigned int interface; |
---|
187 | char pstr[16]; |
---|
188 | struct group_req greq; |
---|
189 | int bufsize, val; |
---|
190 | |
---|
191 | int sock; |
---|
192 | |
---|
193 | if (portstr == NULL) { |
---|
194 | snprintf(pstr, 15, "%u", portnum); |
---|
195 | portstr = pstr; |
---|
196 | } |
---|
197 | |
---|
198 | interface = if_nametoindex(localiface); |
---|
199 | if (interface == 0) { |
---|
200 | fprintf(stderr, "Failed to lookup interface %s -- %s\n", |
---|
201 | localiface, strerror(errno)); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | |
---|
205 | hints.ai_family = PF_UNSPEC; |
---|
206 | hints.ai_socktype = SOCK_DGRAM; |
---|
207 | hints.ai_flags = AI_PASSIVE; |
---|
208 | hints.ai_protocol = 0; |
---|
209 | |
---|
210 | if (getaddrinfo(NULL, portstr, &hints, &gotten) != 0) { |
---|
211 | fprintf(stderr, |
---|
212 | "Call to getaddrinfo failed for NULL:%s -- %s\n", |
---|
213 | portstr, strerror(errno)); |
---|
214 | return -1; |
---|
215 | } |
---|
216 | |
---|
217 | if (getaddrinfo(groupaddr, NULL, &hints, &group) != 0) { |
---|
218 | fprintf(stderr, "Call to getaddrinfo failed for %s -- %s\n", |
---|
219 | groupaddr, strerror(errno)); |
---|
220 | return -1; |
---|
221 | } |
---|
222 | |
---|
223 | *srcinfo = gotten; |
---|
224 | sock = socket(gotten->ai_family, gotten->ai_socktype, 0); |
---|
225 | if (sock < 0) { |
---|
226 | fprintf(stderr, |
---|
227 | "Failed to create multicast socket for %s:%s -- %s\n", |
---|
228 | groupaddr, portstr, strerror(errno)); |
---|
229 | goto sockcreateover; |
---|
230 | } |
---|
231 | |
---|
232 | val = 1; |
---|
233 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { |
---|
234 | fprintf(stderr, |
---|
235 | "Failed to set REUSEADDR socket option for %s:%s -- %s\n", |
---|
236 | groupaddr, portstr, strerror(errno)); |
---|
237 | goto sockcreateover; |
---|
238 | } |
---|
239 | |
---|
240 | if (bind(sock, (struct sockaddr *)gotten->ai_addr, gotten->ai_addrlen) < 0) |
---|
241 | { |
---|
242 | fprintf(stderr, |
---|
243 | "Failed to bind to multicast socket %s:%s -- %s\n", |
---|
244 | groupaddr, portstr, strerror(errno)); |
---|
245 | sock = -1; |
---|
246 | goto sockcreateover; |
---|
247 | } |
---|
248 | |
---|
249 | greq.gr_interface = interface; |
---|
250 | memcpy(&(greq.gr_group), group->ai_addr, group->ai_addrlen); |
---|
251 | |
---|
252 | if (setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP, &greq, |
---|
253 | sizeof(greq)) < 0) { |
---|
254 | fprintf(stderr, |
---|
255 | "Failed to join multicast group %s:%s -- %s\n", |
---|
256 | groupaddr, portstr, strerror(errno)); |
---|
257 | close(sock); |
---|
258 | sock = -1; |
---|
259 | goto sockcreateover; |
---|
260 | } |
---|
261 | |
---|
262 | bufsize = 16 * 1024 * 1024; |
---|
263 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufsize, |
---|
264 | (socklen_t)sizeof(int)) < 0) { |
---|
265 | |
---|
266 | fprintf(stderr, |
---|
267 | "Failed to increase buffer size for multicast group %s:%s -- %s\n", |
---|
268 | groupaddr, portstr, strerror(errno)); |
---|
269 | close(sock); |
---|
270 | sock = -1; |
---|
271 | goto sockcreateover; |
---|
272 | } |
---|
273 | |
---|
274 | sockcreateover: |
---|
275 | freeaddrinfo(group); |
---|
276 | return sock; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | static int ndag_init_input(libtrace_t *libtrace) { |
---|
281 | |
---|
282 | char *scan = NULL; |
---|
283 | char *next = NULL; |
---|
284 | |
---|
285 | libtrace->format_data = (ndag_format_data_t *)malloc( |
---|
286 | sizeof(ndag_format_data_t)); |
---|
287 | |
---|
288 | if (!libtrace->format_data) { |
---|
289 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " |
---|
290 | "format data inside ndag_init_input()"); |
---|
291 | return -1; |
---|
292 | } |
---|
293 | |
---|
294 | FORMAT_DATA->multicastgroup = NULL; |
---|
295 | FORMAT_DATA->portstr = NULL; |
---|
296 | FORMAT_DATA->localiface = NULL; |
---|
297 | FORMAT_DATA->nextthreadid = 0; |
---|
298 | FORMAT_DATA->receivers = NULL; |
---|
299 | FORMAT_DATA->consterfframing = -1; |
---|
300 | |
---|
301 | scan = strchr(libtrace->uridata, ','); |
---|
302 | if (scan == NULL) { |
---|
303 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
---|
304 | "Bad ndag URI. Should be ndag:<interface>,<multicast group>,<port number>"); |
---|
305 | return -1; |
---|
306 | } |
---|
307 | FORMAT_DATA->localiface = strndup(libtrace->uridata, |
---|
308 | (size_t)(scan - libtrace->uridata)); |
---|
309 | next = scan + 1; |
---|
310 | |
---|
311 | scan = strchr(next, ','); |
---|
312 | if (scan == NULL) { |
---|
313 | FORMAT_DATA->portstr = strdup("9001"); |
---|
314 | FORMAT_DATA->multicastgroup = strdup(next); |
---|
315 | } else { |
---|
316 | FORMAT_DATA->multicastgroup = strndup(next, (size_t)(scan - next)); |
---|
317 | |
---|
318 | FORMAT_DATA->portstr = strdup(scan + 1); |
---|
319 | } |
---|
320 | return 0; |
---|
321 | } |
---|
322 | |
---|
323 | static int ndag_config_input(libtrace_t *libtrace, trace_option_t option, |
---|
324 | void *value) { |
---|
325 | |
---|
326 | switch(option) { |
---|
327 | case TRACE_OPTION_CONSTANT_ERF_FRAMING: |
---|
328 | FORMAT_DATA->consterfframing = *(int *)value; |
---|
329 | break; |
---|
330 | case TRACE_OPTION_EVENT_REALTIME: |
---|
331 | case TRACE_OPTION_SNAPLEN: |
---|
332 | case TRACE_OPTION_PROMISC: |
---|
333 | case TRACE_OPTION_FILTER: |
---|
334 | case TRACE_OPTION_META_FREQ: |
---|
335 | default: |
---|
336 | trace_set_err(libtrace, TRACE_ERR_OPTION_UNAVAIL, |
---|
337 | "Unsupported option %d", |
---|
338 | option); |
---|
339 | return -1; |
---|
340 | } |
---|
341 | |
---|
342 | return 0; |
---|
343 | } |
---|
344 | |
---|
345 | static void new_group_alert(libtrace_t *libtrace, uint16_t threadid, |
---|
346 | uint16_t portnum, uint16_t monid) { |
---|
347 | |
---|
348 | ndag_internal_message_t alert; |
---|
349 | |
---|
350 | alert.type = NDAG_CLIENT_NEWGROUP; |
---|
351 | alert.contents.groupaddr = FORMAT_DATA->multicastgroup; |
---|
352 | alert.contents.localiface = FORMAT_DATA->localiface; |
---|
353 | alert.contents.port = portnum; |
---|
354 | alert.contents.monitor = monid; |
---|
355 | |
---|
356 | libtrace_message_queue_put(&(FORMAT_DATA->receivers[threadid].mqueue), |
---|
357 | (void *)&alert); |
---|
358 | |
---|
359 | } |
---|
360 | |
---|
361 | static int ndag_parse_control_message(libtrace_t *libtrace, char *msgbuf, |
---|
362 | int msgsize, uint16_t *ptmap) { |
---|
363 | |
---|
364 | int i; |
---|
365 | ndag_common_t *ndaghdr = (ndag_common_t *)msgbuf; |
---|
366 | uint8_t msgtype; |
---|
367 | |
---|
368 | msgtype = check_ndag_header(msgbuf, (uint32_t)msgsize); |
---|
369 | if (msgtype == 0) { |
---|
370 | return -1; |
---|
371 | } |
---|
372 | |
---|
373 | msgsize -= sizeof(ndag_common_t); |
---|
374 | if (msgtype == NDAG_PKT_BEACON) { |
---|
375 | /* If message is a beacon, make sure every port included in the |
---|
376 | * beacon is assigned to a receive thread. |
---|
377 | */ |
---|
378 | uint16_t *ptr, numstreams; |
---|
379 | |
---|
380 | if ((uint32_t)msgsize < sizeof(uint16_t)) { |
---|
381 | fprintf(stderr, "Malformed beacon (missing number of streams).\n"); |
---|
382 | return -1; |
---|
383 | } |
---|
384 | |
---|
385 | ptr = (uint16_t *)(msgbuf + sizeof(ndag_common_t)); |
---|
386 | numstreams = ntohs(*ptr); |
---|
387 | ptr ++; |
---|
388 | |
---|
389 | if ((uint32_t)msgsize != ((numstreams + 1) * sizeof(uint16_t))) |
---|
390 | { |
---|
391 | fprintf(stderr, "Malformed beacon (length doesn't match number of streams).\n"); |
---|
392 | fprintf(stderr, "%u %u\n", msgsize, numstreams); |
---|
393 | return -1; |
---|
394 | } |
---|
395 | |
---|
396 | for (i = 0; i < numstreams; i++) { |
---|
397 | uint16_t streamport = ntohs(*ptr); |
---|
398 | |
---|
399 | if (ptmap[streamport] == 0xffff) { |
---|
400 | new_group_alert(libtrace, |
---|
401 | FORMAT_DATA->nextthreadid, streamport, |
---|
402 | ntohs(ndaghdr->monitorid)); |
---|
403 | |
---|
404 | ptmap[streamport] = FORMAT_DATA->nextthreadid; |
---|
405 | |
---|
406 | if (libtrace->perpkt_thread_count == 0) { |
---|
407 | FORMAT_DATA->nextthreadid = 0; |
---|
408 | } else { |
---|
409 | FORMAT_DATA->nextthreadid = |
---|
410 | ((FORMAT_DATA->nextthreadid + 1) % libtrace->perpkt_thread_count); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | ptr ++; |
---|
415 | } |
---|
416 | } else { |
---|
417 | fprintf(stderr, |
---|
418 | "Unexpected message type on control channel: %u\n", |
---|
419 | msgtype); |
---|
420 | return -1; |
---|
421 | } |
---|
422 | |
---|
423 | return 0; |
---|
424 | |
---|
425 | } |
---|
426 | |
---|
427 | static void *ndag_controller_run(void *tdata) { |
---|
428 | |
---|
429 | libtrace_t *libtrace = (libtrace_t *)tdata; |
---|
430 | uint16_t ptmap[65536]; |
---|
431 | int sock = -1; |
---|
432 | struct addrinfo *receiveaddr = NULL; |
---|
433 | fd_set listening; |
---|
434 | struct timeval timeout; |
---|
435 | |
---|
436 | /* ptmap is a dirty hack to allow us to quickly check if we've already |
---|
437 | * assigned a stream to a thread. |
---|
438 | */ |
---|
439 | memset(ptmap, 0xff, 65536 * sizeof(uint16_t)); |
---|
440 | |
---|
441 | sock = join_multicast_group(FORMAT_DATA->multicastgroup, |
---|
442 | FORMAT_DATA->localiface, FORMAT_DATA->portstr, 0, |
---|
443 | &receiveaddr); |
---|
444 | if (sock == -1) { |
---|
445 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
446 | "Unable to join multicast group for nDAG control channel"); |
---|
447 | trace_interrupt(); |
---|
448 | pthread_exit(NULL); |
---|
449 | } |
---|
450 | |
---|
451 | ndag_paused = 0; |
---|
452 | while ((is_halted(libtrace) == -1) && !ndag_paused) { |
---|
453 | int ret; |
---|
454 | char buf[CTRL_BUF_SIZE]; |
---|
455 | |
---|
456 | FD_ZERO(&listening); |
---|
457 | FD_SET(sock, &listening); |
---|
458 | |
---|
459 | timeout.tv_sec = 0; |
---|
460 | timeout.tv_usec = 500000; |
---|
461 | |
---|
462 | ret = select(sock + 1, &listening, NULL, NULL, &timeout); |
---|
463 | if (ret < 0) { |
---|
464 | fprintf(stderr, "Error while waiting for nDAG control messages: %s\n", strerror(errno)); |
---|
465 | break; |
---|
466 | } |
---|
467 | |
---|
468 | if (!FD_ISSET(sock, &listening)) { |
---|
469 | continue; |
---|
470 | } |
---|
471 | |
---|
472 | ret = recvfrom(sock, buf, CTRL_BUF_SIZE, 0, |
---|
473 | receiveaddr->ai_addr, |
---|
474 | &(receiveaddr->ai_addrlen)); |
---|
475 | if (ret < 0) { |
---|
476 | fprintf(stderr, "Error while receiving nDAG control message: %s\n", strerror(errno)); |
---|
477 | break; |
---|
478 | } |
---|
479 | |
---|
480 | if (ret == 0) { |
---|
481 | break; |
---|
482 | } |
---|
483 | |
---|
484 | if (ndag_parse_control_message(libtrace, buf, ret, ptmap) < 0) { |
---|
485 | fprintf(stderr, "Error while parsing nDAG control message.\n"); |
---|
486 | continue; |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | if (sock >= 0) { |
---|
491 | close(sock); |
---|
492 | } |
---|
493 | |
---|
494 | /* Control channel has fallen over, should probably encourage libtrace |
---|
495 | * to halt the receiver threads as well. |
---|
496 | */ |
---|
497 | if (!is_halted(libtrace)) { |
---|
498 | trace_interrupt(); |
---|
499 | } |
---|
500 | |
---|
501 | pthread_exit(NULL); |
---|
502 | } |
---|
503 | |
---|
504 | static int ndag_start_threads(libtrace_t *libtrace, uint32_t maxthreads) |
---|
505 | { |
---|
506 | int ret; |
---|
507 | uint32_t i; |
---|
508 | /* Configure the set of receiver threads */ |
---|
509 | |
---|
510 | if (FORMAT_DATA->receivers == NULL) { |
---|
511 | /* What if the number of threads changes between a pause and |
---|
512 | * a restart? Can this happen? */ |
---|
513 | FORMAT_DATA->receivers = (recvstream_t *) |
---|
514 | malloc(sizeof(recvstream_t) * maxthreads); |
---|
515 | } |
---|
516 | |
---|
517 | for (i = 0; i < maxthreads; i++) { |
---|
518 | FORMAT_DATA->receivers[i].sources = NULL; |
---|
519 | FORMAT_DATA->receivers[i].sourcecount = 0; |
---|
520 | FORMAT_DATA->receivers[i].knownmonitors = NULL; |
---|
521 | FORMAT_DATA->receivers[i].monitorcount = 0; |
---|
522 | FORMAT_DATA->receivers[i].threadindex = i; |
---|
523 | FORMAT_DATA->receivers[i].dropped_upstream = 0; |
---|
524 | FORMAT_DATA->receivers[i].received_packets = 0; |
---|
525 | FORMAT_DATA->receivers[i].missing_records = 0; |
---|
526 | FORMAT_DATA->receivers[i].maxfd = -1; |
---|
527 | |
---|
528 | libtrace_message_queue_init(&(FORMAT_DATA->receivers[i].mqueue), |
---|
529 | sizeof(ndag_internal_message_t)); |
---|
530 | } |
---|
531 | |
---|
532 | /* Start the controller thread */ |
---|
533 | /* TODO consider affinity of this thread? */ |
---|
534 | |
---|
535 | ret = pthread_create(&(FORMAT_DATA->controlthread), NULL, |
---|
536 | ndag_controller_run, libtrace); |
---|
537 | if (ret != 0) { |
---|
538 | return -1; |
---|
539 | } |
---|
540 | return maxthreads; |
---|
541 | } |
---|
542 | |
---|
543 | static int ndag_start_input(libtrace_t *libtrace) { |
---|
544 | return ndag_start_threads(libtrace, 1); |
---|
545 | } |
---|
546 | |
---|
547 | static int ndag_pstart_input(libtrace_t *libtrace) { |
---|
548 | if (ndag_start_threads(libtrace, libtrace->perpkt_thread_count) == |
---|
549 | libtrace->perpkt_thread_count) |
---|
550 | return 0; |
---|
551 | return -1; |
---|
552 | } |
---|
553 | |
---|
554 | static void halt_ndag_receiver(recvstream_t *receiver) { |
---|
555 | int j, i; |
---|
556 | libtrace_message_queue_destroy(&(receiver->mqueue)); |
---|
557 | |
---|
558 | if (receiver->sources == NULL) |
---|
559 | return; |
---|
560 | for (i = 0; i < receiver->sourcecount; i++) { |
---|
561 | streamsock_t src = receiver->sources[i]; |
---|
562 | if (src.saved) { |
---|
563 | for (j = 0; j < ENCAP_BUFFERS; j++) { |
---|
564 | if (src.saved[j]) { |
---|
565 | free(src.saved[j]); |
---|
566 | } |
---|
567 | } |
---|
568 | free(src.saved); |
---|
569 | } |
---|
570 | |
---|
571 | #if HAVE_DECL_RECVMMSG |
---|
572 | for (j = 0; j < RECV_BATCH_SIZE; j++) { |
---|
573 | if (src.mmsgbufs[j].msg_hdr.msg_iov) { |
---|
574 | free(src.mmsgbufs[j].msg_hdr.msg_iov); |
---|
575 | } |
---|
576 | } |
---|
577 | #else |
---|
578 | free(src.singlemsg.msg_iov); |
---|
579 | #endif |
---|
580 | |
---|
581 | if (src.sock != -1) { |
---|
582 | close(src.sock); |
---|
583 | } |
---|
584 | } |
---|
585 | if (receiver->knownmonitors) { |
---|
586 | free(receiver->knownmonitors); |
---|
587 | } |
---|
588 | |
---|
589 | if (receiver->sources) { |
---|
590 | free(receiver->sources); |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | static int ndag_pause_input(libtrace_t *libtrace) { |
---|
595 | int i; |
---|
596 | |
---|
597 | /* Close the existing receiver sockets */ |
---|
598 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
599 | halt_ndag_receiver(&(FORMAT_DATA->receivers[i])); |
---|
600 | } |
---|
601 | return 0; |
---|
602 | } |
---|
603 | |
---|
604 | static int ndag_fin_input(libtrace_t *libtrace) { |
---|
605 | |
---|
606 | if (FORMAT_DATA->receivers) { |
---|
607 | free(FORMAT_DATA->receivers); |
---|
608 | } |
---|
609 | if (FORMAT_DATA->multicastgroup) { |
---|
610 | free(FORMAT_DATA->multicastgroup); |
---|
611 | } |
---|
612 | if (FORMAT_DATA->portstr) { |
---|
613 | free(FORMAT_DATA->portstr); |
---|
614 | } |
---|
615 | if (FORMAT_DATA->localiface) { |
---|
616 | free(FORMAT_DATA->localiface); |
---|
617 | } |
---|
618 | |
---|
619 | free(libtrace->format_data); |
---|
620 | return 0; |
---|
621 | } |
---|
622 | |
---|
623 | static int ndag_get_framing_length(const libtrace_packet_t *packet) { |
---|
624 | |
---|
625 | libtrace_t *libtrace = packet->trace; |
---|
626 | |
---|
627 | if (FORMAT_DATA->consterfframing >= 0) { |
---|
628 | return FORMAT_DATA->consterfframing; |
---|
629 | } |
---|
630 | return erf_get_framing_length(packet); |
---|
631 | } |
---|
632 | |
---|
633 | static int ndag_prepare_packet_stream(libtrace_t *restrict libtrace, |
---|
634 | recvstream_t *restrict rt, |
---|
635 | streamsock_t *restrict ssock, |
---|
636 | libtrace_packet_t *restrict packet, |
---|
637 | uint32_t flags UNUSED) { |
---|
638 | |
---|
639 | /* XXX flags is constant, so we can tell the compiler to not |
---|
640 | * bother copying over the parameter |
---|
641 | */ |
---|
642 | |
---|
643 | dag_record_t *erfptr; |
---|
644 | ndag_encap_t *encaphdr; |
---|
645 | uint16_t ndag_reccount = 0; |
---|
646 | int nr; |
---|
647 | uint16_t rlen; |
---|
648 | |
---|
649 | /* |
---|
650 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
---|
651 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
652 | } else { |
---|
653 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
654 | } |
---|
655 | */ |
---|
656 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
657 | |
---|
658 | packet->trace = libtrace; |
---|
659 | packet->buffer = ssock->nextread; |
---|
660 | packet->header = ssock->nextread; |
---|
661 | packet->type = TRACE_RT_DATA_ERF; |
---|
662 | |
---|
663 | erfptr = (dag_record_t *)packet->header; |
---|
664 | |
---|
665 | if (erfptr->flags.rxerror == 1) { |
---|
666 | packet->payload = NULL; |
---|
667 | if (FORMAT_DATA->consterfframing >= 0) { |
---|
668 | erfptr->rlen = htons(FORMAT_DATA->consterfframing & 0xffff); |
---|
669 | } else { |
---|
670 | erfptr->rlen = htons(erf_get_framing_length(packet)); |
---|
671 | } |
---|
672 | } else { |
---|
673 | if (FORMAT_DATA->consterfframing >= 0) { |
---|
674 | packet->payload = (char *)packet->buffer + |
---|
675 | FORMAT_DATA->consterfframing; |
---|
676 | } else { |
---|
677 | packet->payload = (char *)packet->buffer + |
---|
678 | erf_get_framing_length(packet); |
---|
679 | } |
---|
680 | } |
---|
681 | |
---|
682 | /* Update upstream drops using lctr */ |
---|
683 | |
---|
684 | if (erfptr->type == TYPE_DSM_COLOR_ETH) { |
---|
685 | /* TODO */ |
---|
686 | } else { |
---|
687 | if (rt->received_packets > 0) { |
---|
688 | rt->dropped_upstream += ntohs(erfptr->lctr); |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | rt->received_packets ++; |
---|
693 | ssock->recordcount += 1; |
---|
694 | |
---|
695 | nr = ssock->nextreadind; |
---|
696 | encaphdr = (ndag_encap_t *)(ssock->saved[nr] + |
---|
697 | sizeof(ndag_common_t)); |
---|
698 | |
---|
699 | ndag_reccount = ntohs(encaphdr->recordcount); |
---|
700 | if ((ndag_reccount & 0x8000) != 0) { |
---|
701 | /* Record was truncated -- update rlen appropriately */ |
---|
702 | rlen = ssock->savedsize[nr] - |
---|
703 | (ssock->nextread - ssock->saved[nr]); |
---|
704 | erfptr->rlen = htons(rlen); |
---|
705 | } else { |
---|
706 | rlen = ntohs(erfptr->rlen); |
---|
707 | } |
---|
708 | ssock->nextread += rlen; |
---|
709 | ssock->nextts = 0; |
---|
710 | |
---|
711 | if (ssock->nextread - ssock->saved[nr] > ssock->savedsize[nr]) { |
---|
712 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Walked past the end of the " |
---|
713 | "nDAG receive buffer, probably due to a invalid rlen, in ndag_prepare_packet_stream()"); |
---|
714 | return -1; |
---|
715 | } |
---|
716 | |
---|
717 | if (ssock->nextread - ssock->saved[nr] >= ssock->savedsize[nr]) { |
---|
718 | /* Read everything from this buffer, mark as empty and |
---|
719 | * move on. */ |
---|
720 | ssock->savedsize[nr] = 0; |
---|
721 | ssock->bufwaiting ++; |
---|
722 | |
---|
723 | nr ++; |
---|
724 | if (nr == ENCAP_BUFFERS) { |
---|
725 | nr = 0; |
---|
726 | } |
---|
727 | ssock->nextread = ssock->saved[nr] + sizeof(ndag_common_t) + |
---|
728 | sizeof(ndag_encap_t); |
---|
729 | ssock->nextreadind = nr; |
---|
730 | } |
---|
731 | |
---|
732 | packet->order = erf_get_erf_timestamp(packet); |
---|
733 | packet->error = rlen; |
---|
734 | return rlen; |
---|
735 | } |
---|
736 | |
---|
737 | static int ndag_prepare_packet(libtrace_t *libtrace UNUSED, |
---|
738 | libtrace_packet_t *packet UNUSED, |
---|
739 | void *buffer UNUSED, libtrace_rt_types_t rt_type UNUSED, |
---|
740 | uint32_t flags UNUSED) { |
---|
741 | |
---|
742 | fprintf(stderr, "Sending nDAG records over RT doesn't make sense! Please stop\n"); |
---|
743 | return 0; |
---|
744 | |
---|
745 | } |
---|
746 | |
---|
747 | static ndag_monitor_t *add_new_knownmonitor(recvstream_t *rt, uint16_t monid) { |
---|
748 | |
---|
749 | ndag_monitor_t *mon; |
---|
750 | |
---|
751 | if (rt->monitorcount == 0) { |
---|
752 | rt->knownmonitors = (ndag_monitor_t *) |
---|
753 | malloc(sizeof(ndag_monitor_t) * 5); |
---|
754 | } else { |
---|
755 | rt->knownmonitors = (ndag_monitor_t *) |
---|
756 | realloc(rt->knownmonitors, |
---|
757 | sizeof(ndag_monitor_t) * (rt->monitorcount * 5)); |
---|
758 | } |
---|
759 | |
---|
760 | mon = &(rt->knownmonitors[rt->monitorcount]); |
---|
761 | mon->monitorid = monid; |
---|
762 | mon->laststart = 0; |
---|
763 | |
---|
764 | rt->monitorcount ++; |
---|
765 | return mon; |
---|
766 | } |
---|
767 | |
---|
768 | static int add_new_streamsock(recvstream_t *rt, streamsource_t src) { |
---|
769 | |
---|
770 | streamsock_t *ssock = NULL; |
---|
771 | ndag_monitor_t *mon = NULL; |
---|
772 | int i; |
---|
773 | |
---|
774 | /* TODO consider replacing this with a list or vector so we can |
---|
775 | * easily remove sources that are no longer in use, rather than |
---|
776 | * just setting the sock to -1 and having to check them every |
---|
777 | * time we want to read a packet. |
---|
778 | */ |
---|
779 | if (rt->sourcecount == 0) { |
---|
780 | rt->sources = (streamsock_t *)malloc(sizeof(streamsock_t) * 10); |
---|
781 | } else if ((rt->sourcecount % 10) == 0) { |
---|
782 | rt->sources = (streamsock_t *)realloc(rt->sources, |
---|
783 | sizeof(streamsock_t) * (rt->sourcecount + 10)); |
---|
784 | } |
---|
785 | |
---|
786 | ssock = &(rt->sources[rt->sourcecount]); |
---|
787 | |
---|
788 | for (i = 0; i < rt->monitorcount; i++) { |
---|
789 | if (rt->knownmonitors[i].monitorid == src.monitor) { |
---|
790 | mon = &(rt->knownmonitors[i]); |
---|
791 | break; |
---|
792 | } |
---|
793 | } |
---|
794 | |
---|
795 | if (mon == NULL) { |
---|
796 | mon = add_new_knownmonitor(rt, src.monitor); |
---|
797 | } |
---|
798 | |
---|
799 | ssock->port = src.port; |
---|
800 | ssock->groupaddr = src.groupaddr; |
---|
801 | ssock->expectedseq = 0; |
---|
802 | ssock->monitorptr = mon; |
---|
803 | ssock->saved = (char **)malloc(sizeof(char *) * ENCAP_BUFFERS); |
---|
804 | ssock->bufavail = ENCAP_BUFFERS; |
---|
805 | ssock->bufwaiting = 0; |
---|
806 | ssock->startidle = 0; |
---|
807 | ssock->nextts = 0; |
---|
808 | |
---|
809 | for (i = 0; i < ENCAP_BUFFERS; i++) { |
---|
810 | ssock->saved[i] = (char *)malloc(ENCAP_BUFSIZE); |
---|
811 | ssock->savedsize[i] = 0; |
---|
812 | } |
---|
813 | |
---|
814 | ssock->sock = join_multicast_group(src.groupaddr, src.localiface, |
---|
815 | NULL, src.port, &(ssock->srcaddr)); |
---|
816 | |
---|
817 | if (ssock->sock < 0) { |
---|
818 | return -1; |
---|
819 | } |
---|
820 | |
---|
821 | if (ssock->sock > rt->maxfd) { |
---|
822 | rt->maxfd = ssock->sock; |
---|
823 | } |
---|
824 | |
---|
825 | #if HAVE_DECL_RECVMMSG |
---|
826 | for (i = 0; i < RECV_BATCH_SIZE; i++) { |
---|
827 | ssock->mmsgbufs[i].msg_hdr.msg_iov = (struct iovec *) |
---|
828 | malloc(sizeof(struct iovec)); |
---|
829 | ssock->mmsgbufs[i].msg_hdr.msg_name = ssock->srcaddr->ai_addr; |
---|
830 | ssock->mmsgbufs[i].msg_hdr.msg_namelen = ssock->srcaddr->ai_addrlen; |
---|
831 | ssock->mmsgbufs[i].msg_hdr.msg_control = NULL; |
---|
832 | ssock->mmsgbufs[i].msg_hdr.msg_controllen = 0; |
---|
833 | ssock->mmsgbufs[i].msg_hdr.msg_flags = 0; |
---|
834 | ssock->mmsgbufs[i].msg_len = 0; |
---|
835 | } |
---|
836 | #else |
---|
837 | ssock->singlemsg.msg_iov = (struct iovec *) calloc(1, sizeof(struct iovec)); |
---|
838 | #endif |
---|
839 | |
---|
840 | ssock->nextread = NULL;; |
---|
841 | ssock->nextreadind = 0; |
---|
842 | ssock->nextwriteind = 0; |
---|
843 | ssock->recordcount = 0; |
---|
844 | rt->sourcecount += 1; |
---|
845 | |
---|
846 | fprintf(stderr, "Added new stream %s:%u to thread %d\n", |
---|
847 | ssock->groupaddr, ssock->port, rt->threadindex); |
---|
848 | |
---|
849 | return ssock->port; |
---|
850 | } |
---|
851 | |
---|
852 | static int receiver_read_messages(recvstream_t *rt) { |
---|
853 | |
---|
854 | ndag_internal_message_t msg; |
---|
855 | |
---|
856 | while (libtrace_message_queue_try_get(&(rt->mqueue), |
---|
857 | (void *)&msg) != LIBTRACE_MQ_FAILED) { |
---|
858 | switch(msg.type) { |
---|
859 | case NDAG_CLIENT_NEWGROUP: |
---|
860 | if (add_new_streamsock(rt, msg.contents) < 0) { |
---|
861 | return -1; |
---|
862 | } |
---|
863 | break; |
---|
864 | case NDAG_CLIENT_HALT: |
---|
865 | return 0; |
---|
866 | } |
---|
867 | } |
---|
868 | return 1; |
---|
869 | |
---|
870 | } |
---|
871 | |
---|
872 | static inline int readable_data(streamsock_t *ssock) { |
---|
873 | |
---|
874 | if (ssock->sock == -1) { |
---|
875 | return 0; |
---|
876 | } |
---|
877 | if (ssock->savedsize[ssock->nextreadind] == 0) { |
---|
878 | return 0; |
---|
879 | } |
---|
880 | /* |
---|
881 | if (ssock->nextread - ssock->saved[ssock->nextreadind] >= |
---|
882 | ssock->savedsize[ssock->nextreadind]) { |
---|
883 | return 0; |
---|
884 | } |
---|
885 | */ |
---|
886 | return 1; |
---|
887 | |
---|
888 | |
---|
889 | } |
---|
890 | |
---|
891 | static inline void reset_expected_seqs(recvstream_t *rt, ndag_monitor_t *mon) { |
---|
892 | |
---|
893 | int i; |
---|
894 | for (i = 0; i < rt->sourcecount; i++) { |
---|
895 | if (rt->sources[i].monitorptr == mon) { |
---|
896 | rt->sources[i].expectedseq = 0; |
---|
897 | } |
---|
898 | } |
---|
899 | |
---|
900 | } |
---|
901 | |
---|
902 | static int init_receivers(streamsock_t *ssock, int required) { |
---|
903 | |
---|
904 | int wind = ssock->nextwriteind; |
---|
905 | int i = 1; |
---|
906 | |
---|
907 | #if HAVE_DECL_RECVMMSG |
---|
908 | for (i = 0; i < required; i++) { |
---|
909 | if (i >= RECV_BATCH_SIZE) { |
---|
910 | break; |
---|
911 | } |
---|
912 | |
---|
913 | if (wind >= ENCAP_BUFFERS) { |
---|
914 | wind = 0; |
---|
915 | } |
---|
916 | |
---|
917 | ssock->mmsgbufs[i].msg_len = 0; |
---|
918 | ssock->mmsgbufs[i].msg_hdr.msg_iov->iov_base = ssock->saved[wind]; |
---|
919 | ssock->mmsgbufs[i].msg_hdr.msg_iov->iov_len = ENCAP_BUFSIZE; |
---|
920 | ssock->mmsgbufs[i].msg_hdr.msg_iovlen = 1; |
---|
921 | |
---|
922 | wind ++; |
---|
923 | } |
---|
924 | #else |
---|
925 | if (required <= 0) { |
---|
926 | fprintf(stderr, "You are required to have atleast 1 receiver in init_receivers\n"); |
---|
927 | return TRACE_ERR_INIT_FAILED; |
---|
928 | } |
---|
929 | ssock->singlemsg.msg_iov->iov_base = ssock->saved[wind]; |
---|
930 | ssock->singlemsg.msg_iov->iov_len = ENCAP_BUFSIZE; |
---|
931 | ssock->singlemsg.msg_iovlen = 1; |
---|
932 | #endif |
---|
933 | return i; |
---|
934 | } |
---|
935 | |
---|
936 | static int check_ndag_received(streamsock_t *ssock, int index, |
---|
937 | unsigned int msglen, recvstream_t *rt) { |
---|
938 | |
---|
939 | ndag_encap_t *encaphdr; |
---|
940 | ndag_monitor_t *mon; |
---|
941 | uint8_t rectype; |
---|
942 | |
---|
943 | /* Check that we have a valid nDAG encap record */ |
---|
944 | rectype = check_ndag_header(ssock->saved[index], (uint32_t)msglen); |
---|
945 | |
---|
946 | if (rectype == NDAG_PKT_KEEPALIVE) { |
---|
947 | /* Keep-alive, reset startidle and carry on. Don't |
---|
948 | * change nextwrite -- we want to overwrite the |
---|
949 | * keep-alive with usable content. */ |
---|
950 | return 0; |
---|
951 | } else if (rectype != NDAG_PKT_ENCAPERF) { |
---|
952 | fprintf(stderr, "Received invalid record on the channel for %s:%u.\n", |
---|
953 | ssock->groupaddr, ssock->port); |
---|
954 | close(ssock->sock); |
---|
955 | ssock->sock = -1; |
---|
956 | return -1; |
---|
957 | } |
---|
958 | |
---|
959 | ssock->savedsize[index] = msglen; |
---|
960 | ssock->nextwriteind ++; |
---|
961 | ssock->bufavail --; |
---|
962 | |
---|
963 | if (ssock->bufavail < 0) { |
---|
964 | fprintf(stderr, "No space in buffer in check_ndag_received()\n"); |
---|
965 | return -1; |
---|
966 | } |
---|
967 | if (ssock->nextwriteind >= ENCAP_BUFFERS) { |
---|
968 | ssock->nextwriteind = 0; |
---|
969 | } |
---|
970 | |
---|
971 | /* Get the useful info from the encap header */ |
---|
972 | encaphdr=(ndag_encap_t *)(ssock->saved[index] + sizeof(ndag_common_t)); |
---|
973 | |
---|
974 | mon = ssock->monitorptr; |
---|
975 | |
---|
976 | if (mon->laststart == 0) { |
---|
977 | mon->laststart = bswap_be_to_host64(encaphdr->started); |
---|
978 | } else if (mon->laststart != bswap_be_to_host64(encaphdr->started)) { |
---|
979 | mon->laststart = bswap_be_to_host64(encaphdr->started); |
---|
980 | reset_expected_seqs(rt, mon); |
---|
981 | |
---|
982 | /* TODO what is a good way to indicate this to clients? |
---|
983 | * set the loss counter in the ERF header? a bit rude? |
---|
984 | * use another bit in the ERF header? |
---|
985 | * add a queryable flag to libtrace_packet_t? |
---|
986 | */ |
---|
987 | |
---|
988 | } |
---|
989 | |
---|
990 | if (ssock->expectedseq != 0) { |
---|
991 | rt->missing_records += seq_cmp( |
---|
992 | ntohl(encaphdr->seqno), ssock->expectedseq); |
---|
993 | |
---|
994 | } |
---|
995 | ssock->expectedseq = ntohl(encaphdr->seqno) + 1; |
---|
996 | if (ssock->expectedseq == 0) { |
---|
997 | ssock->expectedseq ++; |
---|
998 | } |
---|
999 | |
---|
1000 | if (ssock->nextread == NULL) { |
---|
1001 | /* If this is our first read, set up 'nextread' |
---|
1002 | * by skipping past the nDAG headers */ |
---|
1003 | ssock->nextread = ssock->saved[0] + |
---|
1004 | sizeof(ndag_common_t) + sizeof(ndag_encap_t); |
---|
1005 | } |
---|
1006 | return 1; |
---|
1007 | |
---|
1008 | } |
---|
1009 | |
---|
1010 | static int receive_from_single_socket(streamsock_t *ssock, struct timeval *tv, |
---|
1011 | int *gottime, recvstream_t *rt) { |
---|
1012 | |
---|
1013 | int ret, ndagstat, avail; |
---|
1014 | int toret = 0; |
---|
1015 | |
---|
1016 | #if HAVE_DECL_RECVMMSG |
---|
1017 | int i; |
---|
1018 | #endif |
---|
1019 | |
---|
1020 | avail = init_receivers(ssock, ssock->bufavail); |
---|
1021 | |
---|
1022 | #if HAVE_DECL_RECVMMSG |
---|
1023 | ret = recvmmsg(ssock->sock, ssock->mmsgbufs, avail, |
---|
1024 | MSG_DONTWAIT, NULL); |
---|
1025 | #else |
---|
1026 | if (avail != 1) { |
---|
1027 | return 0; |
---|
1028 | } |
---|
1029 | |
---|
1030 | ret = recvmsg(ssock->sock, &(ssock->singlemsg), MSG_DONTWAIT); |
---|
1031 | #endif |
---|
1032 | if (ret < 0) { |
---|
1033 | /* Nothing to receive right now, but we should still |
---|
1034 | * count as 'ready' if at least one buffer is full */ |
---|
1035 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
---|
1036 | if (readable_data(ssock)) { |
---|
1037 | toret = 1; |
---|
1038 | } |
---|
1039 | if (!(*gottime)) { |
---|
1040 | gettimeofday(tv, NULL); |
---|
1041 | *gottime = 1; |
---|
1042 | } |
---|
1043 | if (ssock->startidle == 0) { |
---|
1044 | ssock->startidle = tv->tv_sec; |
---|
1045 | } else if (tv->tv_sec - ssock->startidle > NDAG_IDLE_TIMEOUT) { |
---|
1046 | fprintf(stderr, |
---|
1047 | "Closing channel %s:%u due to inactivity.\n", |
---|
1048 | ssock->groupaddr, |
---|
1049 | ssock->port); |
---|
1050 | |
---|
1051 | close(ssock->sock); |
---|
1052 | ssock->sock = -1; |
---|
1053 | } |
---|
1054 | } else { |
---|
1055 | |
---|
1056 | fprintf(stderr, |
---|
1057 | "Error receiving encapsulated records from %s:%u -- %s \n", |
---|
1058 | ssock->groupaddr, ssock->port, |
---|
1059 | strerror(errno)); |
---|
1060 | close(ssock->sock); |
---|
1061 | ssock->sock = -1; |
---|
1062 | } |
---|
1063 | return toret; |
---|
1064 | } |
---|
1065 | |
---|
1066 | ssock->startidle = 0; |
---|
1067 | |
---|
1068 | #if HAVE_DECL_RECVMMSG |
---|
1069 | for (i = 0; i < ret; i++) { |
---|
1070 | ndagstat = check_ndag_received(ssock, ssock->nextwriteind, |
---|
1071 | ssock->mmsgbufs[i].msg_len, rt); |
---|
1072 | if (ndagstat == -1) { |
---|
1073 | break; |
---|
1074 | } |
---|
1075 | |
---|
1076 | if (ndagstat == 1) { |
---|
1077 | toret = 1; |
---|
1078 | } |
---|
1079 | } |
---|
1080 | #else |
---|
1081 | ndagstat = check_ndag_received(ssock, ssock->nextwriteind, ret, rt); |
---|
1082 | if (ndagstat <= 0) { |
---|
1083 | toret = 0; |
---|
1084 | } else { |
---|
1085 | toret = 1; |
---|
1086 | } |
---|
1087 | #endif |
---|
1088 | |
---|
1089 | return toret; |
---|
1090 | } |
---|
1091 | |
---|
1092 | static int receive_from_sockets(recvstream_t *rt) { |
---|
1093 | |
---|
1094 | int i, readybufs, gottime; |
---|
1095 | struct timeval tv; |
---|
1096 | fd_set fds; |
---|
1097 | int maxfd = 0; |
---|
1098 | struct timeval zerotv; |
---|
1099 | |
---|
1100 | readybufs = 0; |
---|
1101 | gottime = 0; |
---|
1102 | |
---|
1103 | if (rt->maxfd == -1) { |
---|
1104 | return 0; |
---|
1105 | } |
---|
1106 | |
---|
1107 | for (i = 0; i < rt->sourcecount; i++) { |
---|
1108 | if (rt->sources[i].sock == -1) { |
---|
1109 | continue; |
---|
1110 | } |
---|
1111 | |
---|
1112 | #if HAVE_DECL_RECVMMSG |
---|
1113 | /* Plenty of full buffers, just use the packets in those */ |
---|
1114 | if (rt->sources[i].bufavail < RECV_BATCH_SIZE / 2) { |
---|
1115 | readybufs ++; |
---|
1116 | continue; |
---|
1117 | } |
---|
1118 | #else |
---|
1119 | if (rt->sources[i].bufavail == 0) { |
---|
1120 | readybufs ++; |
---|
1121 | continue; |
---|
1122 | } |
---|
1123 | #endif |
---|
1124 | if (maxfd == 0) { |
---|
1125 | FD_ZERO(&fds); |
---|
1126 | } |
---|
1127 | FD_SET(rt->sources[i].sock, &fds); |
---|
1128 | if (maxfd < rt->sources[i].sock) { |
---|
1129 | maxfd = rt->sources[i].sock; |
---|
1130 | } |
---|
1131 | } |
---|
1132 | |
---|
1133 | |
---|
1134 | if (maxfd <= 0) { |
---|
1135 | return readybufs; |
---|
1136 | } |
---|
1137 | |
---|
1138 | zerotv.tv_sec = 0; |
---|
1139 | zerotv.tv_usec = 0; |
---|
1140 | if (select(maxfd + 1, &fds, NULL, NULL, &zerotv) == -1) { |
---|
1141 | /* log the error? XXX */ |
---|
1142 | return -1; |
---|
1143 | } |
---|
1144 | |
---|
1145 | for (i = 0; i < rt->sourcecount; i++) { |
---|
1146 | if (!FD_ISSET(rt->sources[i].sock, &fds)) { |
---|
1147 | if (rt->sources[i].bufavail < ENCAP_BUFFERS) { |
---|
1148 | readybufs ++; |
---|
1149 | } |
---|
1150 | continue; |
---|
1151 | } |
---|
1152 | readybufs += receive_from_single_socket(&(rt->sources[i]), |
---|
1153 | &tv, &gottime, rt); |
---|
1154 | } |
---|
1155 | |
---|
1156 | return readybufs; |
---|
1157 | |
---|
1158 | } |
---|
1159 | |
---|
1160 | |
---|
1161 | static int receive_encap_records_block(libtrace_t *libtrace, recvstream_t *rt, |
---|
1162 | libtrace_packet_t *packet) { |
---|
1163 | |
---|
1164 | int iserr = 0; |
---|
1165 | |
---|
1166 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
1167 | free(packet->buffer); |
---|
1168 | packet->buffer = NULL; |
---|
1169 | } |
---|
1170 | |
---|
1171 | do { |
---|
1172 | /* Make sure we shouldn't be halting */ |
---|
1173 | if ((iserr = is_halted(libtrace)) != -1) { |
---|
1174 | return iserr; |
---|
1175 | } |
---|
1176 | |
---|
1177 | /* Check for any messages from the control thread */ |
---|
1178 | iserr = receiver_read_messages(rt); |
---|
1179 | |
---|
1180 | if (iserr <= 0) { |
---|
1181 | return iserr; |
---|
1182 | } |
---|
1183 | |
---|
1184 | /* If blocking and no sources, sleep for a bit and then try |
---|
1185 | * checking for messages again. |
---|
1186 | */ |
---|
1187 | if (rt->sourcecount == 0) { |
---|
1188 | usleep(10000); |
---|
1189 | continue; |
---|
1190 | } |
---|
1191 | |
---|
1192 | if ((iserr = receive_from_sockets(rt)) < 0) { |
---|
1193 | return iserr; |
---|
1194 | } else if (iserr > 0) { |
---|
1195 | /* At least one of our input sockets has available |
---|
1196 | * data, let's go ahead and use what we have. */ |
---|
1197 | break; |
---|
1198 | } |
---|
1199 | |
---|
1200 | /* None of our sources have anything available, we can take |
---|
1201 | * a short break rather than immediately trying again. |
---|
1202 | */ |
---|
1203 | if (iserr == 0) { |
---|
1204 | usleep(100); |
---|
1205 | } |
---|
1206 | |
---|
1207 | } while (1); |
---|
1208 | |
---|
1209 | return iserr; |
---|
1210 | } |
---|
1211 | |
---|
1212 | static int receive_encap_records_nonblock(libtrace_t *libtrace, recvstream_t *rt, |
---|
1213 | libtrace_packet_t *packet) { |
---|
1214 | |
---|
1215 | int iserr = 0; |
---|
1216 | |
---|
1217 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
1218 | free(packet->buffer); |
---|
1219 | packet->buffer = NULL; |
---|
1220 | } |
---|
1221 | |
---|
1222 | /* Make sure we shouldn't be halting */ |
---|
1223 | if ((iserr = is_halted(libtrace)) != -1) { |
---|
1224 | return iserr; |
---|
1225 | } |
---|
1226 | |
---|
1227 | /* If non-blocking and there are no sources, just break */ |
---|
1228 | if (rt->sourcecount == 0) { |
---|
1229 | return 0; |
---|
1230 | } |
---|
1231 | |
---|
1232 | return receive_from_sockets(rt); |
---|
1233 | } |
---|
1234 | |
---|
1235 | static streamsock_t *select_next_packet(recvstream_t *rt) { |
---|
1236 | int i; |
---|
1237 | streamsock_t *ssock = NULL; |
---|
1238 | uint64_t earliest = 0; |
---|
1239 | uint64_t currentts = 0; |
---|
1240 | dag_record_t *daghdr; |
---|
1241 | |
---|
1242 | /* If we only have one source, then no need to do any |
---|
1243 | * timestamp parsing or byteswapping. |
---|
1244 | */ |
---|
1245 | if (rt->sourcecount == 1) { |
---|
1246 | if (readable_data(&(rt->sources[0]))) { |
---|
1247 | return &(rt->sources[0]); |
---|
1248 | } |
---|
1249 | return NULL; |
---|
1250 | } |
---|
1251 | |
---|
1252 | |
---|
1253 | for (i = 0; i < rt->sourcecount; i ++) { |
---|
1254 | if (!readable_data(&(rt->sources[i]))) { |
---|
1255 | continue; |
---|
1256 | } |
---|
1257 | |
---|
1258 | if (rt->sources[i].nextts == 0) { |
---|
1259 | daghdr = (dag_record_t *)(rt->sources[i].nextread); |
---|
1260 | currentts = bswap_le_to_host64(daghdr->ts); |
---|
1261 | rt->sources[i].nextts = currentts; |
---|
1262 | } else { |
---|
1263 | currentts = rt->sources[i].nextts; |
---|
1264 | } |
---|
1265 | |
---|
1266 | if (earliest == 0 || earliest > currentts) { |
---|
1267 | earliest = currentts; |
---|
1268 | ssock = &(rt->sources[i]); |
---|
1269 | } |
---|
1270 | } |
---|
1271 | return ssock; |
---|
1272 | } |
---|
1273 | |
---|
1274 | static int ndag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
1275 | |
---|
1276 | int rem, ret; |
---|
1277 | streamsock_t *nextavail = NULL; |
---|
1278 | rem = receive_encap_records_block(libtrace, &(FORMAT_DATA->receivers[0]), |
---|
1279 | packet); |
---|
1280 | |
---|
1281 | if (rem <= 0) { |
---|
1282 | return rem; |
---|
1283 | } |
---|
1284 | |
---|
1285 | nextavail = select_next_packet(&(FORMAT_DATA->receivers[0])); |
---|
1286 | if (nextavail == NULL) { |
---|
1287 | return 0; |
---|
1288 | } |
---|
1289 | |
---|
1290 | /* nextread should point at an ERF header, so prepare 'packet' to be |
---|
1291 | * a libtrace ERF packet. */ |
---|
1292 | |
---|
1293 | ret = ndag_prepare_packet_stream(libtrace, |
---|
1294 | &(FORMAT_DATA->receivers[0]), nextavail, |
---|
1295 | packet, TRACE_PREP_DO_NOT_OWN_BUFFER); |
---|
1296 | nextavail->bufavail += nextavail->bufwaiting; |
---|
1297 | nextavail->bufwaiting = 0; |
---|
1298 | return ret; |
---|
1299 | } |
---|
1300 | |
---|
1301 | static int ndag_pread_packets(libtrace_t *libtrace, libtrace_thread_t *t, |
---|
1302 | libtrace_packet_t **packets, size_t nb_packets) { |
---|
1303 | |
---|
1304 | recvstream_t *rt; |
---|
1305 | int rem, i; |
---|
1306 | size_t read_packets = 0; |
---|
1307 | streamsock_t *nextavail = NULL; |
---|
1308 | |
---|
1309 | rt = (recvstream_t *)t->format_data; |
---|
1310 | |
---|
1311 | do { |
---|
1312 | /* Only check for messages once per batch */ |
---|
1313 | if (read_packets == 0) { |
---|
1314 | rem = receive_encap_records_block(libtrace, rt, |
---|
1315 | packets[read_packets]); |
---|
1316 | if (rem < 0) { |
---|
1317 | return rem; |
---|
1318 | } |
---|
1319 | |
---|
1320 | if (rem == 0) { |
---|
1321 | break; |
---|
1322 | } |
---|
1323 | } |
---|
1324 | |
---|
1325 | nextavail = select_next_packet(rt); |
---|
1326 | if (nextavail == NULL) { |
---|
1327 | break; |
---|
1328 | } |
---|
1329 | |
---|
1330 | ndag_prepare_packet_stream(libtrace, rt, nextavail, |
---|
1331 | packets[read_packets], |
---|
1332 | TRACE_PREP_DO_NOT_OWN_BUFFER); |
---|
1333 | |
---|
1334 | read_packets ++; |
---|
1335 | if (read_packets >= nb_packets) { |
---|
1336 | break; |
---|
1337 | } |
---|
1338 | } while (1); |
---|
1339 | |
---|
1340 | for (i = 0; i < rt->sourcecount; i++) { |
---|
1341 | streamsock_t *src = &(rt->sources[i]); |
---|
1342 | src->bufavail += src->bufwaiting; |
---|
1343 | src->bufwaiting = 0; |
---|
1344 | if (src->bufavail < 0 || src->bufavail > ENCAP_BUFFERS) { |
---|
1345 | trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Not enough buffer space in ndag_pread_packets()"); |
---|
1346 | return -1; |
---|
1347 | } |
---|
1348 | } |
---|
1349 | |
---|
1350 | return read_packets; |
---|
1351 | |
---|
1352 | } |
---|
1353 | |
---|
1354 | static libtrace_eventobj_t trace_event_ndag(libtrace_t *libtrace, |
---|
1355 | libtrace_packet_t *packet) { |
---|
1356 | |
---|
1357 | |
---|
1358 | libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
1359 | int rem, i; |
---|
1360 | streamsock_t *nextavail = NULL; |
---|
1361 | |
---|
1362 | /* Only check for messages once per call */ |
---|
1363 | rem = receiver_read_messages(&(FORMAT_DATA->receivers[0])); |
---|
1364 | if (rem <= 0) { |
---|
1365 | event.type = TRACE_EVENT_TERMINATE; |
---|
1366 | return event; |
---|
1367 | } |
---|
1368 | |
---|
1369 | do { |
---|
1370 | rem = receive_encap_records_nonblock(libtrace, |
---|
1371 | &(FORMAT_DATA->receivers[0]), packet); |
---|
1372 | |
---|
1373 | if (rem < 0) { |
---|
1374 | trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, |
---|
1375 | "Received invalid nDAG records."); |
---|
1376 | event.type = TRACE_EVENT_TERMINATE; |
---|
1377 | break; |
---|
1378 | } |
---|
1379 | |
---|
1380 | if (rem == 0) { |
---|
1381 | /* Either we've been halted or we've got no packets |
---|
1382 | * right now. */ |
---|
1383 | if (is_halted(libtrace) == 0) { |
---|
1384 | event.type = TRACE_EVENT_TERMINATE; |
---|
1385 | break; |
---|
1386 | } |
---|
1387 | event.type = TRACE_EVENT_SLEEP; |
---|
1388 | event.seconds = 0.0001; |
---|
1389 | break; |
---|
1390 | } |
---|
1391 | |
---|
1392 | nextavail = select_next_packet(&(FORMAT_DATA->receivers[0])); |
---|
1393 | if (nextavail == NULL) { |
---|
1394 | event.type = TRACE_EVENT_SLEEP; |
---|
1395 | event.seconds = 0.0001; |
---|
1396 | break; |
---|
1397 | } |
---|
1398 | |
---|
1399 | event.type = TRACE_EVENT_PACKET; |
---|
1400 | ndag_prepare_packet_stream(libtrace, |
---|
1401 | &(FORMAT_DATA->receivers[0]), nextavail, |
---|
1402 | packet, TRACE_PREP_DO_NOT_OWN_BUFFER); |
---|
1403 | event.size = trace_get_capture_length(packet) + |
---|
1404 | trace_get_framing_length(packet); |
---|
1405 | |
---|
1406 | if (libtrace->filter) { |
---|
1407 | int filtret = trace_apply_filter(libtrace->filter, |
---|
1408 | packet); |
---|
1409 | if (filtret == -1) { |
---|
1410 | trace_set_err(libtrace, |
---|
1411 | TRACE_ERR_BAD_FILTER, |
---|
1412 | "Bad BPF Filter"); |
---|
1413 | event.type = TRACE_EVENT_TERMINATE; |
---|
1414 | break; |
---|
1415 | } |
---|
1416 | |
---|
1417 | if (filtret == 0) { |
---|
1418 | /* Didn't match filter, try next one */ |
---|
1419 | libtrace->filtered_packets ++; |
---|
1420 | trace_clear_cache(packet); |
---|
1421 | continue; |
---|
1422 | } |
---|
1423 | } |
---|
1424 | |
---|
1425 | if (libtrace->snaplen > 0) { |
---|
1426 | trace_set_capture_length(packet, libtrace->snaplen); |
---|
1427 | } |
---|
1428 | libtrace->accepted_packets ++; |
---|
1429 | break; |
---|
1430 | } while (1); |
---|
1431 | |
---|
1432 | for (i = 0; i < FORMAT_DATA->receivers[0].sourcecount; i++) { |
---|
1433 | streamsock_t *src = &(FORMAT_DATA->receivers[0].sources[i]); |
---|
1434 | src->bufavail += src->bufwaiting; |
---|
1435 | src->bufwaiting = 0; |
---|
1436 | if (src->bufavail < 0 || src->bufavail > ENCAP_BUFFERS) { |
---|
1437 | trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Not enough buffer space in trace_event_ndag()"); |
---|
1438 | break; |
---|
1439 | } |
---|
1440 | } |
---|
1441 | |
---|
1442 | return event; |
---|
1443 | } |
---|
1444 | |
---|
1445 | static void ndag_get_statistics(libtrace_t *libtrace, libtrace_stat_t *stat) { |
---|
1446 | |
---|
1447 | int i; |
---|
1448 | |
---|
1449 | stat->dropped_valid = 1; |
---|
1450 | stat->dropped = 0; |
---|
1451 | stat->received_valid = 1; |
---|
1452 | stat->received = 0; |
---|
1453 | stat->missing_valid = 1; |
---|
1454 | stat->missing = 0; |
---|
1455 | |
---|
1456 | /* TODO Is this thread safe? */ |
---|
1457 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1458 | stat->dropped += FORMAT_DATA->receivers[i].dropped_upstream; |
---|
1459 | stat->received += FORMAT_DATA->receivers[i].received_packets; |
---|
1460 | stat->missing += FORMAT_DATA->receivers[i].missing_records; |
---|
1461 | } |
---|
1462 | |
---|
1463 | } |
---|
1464 | |
---|
1465 | static void ndag_get_thread_stats(libtrace_t *libtrace, libtrace_thread_t *t, |
---|
1466 | libtrace_stat_t *stat) { |
---|
1467 | |
---|
1468 | recvstream_t *recvr = (recvstream_t *)t->format_data; |
---|
1469 | |
---|
1470 | if (libtrace == NULL) |
---|
1471 | return; |
---|
1472 | /* TODO Is this thread safe */ |
---|
1473 | stat->dropped_valid = 1; |
---|
1474 | stat->dropped = recvr->dropped_upstream; |
---|
1475 | |
---|
1476 | stat->received_valid = 1; |
---|
1477 | stat->received = recvr->received_packets; |
---|
1478 | |
---|
1479 | stat->missing_valid = 1; |
---|
1480 | stat->missing = recvr->missing_records; |
---|
1481 | |
---|
1482 | } |
---|
1483 | |
---|
1484 | static int ndag_pregister_thread(libtrace_t *libtrace, libtrace_thread_t *t, |
---|
1485 | bool reader) { |
---|
1486 | recvstream_t *recvr; |
---|
1487 | |
---|
1488 | if (!reader || t->type != THREAD_PERPKT) { |
---|
1489 | return 0; |
---|
1490 | } |
---|
1491 | |
---|
1492 | recvr = &(FORMAT_DATA->receivers[t->perpkt_num]); |
---|
1493 | t->format_data = recvr; |
---|
1494 | |
---|
1495 | return 0; |
---|
1496 | } |
---|
1497 | |
---|
1498 | static struct libtrace_format_t ndag = { |
---|
1499 | |
---|
1500 | "ndag", |
---|
1501 | "", |
---|
1502 | TRACE_FORMAT_NDAG, |
---|
1503 | NULL, /* probe filename */ |
---|
1504 | NULL, /* probe magic */ |
---|
1505 | ndag_init_input, /* init_input */ |
---|
1506 | ndag_config_input, /* config_input */ |
---|
1507 | ndag_start_input, /* start_input */ |
---|
1508 | ndag_pause_input, /* pause_input */ |
---|
1509 | NULL, /* init_output */ |
---|
1510 | NULL, /* config_output */ |
---|
1511 | NULL, /* start_output */ |
---|
1512 | ndag_fin_input, /* fin_input */ |
---|
1513 | NULL, /* fin_output */ |
---|
1514 | ndag_read_packet, /* read_packet */ |
---|
1515 | ndag_prepare_packet, /* prepare_packet */ |
---|
1516 | NULL, /* fin_packet */ |
---|
1517 | NULL, /* write_packet */ |
---|
1518 | NULL, /* flush_output */ |
---|
1519 | erf_get_link_type, /* get_link_type */ |
---|
1520 | erf_get_direction, /* get_direction */ |
---|
1521 | erf_set_direction, /* set_direction */ |
---|
1522 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
1523 | NULL, /* get_timeval */ |
---|
1524 | NULL, /* get_seconds */ |
---|
1525 | NULL, /* get_timespec */ |
---|
1526 | NULL, /* get_meta_section */ |
---|
1527 | NULL, /* seek_erf */ |
---|
1528 | NULL, /* seek_timeval */ |
---|
1529 | NULL, /* seek_seconds */ |
---|
1530 | erf_get_capture_length, /* get_capture_length */ |
---|
1531 | erf_get_wire_length, /* get_wire_length */ |
---|
1532 | ndag_get_framing_length, /* get_framing_length */ |
---|
1533 | erf_set_capture_length, /* set_capture_length */ |
---|
1534 | NULL, /* get_received_packets */ |
---|
1535 | NULL, /* get_filtered_packets */ |
---|
1536 | NULL, /* get_dropped_packets */ |
---|
1537 | ndag_get_statistics, /* get_statistics */ |
---|
1538 | NULL, /* get_fd */ |
---|
1539 | trace_event_ndag, /* trace_event */ |
---|
1540 | NULL, /* help */ |
---|
1541 | NULL, /* next pointer */ |
---|
1542 | {true, 0}, /* live packet capture */ |
---|
1543 | ndag_pstart_input, /* parallel start */ |
---|
1544 | ndag_pread_packets, /* parallel read */ |
---|
1545 | ndag_pause_input, /* parallel pause */ |
---|
1546 | NULL, |
---|
1547 | ndag_pregister_thread, /* register thread */ |
---|
1548 | NULL, |
---|
1549 | ndag_get_thread_stats /* per-thread stats */ |
---|
1550 | }; |
---|
1551 | |
---|
1552 | void ndag_constructor(void) { |
---|
1553 | register_format(&ndag); |
---|
1554 | } |
---|