2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2009
5 * Swinburne University of Technology, Melbourne, Australia.
6 * Copyright (c) 2009-2010, The FreeBSD Foundation
9 * Portions of this software were developed at the Centre for Advanced
10 * Internet Architectures, Swinburne University of Technology, Melbourne,
11 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /******************************************************
36 * Statistical Information For TCP Research (SIFTR)
38 * A FreeBSD kernel module that adds very basic intrumentation to the
39 * TCP stack, allowing internal stats to be recorded to a log file
40 * for experimental, debugging and performance analysis purposes.
42 * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst
43 * working on the NewTCP research project at Swinburne University of
44 * Technology's Centre for Advanced Internet Architectures, Melbourne,
45 * Australia, which was made possible in part by a grant from the Cisco
46 * University Research Program Fund at Community Foundation Silicon Valley.
47 * More details are available at:
48 * http://caia.swin.edu.au/urp/newtcp/
50 * Work on SIFTR v1.2.x was sponsored by the FreeBSD Foundation as part of
51 * the "Enhancing the FreeBSD TCP Implementation" project 2008-2009.
52 * More details are available at:
53 * http://www.freebsdfoundation.org/
54 * http://caia.swin.edu.au/freebsd/etcp09/
56 * Lawrence Stewart is the current maintainer, and all contact regarding
57 * SIFTR should be directed to him via email: lastewart@swin.edu.au
59 * Initial release date: June 2007
60 * Most recent update: September 2010
61 ******************************************************/
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/eventhandler.h>
71 #include <sys/kernel.h>
72 #include <sys/kthread.h>
75 #include <sys/module.h>
76 #include <sys/mutex.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
88 #include <net/if_var.h>
91 #include <netinet/in.h>
92 #include <netinet/in_kdtrace.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/tcp_var.h>
101 #include <netinet/ip6.h>
102 #include <netinet/ip6_var.h>
103 #include <netinet6/in6_pcb.h>
104 #endif /* SIFTR_IPV6 */
106 #include <machine/in_cksum.h>
109 * Three digit version number refers to X.Y.Z where:
110 * X is the major version number
111 * Y is bumped to mark backwards incompatible changes
112 * Z is bumped to mark backwards compatible changes
115 #define V_BACKBREAK 2
116 #define V_BACKCOMPAT 4
117 #define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT))
118 #define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \
119 __XSTRING(V_BACKCOMPAT)
123 #define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536
124 #define SYS_NAME "FreeBSD"
125 #define PACKET_TAG_SIFTR 100
126 #define PACKET_COOKIE_SIFTR 21749576
127 #define SIFTR_LOG_FILE_MODE 0644
128 #define SIFTR_DISABLE 0
129 #define SIFTR_ENABLE 1
132 * Hard upper limit on the length of log messages. Bump this up if you add new
133 * data fields such that the line length could exceed the below value.
135 #define MAX_LOG_MSG_LEN 200
136 /* XXX: Make this a sysctl tunable. */
137 #define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN)
140 * 1 byte for IP version
141 * IPv4: src/dst IP (4+4) + src/dst port (2+2) = 12 bytes
142 * IPv6: src/dst IP (16+16) + src/dst port (2+2) = 36 bytes
145 #define FLOW_KEY_LEN 37
147 #define FLOW_KEY_LEN 13
151 #define SIFTR_IPMODE 6
153 #define SIFTR_IPMODE 4
157 #define UPPER_SHORT(X) (((X) & 0xFFFF0000) >> 16)
158 #define LOWER_SHORT(X) ((X) & 0x0000FFFF)
160 #define FIRST_OCTET(X) (((X) & 0xFF000000) >> 24)
161 #define SECOND_OCTET(X) (((X) & 0x00FF0000) >> 16)
162 #define THIRD_OCTET(X) (((X) & 0x0000FF00) >> 8)
163 #define FOURTH_OCTET(X) ((X) & 0x000000FF)
165 static MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR");
166 static MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode",
167 "SIFTR pkt_node struct");
168 static MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode",
169 "SIFTR flow_hash_node struct");
171 /* Used as links in the pkt manager queue. */
173 /* Timestamp of pkt as noted in the pfil hook. */
175 /* Direction pkt is travelling. */
180 /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */
182 /* Hash of the pkt which triggered the log message. */
184 /* Local/foreign IP address. */
186 uint32_t ip_laddr[4];
187 uint32_t ip_faddr[4];
192 /* Local TCP port. */
193 uint16_t tcp_localport;
194 /* Foreign TCP port. */
195 uint16_t tcp_foreignport;
196 /* Congestion Window (bytes). */
198 /* Sending Window (bytes). */
200 /* Receive Window (bytes). */
202 /* Unused (was: Bandwidth Controlled Window (bytes)). */
204 /* Slow Start Threshold (bytes). */
206 /* Current state of the TCP FSM. */
208 /* Max Segment Size (bytes). */
211 * Smoothed RTT stored as found in the TCP control block
212 * in units of (TCP_RTT_SCALE*hz).
215 /* Is SACK enabled? */
217 /* Window scaling for snd window. */
219 /* Window scaling for recv window. */
221 /* TCP control block flags. */
223 /* Retransmit timeout length. */
225 /* Size of the TCP send buffer in bytes. */
226 u_int snd_buf_hiwater;
227 /* Current num bytes in the send socket buffer. */
229 /* Size of the TCP receive buffer in bytes. */
230 u_int rcv_buf_hiwater;
231 /* Current num bytes in the receive socket buffer. */
233 /* Number of bytes inflight that we are waiting on ACKs for. */
234 u_int sent_inflight_bytes;
235 /* Number of segments currently in the reassembly queue. */
237 /* Flowid for the connection. */
239 /* Flow type for the connection. */
241 /* Link to next pkt_node in the list. */
242 STAILQ_ENTRY(pkt_node) nodes;
245 struct flow_hash_node
248 uint8_t key[FLOW_KEY_LEN];
249 LIST_ENTRY(flow_hash_node) nodes;
254 /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */
257 /* # pkts skipped due to failed malloc calls. */
258 uint32_t nskip_in_malloc;
259 uint32_t nskip_out_malloc;
260 /* # pkts skipped due to failed mtx acquisition. */
261 uint32_t nskip_in_mtx;
262 uint32_t nskip_out_mtx;
263 /* # pkts skipped due to failed inpcb lookups. */
264 uint32_t nskip_in_inpcb;
265 uint32_t nskip_out_inpcb;
266 /* # pkts skipped due to failed tcpcb lookups. */
267 uint32_t nskip_in_tcpcb;
268 uint32_t nskip_out_tcpcb;
269 /* # pkts skipped due to stack reinjection. */
270 uint32_t nskip_in_dejavu;
271 uint32_t nskip_out_dejavu;
274 DPCPU_DEFINE_STATIC(struct siftr_stats, ss);
276 static volatile unsigned int siftr_exit_pkt_manager_thread = 0;
277 static unsigned int siftr_enabled = 0;
278 static unsigned int siftr_pkts_per_log = 1;
279 static unsigned int siftr_generate_hashes = 0;
280 static uint16_t siftr_port_filter = 0;
281 /* static unsigned int siftr_binary_log = 0; */
282 static char siftr_logfile[PATH_MAX] = "/var/log/siftr.log";
283 static char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log";
284 static u_long siftr_hashmask;
285 STAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue);
286 LIST_HEAD(listhead, flow_hash_node) *counter_hash;
287 static int wait_for_pkt;
288 static struct alq *siftr_alq = NULL;
289 static struct mtx siftr_pkt_queue_mtx;
290 static struct mtx siftr_pkt_mgr_mtx;
291 static struct thread *siftr_pkt_manager_thr = NULL;
292 static char direction[2] = {'i','o'};
294 /* Required function prototypes. */
295 static int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS);
296 static int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS);
299 /* Declare the net.inet.siftr sysctl tree and populate it. */
301 SYSCTL_DECL(_net_inet_siftr);
303 SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
304 "siftr related settings");
306 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled,
307 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
308 &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU",
309 "switch siftr module operations on/off");
311 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile,
312 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &siftr_logfile_shadow,
313 sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler, "A",
314 "file to save siftr log messages to");
316 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW,
317 &siftr_pkts_per_log, 1,
318 "number of packets between generating a log message");
320 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW,
321 &siftr_generate_hashes, 0,
322 "enable packet hash generation");
324 SYSCTL_U16(_net_inet_siftr, OID_AUTO, port_filter, CTLFLAG_RW,
325 &siftr_port_filter, 0,
326 "enable packet filter on a TCP port");
329 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW,
330 &siftr_binary_log, 0,
331 "write log files in binary instead of ascii");
335 /* Begin functions. */
338 siftr_process_pkt(struct pkt_node * pkt_node)
340 struct flow_hash_node *hash_node;
341 struct listhead *counter_list;
342 struct siftr_stats *ss;
344 uint8_t key[FLOW_KEY_LEN];
345 uint8_t found_match, key_offset;
353 * Create the key that will be used to create a hash index
354 * into our hash table. Our key consists of:
355 * ipversion, localip, localport, foreignip, foreignport
357 key[0] = pkt_node->ipver;
358 memcpy(key + key_offset, &pkt_node->ip_laddr,
359 sizeof(pkt_node->ip_laddr));
360 key_offset += sizeof(pkt_node->ip_laddr);
361 memcpy(key + key_offset, &pkt_node->tcp_localport,
362 sizeof(pkt_node->tcp_localport));
363 key_offset += sizeof(pkt_node->tcp_localport);
364 memcpy(key + key_offset, &pkt_node->ip_faddr,
365 sizeof(pkt_node->ip_faddr));
366 key_offset += sizeof(pkt_node->ip_faddr);
367 memcpy(key + key_offset, &pkt_node->tcp_foreignport,
368 sizeof(pkt_node->tcp_foreignport));
370 counter_list = counter_hash +
371 (hash32_buf(key, sizeof(key), 0) & siftr_hashmask);
374 * If the list is not empty i.e. the hash index has
375 * been used by another flow previously.
377 if (LIST_FIRST(counter_list) != NULL) {
379 * Loop through the hash nodes in the list.
380 * There should normally only be 1 hash node in the list,
381 * except if there have been collisions at the hash index
382 * computed by hash32_buf().
384 LIST_FOREACH(hash_node, counter_list, nodes) {
386 * Check if the key for the pkt we are currently
387 * processing is the same as the key stored in the
388 * hash node we are currently processing.
389 * If they are the same, then we've found the
390 * hash node that stores the counter for the flow
391 * the pkt belongs to.
393 if (memcmp(hash_node->key, key, sizeof(key)) == 0) {
400 /* If this flow hash hasn't been seen before or we have a collision. */
401 if (hash_node == NULL || !found_match) {
402 /* Create a new hash node to store the flow's counter. */
403 hash_node = malloc(sizeof(struct flow_hash_node),
404 M_SIFTR_HASHNODE, M_WAITOK);
406 if (hash_node != NULL) {
407 /* Initialise our new hash node list entry. */
408 hash_node->counter = 0;
409 memcpy(hash_node->key, key, sizeof(key));
410 LIST_INSERT_HEAD(counter_list, hash_node, nodes);
413 if (pkt_node->direction == DIR_IN)
414 ss->nskip_in_malloc++;
416 ss->nskip_out_malloc++;
420 } else if (siftr_pkts_per_log > 1) {
422 * Taking the remainder of the counter divided
423 * by the current value of siftr_pkts_per_log
424 * and storing that in counter provides a neat
425 * way to modulate the frequency of log
426 * messages being written to the log file.
428 hash_node->counter = (hash_node->counter + 1) %
432 * If we have not seen enough packets since the last time
433 * we wrote a log message for this connection, return.
435 if (hash_node->counter > 0)
439 log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK);
442 return; /* Should only happen if the ALQ is shutting down. */
445 pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]);
446 pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]);
448 if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */
449 pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]);
450 pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]);
451 pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]);
452 pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]);
453 pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]);
454 pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]);
456 /* Construct an IPv6 log message. */
457 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
459 "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:"
460 "%x:%x:%x:%x:%x,%u,%ld,%ld,%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,"
461 "%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n",
462 direction[pkt_node->direction],
464 pkt_node->tval.tv_sec,
465 pkt_node->tval.tv_usec,
466 UPPER_SHORT(pkt_node->ip_laddr[0]),
467 LOWER_SHORT(pkt_node->ip_laddr[0]),
468 UPPER_SHORT(pkt_node->ip_laddr[1]),
469 LOWER_SHORT(pkt_node->ip_laddr[1]),
470 UPPER_SHORT(pkt_node->ip_laddr[2]),
471 LOWER_SHORT(pkt_node->ip_laddr[2]),
472 UPPER_SHORT(pkt_node->ip_laddr[3]),
473 LOWER_SHORT(pkt_node->ip_laddr[3]),
474 ntohs(pkt_node->tcp_localport),
475 UPPER_SHORT(pkt_node->ip_faddr[0]),
476 LOWER_SHORT(pkt_node->ip_faddr[0]),
477 UPPER_SHORT(pkt_node->ip_faddr[1]),
478 LOWER_SHORT(pkt_node->ip_faddr[1]),
479 UPPER_SHORT(pkt_node->ip_faddr[2]),
480 LOWER_SHORT(pkt_node->ip_faddr[2]),
481 UPPER_SHORT(pkt_node->ip_faddr[3]),
482 LOWER_SHORT(pkt_node->ip_faddr[3]),
483 ntohs(pkt_node->tcp_foreignport),
484 pkt_node->snd_ssthresh,
491 pkt_node->conn_state,
492 pkt_node->max_seg_size,
493 pkt_node->smoothed_rtt,
494 pkt_node->sack_enabled,
496 pkt_node->rxt_length,
497 pkt_node->snd_buf_hiwater,
498 pkt_node->snd_buf_cc,
499 pkt_node->rcv_buf_hiwater,
500 pkt_node->rcv_buf_cc,
501 pkt_node->sent_inflight_bytes,
505 } else { /* IPv4 packet */
506 pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]);
507 pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]);
508 pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]);
509 pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]);
510 pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]);
511 pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]);
512 pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]);
513 pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]);
514 #endif /* SIFTR_IPV6 */
516 /* Construct an IPv4 log message. */
517 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
519 "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%ld,%ld,"
520 "%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n",
521 direction[pkt_node->direction],
523 (intmax_t)pkt_node->tval.tv_sec,
524 pkt_node->tval.tv_usec,
525 pkt_node->ip_laddr[0],
526 pkt_node->ip_laddr[1],
527 pkt_node->ip_laddr[2],
528 pkt_node->ip_laddr[3],
529 ntohs(pkt_node->tcp_localport),
530 pkt_node->ip_faddr[0],
531 pkt_node->ip_faddr[1],
532 pkt_node->ip_faddr[2],
533 pkt_node->ip_faddr[3],
534 ntohs(pkt_node->tcp_foreignport),
535 pkt_node->snd_ssthresh,
542 pkt_node->conn_state,
543 pkt_node->max_seg_size,
544 pkt_node->smoothed_rtt,
545 pkt_node->sack_enabled,
547 pkt_node->rxt_length,
548 pkt_node->snd_buf_hiwater,
549 pkt_node->snd_buf_cc,
550 pkt_node->rcv_buf_hiwater,
551 pkt_node->rcv_buf_cc,
552 pkt_node->sent_inflight_bytes,
560 alq_post_flags(siftr_alq, log_buf, 0);
565 siftr_pkt_manager_thread(void *arg)
567 STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue =
568 STAILQ_HEAD_INITIALIZER(tmp_pkt_queue);
569 struct pkt_node *pkt_node, *pkt_node_temp;
574 mtx_lock(&siftr_pkt_mgr_mtx);
576 /* draining == 0 when queue has been flushed and it's safe to exit. */
579 * Sleep until we are signalled to wake because thread has
580 * been told to exit or until 1 tick has passed.
582 mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait",
585 /* Gain exclusive access to the pkt_node queue. */
586 mtx_lock(&siftr_pkt_queue_mtx);
589 * Move pkt_queue to tmp_pkt_queue, which leaves
590 * pkt_queue empty and ready to receive more pkt_nodes.
592 STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue);
595 * We've finished making changes to the list. Unlock it
596 * so the pfil hooks can continue queuing pkt_nodes.
598 mtx_unlock(&siftr_pkt_queue_mtx);
601 * We can't hold a mutex whilst calling siftr_process_pkt
602 * because ALQ might sleep waiting for buffer space.
604 mtx_unlock(&siftr_pkt_mgr_mtx);
606 /* Flush all pkt_nodes to the log file. */
607 STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes,
609 siftr_process_pkt(pkt_node);
610 STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes);
611 free(pkt_node, M_SIFTR_PKTNODE);
614 KASSERT(STAILQ_EMPTY(&tmp_pkt_queue),
615 ("SIFTR tmp_pkt_queue not empty after flush"));
617 mtx_lock(&siftr_pkt_mgr_mtx);
620 * If siftr_exit_pkt_manager_thread gets set during the window
621 * where we are draining the tmp_pkt_queue above, there might
622 * still be pkts in pkt_queue that need to be drained.
623 * Allow one further iteration to occur after
624 * siftr_exit_pkt_manager_thread has been set to ensure
625 * pkt_queue is completely empty before we kill the thread.
627 * siftr_exit_pkt_manager_thread is set only after the pfil
628 * hooks have been removed, so only 1 extra iteration
629 * is needed to drain the queue.
631 if (siftr_exit_pkt_manager_thread)
635 mtx_unlock(&siftr_pkt_mgr_mtx);
637 /* Calls wakeup on this thread's struct thread ptr. */
643 hash_pkt(struct mbuf *m, uint32_t offset)
649 while (m != NULL && offset > m->m_len) {
651 * The IP packet payload does not start in this mbuf, so
652 * need to figure out which mbuf it starts in and what offset
653 * into the mbuf's data region the payload starts at.
660 /* Ensure there is data in the mbuf */
661 if ((m->m_len - offset) > 0)
662 hash = hash32_buf(m->m_data + offset,
663 m->m_len - offset, hash);
674 * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that
675 * it's a reinjected packet and return. If it doesn't, tag the mbuf and return.
676 * Return value >0 means the caller should skip processing this mbuf.
679 siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss)
681 if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL)
684 ss->nskip_in_dejavu++;
686 ss->nskip_out_dejavu++;
690 struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR,
691 PACKET_TAG_SIFTR, 0, M_NOWAIT);
694 ss->nskip_in_malloc++;
696 ss->nskip_out_malloc++;
701 m_tag_prepend(m, tag);
709 * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL
712 static inline struct inpcb *
713 siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport,
714 uint16_t dport, int dir, struct siftr_stats *ss)
718 /* We need the tcbinfo lock. */
719 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
722 inp = (ipver == INP_IPV4 ?
723 in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst,
724 dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
727 in6_pcblookup(&V_tcbinfo,
728 &((struct ip6_hdr *)ip)->ip6_src, sport,
729 &((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB,
737 inp = (ipver == INP_IPV4 ?
738 in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src,
739 sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
742 in6_pcblookup(&V_tcbinfo,
743 &((struct ip6_hdr *)ip)->ip6_dst, dport,
744 &((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB,
751 /* If we can't find the inpcb, bail. */
754 ss->nskip_in_inpcb++;
756 ss->nskip_out_inpcb++;
764 siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp,
765 int ipver, int dir, int inp_locally_locked)
768 if (ipver == INP_IPV4) {
769 pn->ip_laddr[3] = inp->inp_laddr.s_addr;
770 pn->ip_faddr[3] = inp->inp_faddr.s_addr;
772 *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr;
773 *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr;
777 pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0];
778 pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1];
779 pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2];
780 pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3];
781 pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0];
782 pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1];
783 pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2];
784 pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3];
787 pn->tcp_localport = inp->inp_lport;
788 pn->tcp_foreignport = inp->inp_fport;
789 pn->snd_cwnd = tp->snd_cwnd;
790 pn->snd_wnd = tp->snd_wnd;
791 pn->rcv_wnd = tp->rcv_wnd;
792 pn->snd_bwnd = 0; /* Unused, kept for compat. */
793 pn->snd_ssthresh = tp->snd_ssthresh;
794 pn->snd_scale = tp->snd_scale;
795 pn->rcv_scale = tp->rcv_scale;
796 pn->conn_state = tp->t_state;
797 pn->max_seg_size = tp->t_maxseg;
798 pn->smoothed_rtt = tp->t_srtt;
799 pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0;
800 pn->flags = tp->t_flags;
801 pn->rxt_length = tp->t_rxtcur;
802 pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat;
803 pn->snd_buf_cc = sbused(&inp->inp_socket->so_snd);
804 pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat;
805 pn->rcv_buf_cc = sbused(&inp->inp_socket->so_rcv);
806 pn->sent_inflight_bytes = tp->snd_max - tp->snd_una;
807 pn->t_segqlen = tp->t_segqlen;
808 pn->flowid = inp->inp_flowid;
809 pn->flowtype = inp->inp_flowtype;
811 /* We've finished accessing the tcb so release the lock. */
812 if (inp_locally_locked)
816 pn->direction = (dir == PFIL_IN ? DIR_IN : DIR_OUT);
819 * Significantly more accurate than using getmicrotime(), but slower!
820 * Gives true microsecond resolution at the expense of a hit to
821 * maximum pps throughput processing when SIFTR is loaded and enabled.
823 microtime(&pn->tval);
824 TCP_PROBE1(siftr, &pn);
830 * pfil hook that is called for each IPv4 packet making its way through the
831 * stack in either direction.
832 * The pfil subsystem holds a non-sleepable mutex somewhere when
833 * calling our hook function, so we can't sleep at all.
834 * It's very important to use the M_NOWAIT flag with all function calls
835 * that support it so that they won't sleep, otherwise you get a panic.
838 siftr_chkpkt(struct mbuf **m, struct ifnet *ifp, int flags,
839 void *ruleset __unused, struct inpcb *inp)
845 struct siftr_stats *ss;
847 int inp_locally_locked, dir;
849 inp_locally_locked = 0;
850 dir = PFIL_DIR(flags);
854 * m_pullup is not required here because ip_{input|output}
855 * already do the heavy lifting for us.
858 ip = mtod(*m, struct ip *);
860 /* Only continue processing if the packet is TCP. */
861 if (ip->ip_p != IPPROTO_TCP)
865 * If a kernel subsystem reinjects packets into the stack, our pfil
866 * hook will be called multiple times for the same packet.
867 * Make sure we only process unique packets.
869 if (siftr_chkreinject(*m, dir, ss))
878 * Create a tcphdr struct starting at the correct offset
879 * in the IP packet. ip->ip_hl gives the ip header length
880 * in 4-byte words, so multiply it to get the size in bytes.
882 ip_hl = (ip->ip_hl << 2);
883 th = (struct tcphdr *)((caddr_t)ip + ip_hl);
886 * If the pfil hooks don't provide a pointer to the
887 * inpcb, we need to find it ourselves and lock it.
890 /* Find the corresponding inpcb for this pkt. */
891 inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport,
892 th->th_dport, dir, ss);
897 inp_locally_locked = 1;
900 INP_LOCK_ASSERT(inp);
902 /* Find the TCP control block that corresponds with this packet */
906 * If we can't find the TCP control block (happens occasionaly for a
907 * packet sent during the shutdown phase of a TCP connection),
908 * or we're in the timewait state, bail
910 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) {
912 ss->nskip_in_tcpcb++;
914 ss->nskip_out_tcpcb++;
920 * Only pkts selected by the tcp port filter
921 * can be inserted into the pkt_queue
923 if ((siftr_port_filter != 0) &&
924 (siftr_port_filter != ntohs(inp->inp_lport)) &&
925 (siftr_port_filter != ntohs(inp->inp_fport))) {
929 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
933 ss->nskip_in_malloc++;
935 ss->nskip_out_malloc++;
940 siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked);
942 if (siftr_generate_hashes) {
943 if ((*m)->m_pkthdr.csum_flags & CSUM_TCP) {
945 * For outbound packets, the TCP checksum isn't
946 * calculated yet. This is a problem for our packet
947 * hashing as the receiver will calc a different hash
948 * to ours if we don't include the correct TCP checksum
949 * in the bytes being hashed. To work around this
950 * problem, we manually calc the TCP checksum here in
951 * software. We unset the CSUM_TCP flag so the lower
952 * layers don't recalc it.
954 (*m)->m_pkthdr.csum_flags &= ~CSUM_TCP;
957 * Calculate the TCP checksum in software and assign
958 * to correct TCP header field, which will follow the
959 * packet mbuf down the stack. The trick here is that
960 * tcp_output() sets th->th_sum to the checksum of the
961 * pseudo header for us already. Because of the nature
962 * of the checksumming algorithm, we can sum over the
963 * entire IP payload (i.e. TCP header and data), which
964 * will include the already calculated pseduo header
965 * checksum, thus giving us the complete TCP checksum.
967 * To put it in simple terms, if checksum(1,2,3,4)=10,
968 * then checksum(1,2,3,4,5) == checksum(10,5).
969 * This property is what allows us to "cheat" and
970 * checksum only the IP payload which has the TCP
971 * th_sum field populated with the pseudo header's
972 * checksum, and not need to futz around checksumming
973 * pseudo header bytes and TCP header/data in one hit.
974 * Refer to RFC 1071 for more info.
976 * NB: in_cksum_skip(struct mbuf *m, int len, int skip)
977 * in_cksum_skip 2nd argument is NOT the number of
978 * bytes to read from the mbuf at "skip" bytes offset
979 * from the start of the mbuf (very counter intuitive!).
980 * The number of bytes to read is calculated internally
981 * by the function as len-skip i.e. to sum over the IP
982 * payload (TCP header + data) bytes, it is INCORRECT
983 * to call the function like this:
984 * in_cksum_skip(at, ip->ip_len - offset, offset)
985 * Rather, it should be called like this:
986 * in_cksum_skip(at, ip->ip_len, offset)
987 * which means read "ip->ip_len - offset" bytes from
988 * the mbuf cluster "at" at offset "offset" bytes from
989 * the beginning of the "at" mbuf's data pointer.
991 th->th_sum = in_cksum_skip(*m, ntohs(ip->ip_len),
996 * XXX: Having to calculate the checksum in software and then
997 * hash over all bytes is really inefficient. Would be nice to
998 * find a way to create the hash and checksum in the same pass
1001 pn->hash = hash_pkt(*m, ip_hl);
1004 mtx_lock(&siftr_pkt_queue_mtx);
1005 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
1006 mtx_unlock(&siftr_pkt_queue_mtx);
1010 if (inp_locally_locked)
1020 siftr_chkpkt6(struct mbuf **m, struct ifnet *ifp, int flags, struct inpcb *inp)
1022 struct pkt_node *pn;
1023 struct ip6_hdr *ip6;
1026 struct siftr_stats *ss;
1027 unsigned int ip6_hl;
1028 int inp_locally_locked, dir;
1030 inp_locally_locked = 0;
1031 dir = PFIL_DIR(flags);
1035 * m_pullup is not required here because ip6_{input|output}
1036 * already do the heavy lifting for us.
1039 ip6 = mtod(*m, struct ip6_hdr *);
1042 * Only continue processing if the packet is TCP
1043 * XXX: We should follow the next header fields
1044 * as shown on Pg 6 RFC 2460, but right now we'll
1045 * only check pkts that have no extension headers.
1047 if (ip6->ip6_nxt != IPPROTO_TCP)
1051 * If a kernel subsystem reinjects packets into the stack, our pfil
1052 * hook will be called multiple times for the same packet.
1053 * Make sure we only process unique packets.
1055 if (siftr_chkreinject(*m, dir, ss))
1063 ip6_hl = sizeof(struct ip6_hdr);
1066 * Create a tcphdr struct starting at the correct offset
1067 * in the ipv6 packet. ip->ip_hl gives the ip header length
1068 * in 4-byte words, so multiply it to get the size in bytes.
1070 th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl);
1073 * For inbound packets, the pfil hooks don't provide a pointer to the
1074 * inpcb, so we need to find it ourselves and lock it.
1077 /* Find the corresponding inpcb for this pkt. */
1078 inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m,
1079 th->th_sport, th->th_dport, dir, ss);
1084 inp_locally_locked = 1;
1087 /* Find the TCP control block that corresponds with this packet. */
1088 tp = intotcpcb(inp);
1091 * If we can't find the TCP control block (happens occasionaly for a
1092 * packet sent during the shutdown phase of a TCP connection),
1093 * or we're in the timewait state, bail.
1095 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) {
1097 ss->nskip_in_tcpcb++;
1099 ss->nskip_out_tcpcb++;
1105 * Only pkts selected by the tcp port filter
1106 * can be inserted into the pkt_queue
1108 if ((siftr_port_filter != 0) &&
1109 (siftr_port_filter != ntohs(inp->inp_lport)) &&
1110 (siftr_port_filter != ntohs(inp->inp_fport))) {
1114 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
1118 ss->nskip_in_malloc++;
1120 ss->nskip_out_malloc++;
1125 siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked);
1127 /* XXX: Figure out how to generate hashes for IPv6 packets. */
1129 mtx_lock(&siftr_pkt_queue_mtx);
1130 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
1131 mtx_unlock(&siftr_pkt_queue_mtx);
1135 if (inp_locally_locked)
1139 /* Returning 0 ensures pfil will not discard the pkt. */
1142 #endif /* #ifdef SIFTR_IPV6 */
1144 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet_hook);
1145 #define V_siftr_inet_hook VNET(siftr_inet_hook)
1147 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet6_hook);
1148 #define V_siftr_inet6_hook VNET(siftr_inet6_hook)
1151 siftr_pfil(int action)
1153 struct pfil_hook_args pha;
1154 struct pfil_link_args pla;
1156 pha.pa_version = PFIL_VERSION;
1157 pha.pa_flags = PFIL_IN | PFIL_OUT;
1158 pha.pa_modname = "siftr";
1159 pha.pa_ruleset = NULL;
1160 pha.pa_rulname = "default";
1162 pla.pa_version = PFIL_VERSION;
1163 pla.pa_flags = PFIL_IN | PFIL_OUT |
1164 PFIL_HEADPTR | PFIL_HOOKPTR;
1166 VNET_ITERATOR_DECL(vnet_iter);
1169 VNET_FOREACH(vnet_iter) {
1170 CURVNET_SET(vnet_iter);
1172 if (action == HOOK) {
1173 pha.pa_func = siftr_chkpkt;
1174 pha.pa_type = PFIL_TYPE_IP4;
1175 V_siftr_inet_hook = pfil_add_hook(&pha);
1176 pla.pa_hook = V_siftr_inet_hook;
1177 pla.pa_head = V_inet_pfil_head;
1178 (void)pfil_link(&pla);
1180 pha.pa_func = siftr_chkpkt6;
1181 pha.pa_type = PFIL_TYPE_IP6;
1182 V_siftr_inet6_hook = pfil_add_hook(&pha);
1183 pla.pa_hook = V_siftr_inet6_hook;
1184 pla.pa_head = V_inet6_pfil_head;
1185 (void)pfil_link(&pla);
1187 } else if (action == UNHOOK) {
1188 pfil_remove_hook(V_siftr_inet_hook);
1190 pfil_remove_hook(V_siftr_inet6_hook);
1195 VNET_LIST_RUNLOCK();
1202 siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS)
1204 struct alq *new_alq;
1207 error = sysctl_handle_string(oidp, arg1, arg2, req);
1209 /* Check for error or same filename */
1210 if (error != 0 || req->newptr == NULL ||
1211 strncmp(siftr_logfile, arg1, arg2) == 0)
1214 /* Filname changed */
1215 error = alq_open(&new_alq, arg1, curthread->td_ucred,
1216 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1221 * If disabled, siftr_alq == NULL so we simply close
1222 * the alq as we've proved it can be opened.
1223 * If enabled, close the existing alq and switch the old
1226 if (siftr_alq == NULL) {
1229 alq_close(siftr_alq);
1230 siftr_alq = new_alq;
1233 /* Update filename upon success */
1234 strlcpy(siftr_logfile, arg1, arg2);
1240 siftr_manage_ops(uint8_t action)
1242 struct siftr_stats totalss;
1243 struct timeval tval;
1244 struct flow_hash_node *counter, *tmp_counter;
1246 int i, key_index, error;
1247 uint32_t bytes_to_write, total_skipped_pkts;
1248 uint16_t lport, fport;
1249 uint8_t *key, ipver __unused;
1260 total_skipped_pkts = 0;
1262 /* Init an autosizing sbuf that initially holds 200 chars. */
1263 if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL)
1266 if (action == SIFTR_ENABLE && siftr_pkt_manager_thr == NULL) {
1269 * XXX: We should abort if alq_open fails!
1271 alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred,
1272 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1274 STAILQ_INIT(&pkt_queue);
1278 siftr_exit_pkt_manager_thread = 0;
1280 kthread_add(&siftr_pkt_manager_thread, NULL, NULL,
1281 &siftr_pkt_manager_thr, RFNOWAIT, 0,
1282 "siftr_pkt_manager_thr");
1289 "enable_time_secs=%jd\tenable_time_usecs=%06ld\t"
1290 "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t"
1291 "sysver=%u\tipmode=%u\n",
1292 (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz,
1293 TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE);
1296 alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK);
1298 } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) {
1300 * Remove the pfil hook functions. All threads currently in
1301 * the hook functions are allowed to exit before siftr_pfil()
1306 /* This will block until the pkt manager thread unlocks it. */
1307 mtx_lock(&siftr_pkt_mgr_mtx);
1309 /* Tell the pkt manager thread that it should exit now. */
1310 siftr_exit_pkt_manager_thread = 1;
1313 * Wake the pkt_manager thread so it realises that
1314 * siftr_exit_pkt_manager_thread == 1 and exits gracefully.
1315 * The wakeup won't be delivered until we unlock
1316 * siftr_pkt_mgr_mtx so this isn't racy.
1318 wakeup(&wait_for_pkt);
1320 /* Wait for the pkt_manager thread to exit. */
1321 mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT,
1324 siftr_pkt_manager_thr = NULL;
1325 mtx_unlock(&siftr_pkt_mgr_mtx);
1327 totalss.n_in = DPCPU_VARSUM(ss, n_in);
1328 totalss.n_out = DPCPU_VARSUM(ss, n_out);
1329 totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc);
1330 totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc);
1331 totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx);
1332 totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx);
1333 totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb);
1334 totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb);
1335 totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb);
1336 totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb);
1338 total_skipped_pkts = totalss.nskip_in_malloc +
1339 totalss.nskip_out_malloc + totalss.nskip_in_mtx +
1340 totalss.nskip_out_mtx + totalss.nskip_in_tcpcb +
1341 totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb +
1342 totalss.nskip_out_inpcb;
1347 "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t"
1348 "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t"
1349 "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t"
1350 "num_outbound_skipped_pkts_malloc=%u\t"
1351 "num_inbound_skipped_pkts_mtx=%u\t"
1352 "num_outbound_skipped_pkts_mtx=%u\t"
1353 "num_inbound_skipped_pkts_tcpcb=%u\t"
1354 "num_outbound_skipped_pkts_tcpcb=%u\t"
1355 "num_inbound_skipped_pkts_inpcb=%u\t"
1356 "num_outbound_skipped_pkts_inpcb=%u\t"
1357 "total_skipped_tcp_pkts=%u\tflow_list=",
1358 (intmax_t)tval.tv_sec,
1360 (uintmax_t)totalss.n_in,
1361 (uintmax_t)totalss.n_out,
1362 (uintmax_t)(totalss.n_in + totalss.n_out),
1363 totalss.nskip_in_malloc,
1364 totalss.nskip_out_malloc,
1365 totalss.nskip_in_mtx,
1366 totalss.nskip_out_mtx,
1367 totalss.nskip_in_tcpcb,
1368 totalss.nskip_out_tcpcb,
1369 totalss.nskip_in_inpcb,
1370 totalss.nskip_out_inpcb,
1371 total_skipped_pkts);
1374 * Iterate over the flow hash, printing a summary of each
1375 * flow seen and freeing any malloc'd memory.
1376 * The hash consists of an array of LISTs (man 3 queue).
1378 for (i = 0; i <= siftr_hashmask; i++) {
1379 LIST_FOREACH_SAFE(counter, counter_hash + i, nodes,
1386 memcpy(laddr, key + key_index, sizeof(laddr));
1387 key_index += sizeof(laddr);
1388 memcpy(&lport, key + key_index, sizeof(lport));
1389 key_index += sizeof(lport);
1390 memcpy(faddr, key + key_index, sizeof(faddr));
1391 key_index += sizeof(faddr);
1392 memcpy(&fport, key + key_index, sizeof(fport));
1395 laddr[3] = ntohl(laddr[3]);
1396 faddr[3] = ntohl(faddr[3]);
1398 if (ipver == INP_IPV6) {
1399 laddr[0] = ntohl(laddr[0]);
1400 laddr[1] = ntohl(laddr[1]);
1401 laddr[2] = ntohl(laddr[2]);
1402 faddr[0] = ntohl(faddr[0]);
1403 faddr[1] = ntohl(faddr[1]);
1404 faddr[2] = ntohl(faddr[2]);
1407 "%x:%x:%x:%x:%x:%x:%x:%x;%u-"
1408 "%x:%x:%x:%x:%x:%x:%x:%x;%u,",
1409 UPPER_SHORT(laddr[0]),
1410 LOWER_SHORT(laddr[0]),
1411 UPPER_SHORT(laddr[1]),
1412 LOWER_SHORT(laddr[1]),
1413 UPPER_SHORT(laddr[2]),
1414 LOWER_SHORT(laddr[2]),
1415 UPPER_SHORT(laddr[3]),
1416 LOWER_SHORT(laddr[3]),
1418 UPPER_SHORT(faddr[0]),
1419 LOWER_SHORT(faddr[0]),
1420 UPPER_SHORT(faddr[1]),
1421 LOWER_SHORT(faddr[1]),
1422 UPPER_SHORT(faddr[2]),
1423 LOWER_SHORT(faddr[2]),
1424 UPPER_SHORT(faddr[3]),
1425 LOWER_SHORT(faddr[3]),
1428 laddr[0] = FIRST_OCTET(laddr[3]);
1429 laddr[1] = SECOND_OCTET(laddr[3]);
1430 laddr[2] = THIRD_OCTET(laddr[3]);
1431 laddr[3] = FOURTH_OCTET(laddr[3]);
1432 faddr[0] = FIRST_OCTET(faddr[3]);
1433 faddr[1] = SECOND_OCTET(faddr[3]);
1434 faddr[2] = THIRD_OCTET(faddr[3]);
1435 faddr[3] = FOURTH_OCTET(faddr[3]);
1438 "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,",
1453 free(counter, M_SIFTR_HASHNODE);
1456 LIST_INIT(counter_hash + i);
1459 sbuf_printf(s, "\n");
1464 bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i);
1465 alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK);
1466 i += bytes_to_write;
1467 } while (i < sbuf_len(s));
1469 alq_close(siftr_alq);
1477 * XXX: Should be using ret to check if any functions fail
1478 * and set error appropriately
1486 siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS)
1491 new = siftr_enabled;
1492 error = sysctl_handle_int(oidp, &new, 0, req);
1493 if (error == 0 && req->newptr != NULL) {
1496 else if (new != siftr_enabled) {
1497 if ((error = siftr_manage_ops(new)) == 0) {
1498 siftr_enabled = new;
1500 siftr_manage_ops(SIFTR_DISABLE);
1510 siftr_shutdown_handler(void *arg)
1512 if (siftr_enabled == 1) {
1513 siftr_manage_ops(SIFTR_DISABLE);
1519 * Module is being unloaded or machine is shutting down. Take care of cleanup.
1525 siftr_manage_ops(SIFTR_DISABLE);
1526 hashdestroy(counter_hash, M_SIFTR, siftr_hashmask);
1527 mtx_destroy(&siftr_pkt_queue_mtx);
1528 mtx_destroy(&siftr_pkt_mgr_mtx);
1535 * Module has just been loaded into the kernel.
1540 EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL,
1541 SHUTDOWN_PRI_FIRST);
1543 /* Initialise our flow counter hash table. */
1544 counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR,
1547 mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF);
1548 mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF);
1550 /* Print message to the user's current terminal. */
1551 uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n"
1552 " http://caia.swin.edu.au/urp/newtcp\n\n",
1560 * This is the function that is called to load and unload the module.
1561 * When the module is loaded, this function is called once with
1562 * "what" == MOD_LOAD
1563 * When the module is unloaded, this function is called twice with
1564 * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second
1565 * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command,
1566 * this function is called once with "what" = MOD_SHUTDOWN
1567 * When the system is shut down, the handler isn't called until the very end
1568 * of the shutdown sequence i.e. after the disks have been synced.
1571 siftr_load_handler(module_t mod, int what, void *arg)
1582 ret = deinit_siftr();
1598 static moduledata_t siftr_mod = {
1600 .evhand = siftr_load_handler,
1604 * Param 1: name of the kernel module
1605 * Param 2: moduledata_t struct containing info about the kernel module
1606 * and the execution entry point for the module
1607 * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h
1608 * Defines the module initialisation order
1609 * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h
1610 * Defines the initialisation order of this kld relative to others
1611 * within the same subsystem as defined by param 3
1613 DECLARE_MODULE(siftr, siftr_mod, SI_SUB_LAST, SI_ORDER_ANY);
1614 MODULE_DEPEND(siftr, alq, 1, 1, 1);
1615 MODULE_VERSION(siftr, MODVERSION);