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 <netinet6/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 300
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 /* More tcpcb flags storage */
204 /* Slow Start Threshold (bytes). */
205 uint32_t snd_ssthresh;
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);
298 /* Declare the net.inet.siftr sysctl tree and populate it. */
300 SYSCTL_DECL(_net_inet_siftr);
302 SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
303 "siftr related settings");
305 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled,
306 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
307 &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU",
308 "switch siftr module operations on/off");
310 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile,
311 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &siftr_logfile_shadow,
312 sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler, "A",
313 "file to save siftr log messages to");
315 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW,
316 &siftr_pkts_per_log, 1,
317 "number of packets between generating a log message");
319 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW,
320 &siftr_generate_hashes, 0,
321 "enable packet hash generation");
323 SYSCTL_U16(_net_inet_siftr, OID_AUTO, port_filter, CTLFLAG_RW,
324 &siftr_port_filter, 0,
325 "enable packet filter on a TCP port");
328 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW,
329 &siftr_binary_log, 0,
330 "write log files in binary instead of ascii");
333 /* Begin functions. */
336 siftr_process_pkt(struct pkt_node * pkt_node)
338 struct flow_hash_node *hash_node;
339 struct listhead *counter_list;
340 struct siftr_stats *ss;
342 uint8_t key[FLOW_KEY_LEN];
343 uint8_t found_match, key_offset;
351 * Create the key that will be used to create a hash index
352 * into our hash table. Our key consists of:
353 * ipversion, localip, localport, foreignip, foreignport
355 key[0] = pkt_node->ipver;
356 memcpy(key + key_offset, &pkt_node->ip_laddr,
357 sizeof(pkt_node->ip_laddr));
358 key_offset += sizeof(pkt_node->ip_laddr);
359 memcpy(key + key_offset, &pkt_node->tcp_localport,
360 sizeof(pkt_node->tcp_localport));
361 key_offset += sizeof(pkt_node->tcp_localport);
362 memcpy(key + key_offset, &pkt_node->ip_faddr,
363 sizeof(pkt_node->ip_faddr));
364 key_offset += sizeof(pkt_node->ip_faddr);
365 memcpy(key + key_offset, &pkt_node->tcp_foreignport,
366 sizeof(pkt_node->tcp_foreignport));
368 counter_list = counter_hash +
369 (hash32_buf(key, sizeof(key), 0) & siftr_hashmask);
372 * If the list is not empty i.e. the hash index has
373 * been used by another flow previously.
375 if (LIST_FIRST(counter_list) != NULL) {
377 * Loop through the hash nodes in the list.
378 * There should normally only be 1 hash node in the list,
379 * except if there have been collisions at the hash index
380 * computed by hash32_buf().
382 LIST_FOREACH(hash_node, counter_list, nodes) {
384 * Check if the key for the pkt we are currently
385 * processing is the same as the key stored in the
386 * hash node we are currently processing.
387 * If they are the same, then we've found the
388 * hash node that stores the counter for the flow
389 * the pkt belongs to.
391 if (memcmp(hash_node->key, key, sizeof(key)) == 0) {
398 /* If this flow hash hasn't been seen before or we have a collision. */
399 if (hash_node == NULL || !found_match) {
400 /* Create a new hash node to store the flow's counter. */
401 hash_node = malloc(sizeof(struct flow_hash_node),
402 M_SIFTR_HASHNODE, M_WAITOK);
404 if (hash_node != NULL) {
405 /* Initialise our new hash node list entry. */
406 hash_node->counter = 0;
407 memcpy(hash_node->key, key, sizeof(key));
408 LIST_INSERT_HEAD(counter_list, hash_node, nodes);
411 if (pkt_node->direction == DIR_IN)
412 ss->nskip_in_malloc++;
414 ss->nskip_out_malloc++;
418 } else if (siftr_pkts_per_log > 1) {
420 * Taking the remainder of the counter divided
421 * by the current value of siftr_pkts_per_log
422 * and storing that in counter provides a neat
423 * way to modulate the frequency of log
424 * messages being written to the log file.
426 hash_node->counter = (hash_node->counter + 1) %
430 * If we have not seen enough packets since the last time
431 * we wrote a log message for this connection, return.
433 if (hash_node->counter > 0)
437 log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK);
440 return; /* Should only happen if the ALQ is shutting down. */
443 pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]);
444 pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]);
446 if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */
447 pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]);
448 pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]);
449 pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]);
450 pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]);
451 pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]);
452 pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]);
454 /* Construct an IPv6 log message. */
455 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
457 "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:"
458 "%x:%x:%x:%x:%x,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,"
459 "%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n",
460 direction[pkt_node->direction],
462 pkt_node->tval.tv_sec,
463 pkt_node->tval.tv_usec,
464 UPPER_SHORT(pkt_node->ip_laddr[0]),
465 LOWER_SHORT(pkt_node->ip_laddr[0]),
466 UPPER_SHORT(pkt_node->ip_laddr[1]),
467 LOWER_SHORT(pkt_node->ip_laddr[1]),
468 UPPER_SHORT(pkt_node->ip_laddr[2]),
469 LOWER_SHORT(pkt_node->ip_laddr[2]),
470 UPPER_SHORT(pkt_node->ip_laddr[3]),
471 LOWER_SHORT(pkt_node->ip_laddr[3]),
472 ntohs(pkt_node->tcp_localport),
473 UPPER_SHORT(pkt_node->ip_faddr[0]),
474 LOWER_SHORT(pkt_node->ip_faddr[0]),
475 UPPER_SHORT(pkt_node->ip_faddr[1]),
476 LOWER_SHORT(pkt_node->ip_faddr[1]),
477 UPPER_SHORT(pkt_node->ip_faddr[2]),
478 LOWER_SHORT(pkt_node->ip_faddr[2]),
479 UPPER_SHORT(pkt_node->ip_faddr[3]),
480 LOWER_SHORT(pkt_node->ip_faddr[3]),
481 ntohs(pkt_node->tcp_foreignport),
482 pkt_node->snd_ssthresh,
489 pkt_node->conn_state,
490 pkt_node->max_seg_size,
491 pkt_node->smoothed_rtt,
492 pkt_node->sack_enabled,
494 pkt_node->rxt_length,
495 pkt_node->snd_buf_hiwater,
496 pkt_node->snd_buf_cc,
497 pkt_node->rcv_buf_hiwater,
498 pkt_node->rcv_buf_cc,
499 pkt_node->sent_inflight_bytes,
503 } else { /* IPv4 packet */
504 pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]);
505 pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]);
506 pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]);
507 pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]);
508 pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]);
509 pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]);
510 pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]);
511 pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]);
512 #endif /* SIFTR_IPV6 */
514 /* Construct an IPv4 log message. */
515 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
517 "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%u,%u,"
518 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n",
519 direction[pkt_node->direction],
521 (intmax_t)pkt_node->tval.tv_sec,
522 pkt_node->tval.tv_usec,
523 pkt_node->ip_laddr[0],
524 pkt_node->ip_laddr[1],
525 pkt_node->ip_laddr[2],
526 pkt_node->ip_laddr[3],
527 ntohs(pkt_node->tcp_localport),
528 pkt_node->ip_faddr[0],
529 pkt_node->ip_faddr[1],
530 pkt_node->ip_faddr[2],
531 pkt_node->ip_faddr[3],
532 ntohs(pkt_node->tcp_foreignport),
533 pkt_node->snd_ssthresh,
540 pkt_node->conn_state,
541 pkt_node->max_seg_size,
542 pkt_node->smoothed_rtt,
543 pkt_node->sack_enabled,
545 pkt_node->rxt_length,
546 pkt_node->snd_buf_hiwater,
547 pkt_node->snd_buf_cc,
548 pkt_node->rcv_buf_hiwater,
549 pkt_node->rcv_buf_cc,
550 pkt_node->sent_inflight_bytes,
558 alq_post_flags(siftr_alq, log_buf, 0);
562 siftr_pkt_manager_thread(void *arg)
564 STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue =
565 STAILQ_HEAD_INITIALIZER(tmp_pkt_queue);
566 struct pkt_node *pkt_node, *pkt_node_temp;
571 mtx_lock(&siftr_pkt_mgr_mtx);
573 /* draining == 0 when queue has been flushed and it's safe to exit. */
576 * Sleep until we are signalled to wake because thread has
577 * been told to exit or until 1 tick has passed.
579 mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait",
582 /* Gain exclusive access to the pkt_node queue. */
583 mtx_lock(&siftr_pkt_queue_mtx);
586 * Move pkt_queue to tmp_pkt_queue, which leaves
587 * pkt_queue empty and ready to receive more pkt_nodes.
589 STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue);
592 * We've finished making changes to the list. Unlock it
593 * so the pfil hooks can continue queuing pkt_nodes.
595 mtx_unlock(&siftr_pkt_queue_mtx);
598 * We can't hold a mutex whilst calling siftr_process_pkt
599 * because ALQ might sleep waiting for buffer space.
601 mtx_unlock(&siftr_pkt_mgr_mtx);
603 /* Flush all pkt_nodes to the log file. */
604 STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes,
606 siftr_process_pkt(pkt_node);
607 STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes);
608 free(pkt_node, M_SIFTR_PKTNODE);
611 KASSERT(STAILQ_EMPTY(&tmp_pkt_queue),
612 ("SIFTR tmp_pkt_queue not empty after flush"));
614 mtx_lock(&siftr_pkt_mgr_mtx);
617 * If siftr_exit_pkt_manager_thread gets set during the window
618 * where we are draining the tmp_pkt_queue above, there might
619 * still be pkts in pkt_queue that need to be drained.
620 * Allow one further iteration to occur after
621 * siftr_exit_pkt_manager_thread has been set to ensure
622 * pkt_queue is completely empty before we kill the thread.
624 * siftr_exit_pkt_manager_thread is set only after the pfil
625 * hooks have been removed, so only 1 extra iteration
626 * is needed to drain the queue.
628 if (siftr_exit_pkt_manager_thread)
632 mtx_unlock(&siftr_pkt_mgr_mtx);
634 /* Calls wakeup on this thread's struct thread ptr. */
639 hash_pkt(struct mbuf *m, uint32_t offset)
645 while (m != NULL && offset > m->m_len) {
647 * The IP packet payload does not start in this mbuf, so
648 * need to figure out which mbuf it starts in and what offset
649 * into the mbuf's data region the payload starts at.
656 /* Ensure there is data in the mbuf */
657 if ((m->m_len - offset) > 0)
658 hash = hash32_buf(m->m_data + offset,
659 m->m_len - offset, hash);
669 * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that
670 * it's a reinjected packet and return. If it doesn't, tag the mbuf and return.
671 * Return value >0 means the caller should skip processing this mbuf.
674 siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss)
676 if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL)
679 ss->nskip_in_dejavu++;
681 ss->nskip_out_dejavu++;
685 struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR,
686 PACKET_TAG_SIFTR, 0, M_NOWAIT);
689 ss->nskip_in_malloc++;
691 ss->nskip_out_malloc++;
696 m_tag_prepend(m, tag);
703 * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL
706 static inline struct inpcb *
707 siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport,
708 uint16_t dport, int dir, struct siftr_stats *ss)
712 /* We need the tcbinfo lock. */
713 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
716 inp = (ipver == INP_IPV4 ?
717 in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst,
718 dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
721 in6_pcblookup(&V_tcbinfo,
722 &((struct ip6_hdr *)ip)->ip6_src, sport,
723 &((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB,
731 inp = (ipver == INP_IPV4 ?
732 in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src,
733 sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
736 in6_pcblookup(&V_tcbinfo,
737 &((struct ip6_hdr *)ip)->ip6_dst, dport,
738 &((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB,
745 /* If we can't find the inpcb, bail. */
748 ss->nskip_in_inpcb++;
750 ss->nskip_out_inpcb++;
757 siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp,
758 int ipver, int dir, int inp_locally_locked)
761 if (ipver == INP_IPV4) {
762 pn->ip_laddr[3] = inp->inp_laddr.s_addr;
763 pn->ip_faddr[3] = inp->inp_faddr.s_addr;
765 *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr;
766 *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr;
770 pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0];
771 pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1];
772 pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2];
773 pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3];
774 pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0];
775 pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1];
776 pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2];
777 pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3];
780 pn->tcp_localport = inp->inp_lport;
781 pn->tcp_foreignport = inp->inp_fport;
782 pn->snd_cwnd = tp->snd_cwnd;
783 pn->snd_wnd = tp->snd_wnd;
784 pn->rcv_wnd = tp->rcv_wnd;
785 pn->t_flags2 = tp->t_flags2;
786 pn->snd_ssthresh = tp->snd_ssthresh;
787 pn->snd_scale = tp->snd_scale;
788 pn->rcv_scale = tp->rcv_scale;
789 pn->conn_state = tp->t_state;
790 pn->max_seg_size = tp->t_maxseg;
791 pn->smoothed_rtt = tp->t_srtt;
792 pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0;
793 pn->flags = tp->t_flags;
794 pn->rxt_length = tp->t_rxtcur;
795 pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat;
796 pn->snd_buf_cc = sbused(&inp->inp_socket->so_snd);
797 pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat;
798 pn->rcv_buf_cc = sbused(&inp->inp_socket->so_rcv);
799 pn->sent_inflight_bytes = tp->snd_max - tp->snd_una;
800 pn->t_segqlen = tp->t_segqlen;
801 pn->flowid = inp->inp_flowid;
802 pn->flowtype = inp->inp_flowtype;
804 /* We've finished accessing the tcb so release the lock. */
805 if (inp_locally_locked)
809 pn->direction = (dir == PFIL_IN ? DIR_IN : DIR_OUT);
812 * Significantly more accurate than using getmicrotime(), but slower!
813 * Gives true microsecond resolution at the expense of a hit to
814 * maximum pps throughput processing when SIFTR is loaded and enabled.
816 microtime(&pn->tval);
817 TCP_PROBE1(siftr, &pn);
822 * pfil hook that is called for each IPv4 packet making its way through the
823 * stack in either direction.
824 * The pfil subsystem holds a non-sleepable mutex somewhere when
825 * calling our hook function, so we can't sleep at all.
826 * It's very important to use the M_NOWAIT flag with all function calls
827 * that support it so that they won't sleep, otherwise you get a panic.
830 siftr_chkpkt(struct mbuf **m, struct ifnet *ifp, int flags,
831 void *ruleset __unused, struct inpcb *inp)
837 struct siftr_stats *ss;
839 int inp_locally_locked, dir;
841 inp_locally_locked = 0;
842 dir = PFIL_DIR(flags);
846 * m_pullup is not required here because ip_{input|output}
847 * already do the heavy lifting for us.
850 ip = mtod(*m, struct ip *);
852 /* Only continue processing if the packet is TCP. */
853 if (ip->ip_p != IPPROTO_TCP)
857 * Create a tcphdr struct starting at the correct offset
858 * in the IP packet. ip->ip_hl gives the ip header length
859 * in 4-byte words, so multiply it to get the size in bytes.
861 ip_hl = (ip->ip_hl << 2);
862 th = (struct tcphdr *)((caddr_t)ip + ip_hl);
865 * Only pkts selected by the tcp port filter
866 * can be inserted into the pkt_queue
868 if ((siftr_port_filter != 0) &&
869 (siftr_port_filter != ntohs(th->th_sport)) &&
870 (siftr_port_filter != ntohs(th->th_dport))) {
875 * If a kernel subsystem reinjects packets into the stack, our pfil
876 * hook will be called multiple times for the same packet.
877 * Make sure we only process unique packets.
879 if (siftr_chkreinject(*m, dir, ss))
888 * If the pfil hooks don't provide a pointer to the
889 * inpcb, we need to find it ourselves and lock it.
892 /* Find the corresponding inpcb for this pkt. */
893 inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport,
894 th->th_dport, dir, ss);
899 inp_locally_locked = 1;
902 INP_LOCK_ASSERT(inp);
904 /* Find the TCP control block that corresponds with this packet */
908 * If we can't find the TCP control block (happens occasionaly for a
909 * packet sent during the shutdown phase of a TCP connection), bail
913 ss->nskip_in_tcpcb++;
915 ss->nskip_out_tcpcb++;
921 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
925 ss->nskip_in_malloc++;
927 ss->nskip_out_malloc++;
932 siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked);
934 if (siftr_generate_hashes) {
935 if ((*m)->m_pkthdr.csum_flags & CSUM_TCP) {
937 * For outbound packets, the TCP checksum isn't
938 * calculated yet. This is a problem for our packet
939 * hashing as the receiver will calc a different hash
940 * to ours if we don't include the correct TCP checksum
941 * in the bytes being hashed. To work around this
942 * problem, we manually calc the TCP checksum here in
943 * software. We unset the CSUM_TCP flag so the lower
944 * layers don't recalc it.
946 (*m)->m_pkthdr.csum_flags &= ~CSUM_TCP;
949 * Calculate the TCP checksum in software and assign
950 * to correct TCP header field, which will follow the
951 * packet mbuf down the stack. The trick here is that
952 * tcp_output() sets th->th_sum to the checksum of the
953 * pseudo header for us already. Because of the nature
954 * of the checksumming algorithm, we can sum over the
955 * entire IP payload (i.e. TCP header and data), which
956 * will include the already calculated pseduo header
957 * checksum, thus giving us the complete TCP checksum.
959 * To put it in simple terms, if checksum(1,2,3,4)=10,
960 * then checksum(1,2,3,4,5) == checksum(10,5).
961 * This property is what allows us to "cheat" and
962 * checksum only the IP payload which has the TCP
963 * th_sum field populated with the pseudo header's
964 * checksum, and not need to futz around checksumming
965 * pseudo header bytes and TCP header/data in one hit.
966 * Refer to RFC 1071 for more info.
968 * NB: in_cksum_skip(struct mbuf *m, int len, int skip)
969 * in_cksum_skip 2nd argument is NOT the number of
970 * bytes to read from the mbuf at "skip" bytes offset
971 * from the start of the mbuf (very counter intuitive!).
972 * The number of bytes to read is calculated internally
973 * by the function as len-skip i.e. to sum over the IP
974 * payload (TCP header + data) bytes, it is INCORRECT
975 * to call the function like this:
976 * in_cksum_skip(at, ip->ip_len - offset, offset)
977 * Rather, it should be called like this:
978 * in_cksum_skip(at, ip->ip_len, offset)
979 * which means read "ip->ip_len - offset" bytes from
980 * the mbuf cluster "at" at offset "offset" bytes from
981 * the beginning of the "at" mbuf's data pointer.
983 th->th_sum = in_cksum_skip(*m, ntohs(ip->ip_len),
988 * XXX: Having to calculate the checksum in software and then
989 * hash over all bytes is really inefficient. Would be nice to
990 * find a way to create the hash and checksum in the same pass
993 pn->hash = hash_pkt(*m, ip_hl);
996 mtx_lock(&siftr_pkt_queue_mtx);
997 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
998 mtx_unlock(&siftr_pkt_queue_mtx);
1002 if (inp_locally_locked)
1010 static pfil_return_t
1011 siftr_chkpkt6(struct mbuf **m, struct ifnet *ifp, int flags,
1012 void *ruleset __unused, struct inpcb *inp)
1014 struct pkt_node *pn;
1015 struct ip6_hdr *ip6;
1018 struct siftr_stats *ss;
1019 unsigned int ip6_hl;
1020 int inp_locally_locked, dir;
1022 inp_locally_locked = 0;
1023 dir = PFIL_DIR(flags);
1027 * m_pullup is not required here because ip6_{input|output}
1028 * already do the heavy lifting for us.
1031 ip6 = mtod(*m, struct ip6_hdr *);
1034 * Only continue processing if the packet is TCP
1035 * XXX: We should follow the next header fields
1036 * as shown on Pg 6 RFC 2460, but right now we'll
1037 * only check pkts that have no extension headers.
1039 if (ip6->ip6_nxt != IPPROTO_TCP)
1043 * Create a tcphdr struct starting at the correct offset
1044 * in the ipv6 packet.
1046 ip6_hl = sizeof(struct ip6_hdr);
1047 th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl);
1050 * Only pkts selected by the tcp port filter
1051 * can be inserted into the pkt_queue
1053 if ((siftr_port_filter != 0) &&
1054 (siftr_port_filter != ntohs(th->th_sport)) &&
1055 (siftr_port_filter != ntohs(th->th_dport))) {
1060 * If a kernel subsystem reinjects packets into the stack, our pfil
1061 * hook will be called multiple times for the same packet.
1062 * Make sure we only process unique packets.
1064 if (siftr_chkreinject(*m, dir, ss))
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), bail
1096 ss->nskip_in_tcpcb++;
1098 ss->nskip_out_tcpcb++;
1104 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
1108 ss->nskip_in_malloc++;
1110 ss->nskip_out_malloc++;
1115 siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked);
1117 /* XXX: Figure out how to generate hashes for IPv6 packets. */
1119 mtx_lock(&siftr_pkt_queue_mtx);
1120 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
1121 mtx_unlock(&siftr_pkt_queue_mtx);
1125 if (inp_locally_locked)
1131 #endif /* #ifdef SIFTR_IPV6 */
1133 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet_hook);
1134 #define V_siftr_inet_hook VNET(siftr_inet_hook)
1136 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet6_hook);
1137 #define V_siftr_inet6_hook VNET(siftr_inet6_hook)
1140 siftr_pfil(int action)
1142 struct pfil_hook_args pha;
1143 struct pfil_link_args pla;
1145 pha.pa_version = PFIL_VERSION;
1146 pha.pa_flags = PFIL_IN | PFIL_OUT;
1147 pha.pa_modname = "siftr";
1148 pha.pa_ruleset = NULL;
1149 pha.pa_rulname = "default";
1151 pla.pa_version = PFIL_VERSION;
1152 pla.pa_flags = PFIL_IN | PFIL_OUT |
1153 PFIL_HEADPTR | PFIL_HOOKPTR;
1155 VNET_ITERATOR_DECL(vnet_iter);
1158 VNET_FOREACH(vnet_iter) {
1159 CURVNET_SET(vnet_iter);
1161 if (action == HOOK) {
1162 pha.pa_func = siftr_chkpkt;
1163 pha.pa_type = PFIL_TYPE_IP4;
1164 V_siftr_inet_hook = pfil_add_hook(&pha);
1165 pla.pa_hook = V_siftr_inet_hook;
1166 pla.pa_head = V_inet_pfil_head;
1167 (void)pfil_link(&pla);
1169 pha.pa_func = siftr_chkpkt6;
1170 pha.pa_type = PFIL_TYPE_IP6;
1171 V_siftr_inet6_hook = pfil_add_hook(&pha);
1172 pla.pa_hook = V_siftr_inet6_hook;
1173 pla.pa_head = V_inet6_pfil_head;
1174 (void)pfil_link(&pla);
1176 } else if (action == UNHOOK) {
1177 pfil_remove_hook(V_siftr_inet_hook);
1179 pfil_remove_hook(V_siftr_inet6_hook);
1184 VNET_LIST_RUNLOCK();
1190 siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS)
1192 struct alq *new_alq;
1195 error = sysctl_handle_string(oidp, arg1, arg2, req);
1197 /* Check for error or same filename */
1198 if (error != 0 || req->newptr == NULL ||
1199 strncmp(siftr_logfile, arg1, arg2) == 0)
1202 /* Filname changed */
1203 error = alq_open(&new_alq, arg1, curthread->td_ucred,
1204 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1209 * If disabled, siftr_alq == NULL so we simply close
1210 * the alq as we've proved it can be opened.
1211 * If enabled, close the existing alq and switch the old
1214 if (siftr_alq == NULL) {
1217 alq_close(siftr_alq);
1218 siftr_alq = new_alq;
1221 /* Update filename upon success */
1222 strlcpy(siftr_logfile, arg1, arg2);
1228 siftr_manage_ops(uint8_t action)
1230 struct siftr_stats totalss;
1231 struct timeval tval;
1232 struct flow_hash_node *counter, *tmp_counter;
1234 int i, key_index, error;
1235 uint32_t bytes_to_write, total_skipped_pkts;
1236 uint16_t lport, fport;
1237 uint8_t *key, ipver __unused;
1248 total_skipped_pkts = 0;
1250 /* Init an autosizing sbuf that initially holds 200 chars. */
1251 if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL)
1254 if (action == SIFTR_ENABLE && siftr_pkt_manager_thr == NULL) {
1257 * XXX: We should abort if alq_open fails!
1259 alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred,
1260 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1262 STAILQ_INIT(&pkt_queue);
1266 siftr_exit_pkt_manager_thread = 0;
1268 kthread_add(&siftr_pkt_manager_thread, NULL, NULL,
1269 &siftr_pkt_manager_thr, RFNOWAIT, 0,
1270 "siftr_pkt_manager_thr");
1277 "enable_time_secs=%jd\tenable_time_usecs=%06ld\t"
1278 "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t"
1279 "sysver=%u\tipmode=%u\n",
1280 (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz,
1281 TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE);
1284 alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK);
1286 } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) {
1288 * Remove the pfil hook functions. All threads currently in
1289 * the hook functions are allowed to exit before siftr_pfil()
1294 /* This will block until the pkt manager thread unlocks it. */
1295 mtx_lock(&siftr_pkt_mgr_mtx);
1297 /* Tell the pkt manager thread that it should exit now. */
1298 siftr_exit_pkt_manager_thread = 1;
1301 * Wake the pkt_manager thread so it realises that
1302 * siftr_exit_pkt_manager_thread == 1 and exits gracefully.
1303 * The wakeup won't be delivered until we unlock
1304 * siftr_pkt_mgr_mtx so this isn't racy.
1306 wakeup(&wait_for_pkt);
1308 /* Wait for the pkt_manager thread to exit. */
1309 mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT,
1312 siftr_pkt_manager_thr = NULL;
1313 mtx_unlock(&siftr_pkt_mgr_mtx);
1315 totalss.n_in = DPCPU_VARSUM(ss, n_in);
1316 totalss.n_out = DPCPU_VARSUM(ss, n_out);
1317 totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc);
1318 totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc);
1319 totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx);
1320 totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx);
1321 totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb);
1322 totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb);
1323 totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb);
1324 totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb);
1326 total_skipped_pkts = totalss.nskip_in_malloc +
1327 totalss.nskip_out_malloc + totalss.nskip_in_mtx +
1328 totalss.nskip_out_mtx + totalss.nskip_in_tcpcb +
1329 totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb +
1330 totalss.nskip_out_inpcb;
1335 "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t"
1336 "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t"
1337 "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t"
1338 "num_outbound_skipped_pkts_malloc=%u\t"
1339 "num_inbound_skipped_pkts_mtx=%u\t"
1340 "num_outbound_skipped_pkts_mtx=%u\t"
1341 "num_inbound_skipped_pkts_tcpcb=%u\t"
1342 "num_outbound_skipped_pkts_tcpcb=%u\t"
1343 "num_inbound_skipped_pkts_inpcb=%u\t"
1344 "num_outbound_skipped_pkts_inpcb=%u\t"
1345 "total_skipped_tcp_pkts=%u\tflow_list=",
1346 (intmax_t)tval.tv_sec,
1348 (uintmax_t)totalss.n_in,
1349 (uintmax_t)totalss.n_out,
1350 (uintmax_t)(totalss.n_in + totalss.n_out),
1351 totalss.nskip_in_malloc,
1352 totalss.nskip_out_malloc,
1353 totalss.nskip_in_mtx,
1354 totalss.nskip_out_mtx,
1355 totalss.nskip_in_tcpcb,
1356 totalss.nskip_out_tcpcb,
1357 totalss.nskip_in_inpcb,
1358 totalss.nskip_out_inpcb,
1359 total_skipped_pkts);
1362 * Iterate over the flow hash, printing a summary of each
1363 * flow seen and freeing any malloc'd memory.
1364 * The hash consists of an array of LISTs (man 3 queue).
1366 for (i = 0; i <= siftr_hashmask; i++) {
1367 LIST_FOREACH_SAFE(counter, counter_hash + i, nodes,
1374 memcpy(laddr, key + key_index, sizeof(laddr));
1375 key_index += sizeof(laddr);
1376 memcpy(&lport, key + key_index, sizeof(lport));
1377 key_index += sizeof(lport);
1378 memcpy(faddr, key + key_index, sizeof(faddr));
1379 key_index += sizeof(faddr);
1380 memcpy(&fport, key + key_index, sizeof(fport));
1383 laddr[3] = ntohl(laddr[3]);
1384 faddr[3] = ntohl(faddr[3]);
1386 if (ipver == INP_IPV6) {
1387 laddr[0] = ntohl(laddr[0]);
1388 laddr[1] = ntohl(laddr[1]);
1389 laddr[2] = ntohl(laddr[2]);
1390 faddr[0] = ntohl(faddr[0]);
1391 faddr[1] = ntohl(faddr[1]);
1392 faddr[2] = ntohl(faddr[2]);
1395 "%x:%x:%x:%x:%x:%x:%x:%x;%u-"
1396 "%x:%x:%x:%x:%x:%x:%x:%x;%u,",
1397 UPPER_SHORT(laddr[0]),
1398 LOWER_SHORT(laddr[0]),
1399 UPPER_SHORT(laddr[1]),
1400 LOWER_SHORT(laddr[1]),
1401 UPPER_SHORT(laddr[2]),
1402 LOWER_SHORT(laddr[2]),
1403 UPPER_SHORT(laddr[3]),
1404 LOWER_SHORT(laddr[3]),
1406 UPPER_SHORT(faddr[0]),
1407 LOWER_SHORT(faddr[0]),
1408 UPPER_SHORT(faddr[1]),
1409 LOWER_SHORT(faddr[1]),
1410 UPPER_SHORT(faddr[2]),
1411 LOWER_SHORT(faddr[2]),
1412 UPPER_SHORT(faddr[3]),
1413 LOWER_SHORT(faddr[3]),
1416 laddr[0] = FIRST_OCTET(laddr[3]);
1417 laddr[1] = SECOND_OCTET(laddr[3]);
1418 laddr[2] = THIRD_OCTET(laddr[3]);
1419 laddr[3] = FOURTH_OCTET(laddr[3]);
1420 faddr[0] = FIRST_OCTET(faddr[3]);
1421 faddr[1] = SECOND_OCTET(faddr[3]);
1422 faddr[2] = THIRD_OCTET(faddr[3]);
1423 faddr[3] = FOURTH_OCTET(faddr[3]);
1426 "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,",
1441 free(counter, M_SIFTR_HASHNODE);
1444 LIST_INIT(counter_hash + i);
1447 sbuf_printf(s, "\n");
1452 bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i);
1453 alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK);
1454 i += bytes_to_write;
1455 } while (i < sbuf_len(s));
1457 alq_close(siftr_alq);
1465 * XXX: Should be using ret to check if any functions fail
1466 * and set error appropriately
1473 siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS)
1478 new = siftr_enabled;
1479 error = sysctl_handle_int(oidp, &new, 0, req);
1480 if (error == 0 && req->newptr != NULL) {
1483 else if (new != siftr_enabled) {
1484 if ((error = siftr_manage_ops(new)) == 0) {
1485 siftr_enabled = new;
1487 siftr_manage_ops(SIFTR_DISABLE);
1496 siftr_shutdown_handler(void *arg)
1498 if (siftr_enabled == 1) {
1499 siftr_manage_ops(SIFTR_DISABLE);
1504 * Module is being unloaded or machine is shutting down. Take care of cleanup.
1510 siftr_manage_ops(SIFTR_DISABLE);
1511 hashdestroy(counter_hash, M_SIFTR, siftr_hashmask);
1512 mtx_destroy(&siftr_pkt_queue_mtx);
1513 mtx_destroy(&siftr_pkt_mgr_mtx);
1519 * Module has just been loaded into the kernel.
1524 EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL,
1525 SHUTDOWN_PRI_FIRST);
1527 /* Initialise our flow counter hash table. */
1528 counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR,
1531 mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF);
1532 mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF);
1534 /* Print message to the user's current terminal. */
1535 uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n"
1536 " http://caia.swin.edu.au/urp/newtcp\n\n",
1543 * This is the function that is called to load and unload the module.
1544 * When the module is loaded, this function is called once with
1545 * "what" == MOD_LOAD
1546 * When the module is unloaded, this function is called twice with
1547 * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second
1548 * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command,
1549 * this function is called once with "what" = MOD_SHUTDOWN
1550 * When the system is shut down, the handler isn't called until the very end
1551 * of the shutdown sequence i.e. after the disks have been synced.
1554 siftr_load_handler(module_t mod, int what, void *arg)
1565 ret = deinit_siftr();
1580 static moduledata_t siftr_mod = {
1582 .evhand = siftr_load_handler,
1586 * Param 1: name of the kernel module
1587 * Param 2: moduledata_t struct containing info about the kernel module
1588 * and the execution entry point for the module
1589 * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h
1590 * Defines the module initialisation order
1591 * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h
1592 * Defines the initialisation order of this kld relative to others
1593 * within the same subsystem as defined by param 3
1595 DECLARE_MODULE(siftr, siftr_mod, SI_SUB_LAST, SI_ORDER_ANY);
1596 MODULE_DEPEND(siftr, alq, 1, 1, 1);
1597 MODULE_VERSION(siftr, MODVERSION);