2 * Copyright (c) 2007-2009
3 * Swinburne University of Technology, Melbourne, Australia.
4 * Copyright (c) 2009-2010, The FreeBSD Foundation
7 * Portions of this software were developed at the Centre for Advanced
8 * Internet Architectures, Swinburne University of Technology, Melbourne,
9 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /******************************************************
34 * Statistical Information For TCP Research (SIFTR)
36 * A FreeBSD kernel module that adds very basic intrumentation to the
37 * TCP stack, allowing internal stats to be recorded to a log file
38 * for experimental, debugging and performance analysis purposes.
40 * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst
41 * working on the NewTCP research project at Swinburne University of
42 * Technology's Centre for Advanced Internet Architectures, Melbourne,
43 * Australia, which was made possible in part by a grant from the Cisco
44 * University Research Program Fund at Community Foundation Silicon Valley.
45 * More details are available at:
46 * http://caia.swin.edu.au/urp/newtcp/
48 * Work on SIFTR v1.2.x was sponsored by the FreeBSD Foundation as part of
49 * the "Enhancing the FreeBSD TCP Implementation" project 2008-2009.
50 * More details are available at:
51 * http://www.freebsdfoundation.org/
52 * http://caia.swin.edu.au/freebsd/etcp09/
54 * Lawrence Stewart is the current maintainer, and all contact regarding
55 * SIFTR should be directed to him via email: lastewart@swin.edu.au
57 * Initial release date: June 2007
58 * Most recent update: September 2010
59 ******************************************************/
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
66 #include <sys/errno.h>
67 #include <sys/eventhandler.h>
69 #include <sys/kernel.h>
70 #include <sys/kthread.h>
73 #include <sys/module.h>
74 #include <sys/mutex.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/sysctl.h>
82 #include <sys/unistd.h>
85 #include <net/if_var.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/tcp_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/in6_pcb.h>
98 #endif /* SIFTR_IPV6 */
100 #include <machine/in_cksum.h>
103 * Three digit version number refers to X.Y.Z where:
104 * X is the major version number
105 * Y is bumped to mark backwards incompatible changes
106 * Z is bumped to mark backwards compatible changes
109 #define V_BACKBREAK 2
110 #define V_BACKCOMPAT 4
111 #define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT))
112 #define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \
113 __XSTRING(V_BACKCOMPAT)
117 #define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536
118 #define SYS_NAME "FreeBSD"
119 #define PACKET_TAG_SIFTR 100
120 #define PACKET_COOKIE_SIFTR 21749576
121 #define SIFTR_LOG_FILE_MODE 0644
122 #define SIFTR_DISABLE 0
123 #define SIFTR_ENABLE 1
126 * Hard upper limit on the length of log messages. Bump this up if you add new
127 * data fields such that the line length could exceed the below value.
129 #define MAX_LOG_MSG_LEN 200
130 /* XXX: Make this a sysctl tunable. */
131 #define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN)
134 * 1 byte for IP version
135 * IPv4: src/dst IP (4+4) + src/dst port (2+2) = 12 bytes
136 * IPv6: src/dst IP (16+16) + src/dst port (2+2) = 36 bytes
139 #define FLOW_KEY_LEN 37
141 #define FLOW_KEY_LEN 13
145 #define SIFTR_IPMODE 6
147 #define SIFTR_IPMODE 4
151 #define CAST_PTR_INT(X) (*((int*)(X)))
153 #define UPPER_SHORT(X) (((X) & 0xFFFF0000) >> 16)
154 #define LOWER_SHORT(X) ((X) & 0x0000FFFF)
156 #define FIRST_OCTET(X) (((X) & 0xFF000000) >> 24)
157 #define SECOND_OCTET(X) (((X) & 0x00FF0000) >> 16)
158 #define THIRD_OCTET(X) (((X) & 0x0000FF00) >> 8)
159 #define FOURTH_OCTET(X) ((X) & 0x000000FF)
161 static MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR");
162 static MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode",
163 "SIFTR pkt_node struct");
164 static MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode",
165 "SIFTR flow_hash_node struct");
167 /* Used as links in the pkt manager queue. */
169 /* Timestamp of pkt as noted in the pfil hook. */
171 /* Direction pkt is travelling; either PFIL_IN or PFIL_OUT. */
173 /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */
175 /* Hash of the pkt which triggered the log message. */
177 /* Local/foreign IP address. */
179 uint32_t ip_laddr[4];
180 uint32_t ip_faddr[4];
185 /* Local TCP port. */
186 uint16_t tcp_localport;
187 /* Foreign TCP port. */
188 uint16_t tcp_foreignport;
189 /* Congestion Window (bytes). */
191 /* Sending Window (bytes). */
193 /* Receive Window (bytes). */
195 /* Unused (was: Bandwidth Controlled Window (bytes)). */
197 /* Slow Start Threshold (bytes). */
199 /* Current state of the TCP FSM. */
201 /* Max Segment Size (bytes). */
204 * Smoothed RTT stored as found in the TCP control block
205 * in units of (TCP_RTT_SCALE*hz).
208 /* Is SACK enabled? */
210 /* Window scaling for snd window. */
212 /* Window scaling for recv window. */
214 /* TCP control block flags. */
216 /* Retransmit timeout length. */
218 /* Size of the TCP send buffer in bytes. */
219 u_int snd_buf_hiwater;
220 /* Current num bytes in the send socket buffer. */
222 /* Size of the TCP receive buffer in bytes. */
223 u_int rcv_buf_hiwater;
224 /* Current num bytes in the receive socket buffer. */
226 /* Number of bytes inflight that we are waiting on ACKs for. */
227 u_int sent_inflight_bytes;
228 /* Number of segments currently in the reassembly queue. */
230 /* Link to next pkt_node in the list. */
231 STAILQ_ENTRY(pkt_node) nodes;
234 struct flow_hash_node
237 uint8_t key[FLOW_KEY_LEN];
238 LIST_ENTRY(flow_hash_node) nodes;
243 /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */
246 /* # pkts skipped due to failed malloc calls. */
247 uint32_t nskip_in_malloc;
248 uint32_t nskip_out_malloc;
249 /* # pkts skipped due to failed mtx acquisition. */
250 uint32_t nskip_in_mtx;
251 uint32_t nskip_out_mtx;
252 /* # pkts skipped due to failed inpcb lookups. */
253 uint32_t nskip_in_inpcb;
254 uint32_t nskip_out_inpcb;
255 /* # pkts skipped due to failed tcpcb lookups. */
256 uint32_t nskip_in_tcpcb;
257 uint32_t nskip_out_tcpcb;
258 /* # pkts skipped due to stack reinjection. */
259 uint32_t nskip_in_dejavu;
260 uint32_t nskip_out_dejavu;
263 static DPCPU_DEFINE(struct siftr_stats, ss);
265 static volatile unsigned int siftr_exit_pkt_manager_thread = 0;
266 static unsigned int siftr_enabled = 0;
267 static unsigned int siftr_pkts_per_log = 1;
268 static unsigned int siftr_generate_hashes = 0;
269 /* static unsigned int siftr_binary_log = 0; */
270 static char siftr_logfile[PATH_MAX] = "/var/log/siftr.log";
271 static char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log";
272 static u_long siftr_hashmask;
273 STAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue);
274 LIST_HEAD(listhead, flow_hash_node) *counter_hash;
275 static int wait_for_pkt;
276 static struct alq *siftr_alq = NULL;
277 static struct mtx siftr_pkt_queue_mtx;
278 static struct mtx siftr_pkt_mgr_mtx;
279 static struct thread *siftr_pkt_manager_thr = NULL;
281 * pfil.h defines PFIL_IN as 1 and PFIL_OUT as 2,
282 * which we use as an index into this array.
284 static char direction[3] = {'\0', 'i','o'};
286 /* Required function prototypes. */
287 static int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS);
288 static int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS);
291 /* Declare the net.inet.siftr sysctl tree and populate it. */
293 SYSCTL_DECL(_net_inet_siftr);
295 SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW, NULL,
296 "siftr related settings");
298 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled, CTLTYPE_UINT|CTLFLAG_RW,
299 &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU",
300 "switch siftr module operations on/off");
302 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile, CTLTYPE_STRING|CTLFLAG_RW,
303 &siftr_logfile_shadow, sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler,
304 "A", "file to save siftr log messages to");
306 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW,
307 &siftr_pkts_per_log, 1,
308 "number of packets between generating a log message");
310 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW,
311 &siftr_generate_hashes, 0,
312 "enable packet hash generation");
315 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW,
316 &siftr_binary_log, 0,
317 "write log files in binary instead of ascii");
321 /* Begin functions. */
324 siftr_process_pkt(struct pkt_node * pkt_node)
326 struct flow_hash_node *hash_node;
327 struct listhead *counter_list;
328 struct siftr_stats *ss;
330 uint8_t key[FLOW_KEY_LEN];
331 uint8_t found_match, key_offset;
339 * Create the key that will be used to create a hash index
340 * into our hash table. Our key consists of:
341 * ipversion, localip, localport, foreignip, foreignport
343 key[0] = pkt_node->ipver;
344 memcpy(key + key_offset, &pkt_node->ip_laddr,
345 sizeof(pkt_node->ip_laddr));
346 key_offset += sizeof(pkt_node->ip_laddr);
347 memcpy(key + key_offset, &pkt_node->tcp_localport,
348 sizeof(pkt_node->tcp_localport));
349 key_offset += sizeof(pkt_node->tcp_localport);
350 memcpy(key + key_offset, &pkt_node->ip_faddr,
351 sizeof(pkt_node->ip_faddr));
352 key_offset += sizeof(pkt_node->ip_faddr);
353 memcpy(key + key_offset, &pkt_node->tcp_foreignport,
354 sizeof(pkt_node->tcp_foreignport));
356 counter_list = counter_hash +
357 (hash32_buf(key, sizeof(key), 0) & siftr_hashmask);
360 * If the list is not empty i.e. the hash index has
361 * been used by another flow previously.
363 if (LIST_FIRST(counter_list) != NULL) {
365 * Loop through the hash nodes in the list.
366 * There should normally only be 1 hash node in the list,
367 * except if there have been collisions at the hash index
368 * computed by hash32_buf().
370 LIST_FOREACH(hash_node, counter_list, nodes) {
372 * Check if the key for the pkt we are currently
373 * processing is the same as the key stored in the
374 * hash node we are currently processing.
375 * If they are the same, then we've found the
376 * hash node that stores the counter for the flow
377 * the pkt belongs to.
379 if (memcmp(hash_node->key, key, sizeof(key)) == 0) {
386 /* If this flow hash hasn't been seen before or we have a collision. */
387 if (hash_node == NULL || !found_match) {
388 /* Create a new hash node to store the flow's counter. */
389 hash_node = malloc(sizeof(struct flow_hash_node),
390 M_SIFTR_HASHNODE, M_WAITOK);
392 if (hash_node != NULL) {
393 /* Initialise our new hash node list entry. */
394 hash_node->counter = 0;
395 memcpy(hash_node->key, key, sizeof(key));
396 LIST_INSERT_HEAD(counter_list, hash_node, nodes);
399 if (pkt_node->direction == PFIL_IN)
400 ss->nskip_in_malloc++;
402 ss->nskip_out_malloc++;
406 } else if (siftr_pkts_per_log > 1) {
408 * Taking the remainder of the counter divided
409 * by the current value of siftr_pkts_per_log
410 * and storing that in counter provides a neat
411 * way to modulate the frequency of log
412 * messages being written to the log file.
414 hash_node->counter = (hash_node->counter + 1) %
418 * If we have not seen enough packets since the last time
419 * we wrote a log message for this connection, return.
421 if (hash_node->counter > 0)
425 log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK);
428 return; /* Should only happen if the ALQ is shutting down. */
431 pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]);
432 pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]);
434 if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */
435 pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]);
436 pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]);
437 pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]);
438 pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]);
439 pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]);
440 pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]);
442 /* Construct an IPv6 log message. */
443 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
445 "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:"
446 "%x:%x:%x:%x:%x,%u,%ld,%ld,%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,"
447 "%u,%d,%u,%u,%u,%u,%u,%u\n",
448 direction[pkt_node->direction],
450 pkt_node->tval.tv_sec,
451 pkt_node->tval.tv_usec,
452 UPPER_SHORT(pkt_node->ip_laddr[0]),
453 LOWER_SHORT(pkt_node->ip_laddr[0]),
454 UPPER_SHORT(pkt_node->ip_laddr[1]),
455 LOWER_SHORT(pkt_node->ip_laddr[1]),
456 UPPER_SHORT(pkt_node->ip_laddr[2]),
457 LOWER_SHORT(pkt_node->ip_laddr[2]),
458 UPPER_SHORT(pkt_node->ip_laddr[3]),
459 LOWER_SHORT(pkt_node->ip_laddr[3]),
460 ntohs(pkt_node->tcp_localport),
461 UPPER_SHORT(pkt_node->ip_faddr[0]),
462 LOWER_SHORT(pkt_node->ip_faddr[0]),
463 UPPER_SHORT(pkt_node->ip_faddr[1]),
464 LOWER_SHORT(pkt_node->ip_faddr[1]),
465 UPPER_SHORT(pkt_node->ip_faddr[2]),
466 LOWER_SHORT(pkt_node->ip_faddr[2]),
467 UPPER_SHORT(pkt_node->ip_faddr[3]),
468 LOWER_SHORT(pkt_node->ip_faddr[3]),
469 ntohs(pkt_node->tcp_foreignport),
470 pkt_node->snd_ssthresh,
477 pkt_node->conn_state,
478 pkt_node->max_seg_size,
479 pkt_node->smoothed_rtt,
480 pkt_node->sack_enabled,
482 pkt_node->rxt_length,
483 pkt_node->snd_buf_hiwater,
484 pkt_node->snd_buf_cc,
485 pkt_node->rcv_buf_hiwater,
486 pkt_node->rcv_buf_cc,
487 pkt_node->sent_inflight_bytes,
488 pkt_node->t_segqlen);
489 } else { /* IPv4 packet */
490 pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]);
491 pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]);
492 pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]);
493 pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]);
494 pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]);
495 pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]);
496 pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]);
497 pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]);
498 #endif /* SIFTR_IPV6 */
500 /* Construct an IPv4 log message. */
501 log_buf->ae_bytesused = snprintf(log_buf->ae_data,
503 "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%ld,%ld,"
504 "%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u\n",
505 direction[pkt_node->direction],
507 (intmax_t)pkt_node->tval.tv_sec,
508 pkt_node->tval.tv_usec,
509 pkt_node->ip_laddr[0],
510 pkt_node->ip_laddr[1],
511 pkt_node->ip_laddr[2],
512 pkt_node->ip_laddr[3],
513 ntohs(pkt_node->tcp_localport),
514 pkt_node->ip_faddr[0],
515 pkt_node->ip_faddr[1],
516 pkt_node->ip_faddr[2],
517 pkt_node->ip_faddr[3],
518 ntohs(pkt_node->tcp_foreignport),
519 pkt_node->snd_ssthresh,
526 pkt_node->conn_state,
527 pkt_node->max_seg_size,
528 pkt_node->smoothed_rtt,
529 pkt_node->sack_enabled,
531 pkt_node->rxt_length,
532 pkt_node->snd_buf_hiwater,
533 pkt_node->snd_buf_cc,
534 pkt_node->rcv_buf_hiwater,
535 pkt_node->rcv_buf_cc,
536 pkt_node->sent_inflight_bytes,
537 pkt_node->t_segqlen);
542 alq_post_flags(siftr_alq, log_buf, 0);
547 siftr_pkt_manager_thread(void *arg)
549 STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue =
550 STAILQ_HEAD_INITIALIZER(tmp_pkt_queue);
551 struct pkt_node *pkt_node, *pkt_node_temp;
556 mtx_lock(&siftr_pkt_mgr_mtx);
558 /* draining == 0 when queue has been flushed and it's safe to exit. */
561 * Sleep until we are signalled to wake because thread has
562 * been told to exit or until 1 tick has passed.
564 mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait",
567 /* Gain exclusive access to the pkt_node queue. */
568 mtx_lock(&siftr_pkt_queue_mtx);
571 * Move pkt_queue to tmp_pkt_queue, which leaves
572 * pkt_queue empty and ready to receive more pkt_nodes.
574 STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue);
577 * We've finished making changes to the list. Unlock it
578 * so the pfil hooks can continue queuing pkt_nodes.
580 mtx_unlock(&siftr_pkt_queue_mtx);
583 * We can't hold a mutex whilst calling siftr_process_pkt
584 * because ALQ might sleep waiting for buffer space.
586 mtx_unlock(&siftr_pkt_mgr_mtx);
588 /* Flush all pkt_nodes to the log file. */
589 STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes,
591 siftr_process_pkt(pkt_node);
592 STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes);
593 free(pkt_node, M_SIFTR_PKTNODE);
596 KASSERT(STAILQ_EMPTY(&tmp_pkt_queue),
597 ("SIFTR tmp_pkt_queue not empty after flush"));
599 mtx_lock(&siftr_pkt_mgr_mtx);
602 * If siftr_exit_pkt_manager_thread gets set during the window
603 * where we are draining the tmp_pkt_queue above, there might
604 * still be pkts in pkt_queue that need to be drained.
605 * Allow one further iteration to occur after
606 * siftr_exit_pkt_manager_thread has been set to ensure
607 * pkt_queue is completely empty before we kill the thread.
609 * siftr_exit_pkt_manager_thread is set only after the pfil
610 * hooks have been removed, so only 1 extra iteration
611 * is needed to drain the queue.
613 if (siftr_exit_pkt_manager_thread)
617 mtx_unlock(&siftr_pkt_mgr_mtx);
619 /* Calls wakeup on this thread's struct thread ptr. */
625 hash_pkt(struct mbuf *m, uint32_t offset)
631 while (m != NULL && offset > m->m_len) {
633 * The IP packet payload does not start in this mbuf, so
634 * need to figure out which mbuf it starts in and what offset
635 * into the mbuf's data region the payload starts at.
642 /* Ensure there is data in the mbuf */
643 if ((m->m_len - offset) > 0)
644 hash = hash32_buf(m->m_data + offset,
645 m->m_len - offset, hash);
656 * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that
657 * it's a reinjected packet and return. If it doesn't, tag the mbuf and return.
658 * Return value >0 means the caller should skip processing this mbuf.
661 siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss)
663 if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL)
666 ss->nskip_in_dejavu++;
668 ss->nskip_out_dejavu++;
672 struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR,
673 PACKET_TAG_SIFTR, 0, M_NOWAIT);
676 ss->nskip_in_malloc++;
678 ss->nskip_out_malloc++;
683 m_tag_prepend(m, tag);
691 * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL
694 static inline struct inpcb *
695 siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport,
696 uint16_t dport, int dir, struct siftr_stats *ss)
700 /* We need the tcbinfo lock. */
701 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
704 inp = (ipver == INP_IPV4 ?
705 in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst,
706 dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
709 in6_pcblookup(&V_tcbinfo,
710 &((struct ip6_hdr *)ip)->ip6_src, sport,
711 &((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB,
719 inp = (ipver == INP_IPV4 ?
720 in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src,
721 sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
724 in6_pcblookup(&V_tcbinfo,
725 &((struct ip6_hdr *)ip)->ip6_dst, dport,
726 &((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB,
733 /* If we can't find the inpcb, bail. */
736 ss->nskip_in_inpcb++;
738 ss->nskip_out_inpcb++;
746 siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp,
747 int ipver, int dir, int inp_locally_locked)
750 if (ipver == INP_IPV4) {
751 pn->ip_laddr[3] = inp->inp_laddr.s_addr;
752 pn->ip_faddr[3] = inp->inp_faddr.s_addr;
754 *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr;
755 *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr;
759 pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0];
760 pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1];
761 pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2];
762 pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3];
763 pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0];
764 pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1];
765 pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2];
766 pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3];
769 pn->tcp_localport = inp->inp_lport;
770 pn->tcp_foreignport = inp->inp_fport;
771 pn->snd_cwnd = tp->snd_cwnd;
772 pn->snd_wnd = tp->snd_wnd;
773 pn->rcv_wnd = tp->rcv_wnd;
774 pn->snd_bwnd = 0; /* Unused, kept for compat. */
775 pn->snd_ssthresh = tp->snd_ssthresh;
776 pn->snd_scale = tp->snd_scale;
777 pn->rcv_scale = tp->rcv_scale;
778 pn->conn_state = tp->t_state;
779 pn->max_seg_size = tp->t_maxseg;
780 pn->smoothed_rtt = tp->t_srtt;
781 pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0;
782 pn->flags = tp->t_flags;
783 pn->rxt_length = tp->t_rxtcur;
784 pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat;
785 pn->snd_buf_cc = sbused(&inp->inp_socket->so_snd);
786 pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat;
787 pn->rcv_buf_cc = sbused(&inp->inp_socket->so_rcv);
788 pn->sent_inflight_bytes = tp->snd_max - tp->snd_una;
789 pn->t_segqlen = tp->t_segqlen;
791 /* We've finished accessing the tcb so release the lock. */
792 if (inp_locally_locked)
799 * Significantly more accurate than using getmicrotime(), but slower!
800 * Gives true microsecond resolution at the expense of a hit to
801 * maximum pps throughput processing when SIFTR is loaded and enabled.
803 microtime(&pn->tval);
808 * pfil hook that is called for each IPv4 packet making its way through the
809 * stack in either direction.
810 * The pfil subsystem holds a non-sleepable mutex somewhere when
811 * calling our hook function, so we can't sleep at all.
812 * It's very important to use the M_NOWAIT flag with all function calls
813 * that support it so that they won't sleep, otherwise you get a panic.
816 siftr_chkpkt(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
823 struct siftr_stats *ss;
825 int inp_locally_locked;
827 inp_locally_locked = 0;
831 * m_pullup is not required here because ip_{input|output}
832 * already do the heavy lifting for us.
835 ip = mtod(*m, struct ip *);
837 /* Only continue processing if the packet is TCP. */
838 if (ip->ip_p != IPPROTO_TCP)
842 * If a kernel subsystem reinjects packets into the stack, our pfil
843 * hook will be called multiple times for the same packet.
844 * Make sure we only process unique packets.
846 if (siftr_chkreinject(*m, dir, ss))
855 * Create a tcphdr struct starting at the correct offset
856 * in the IP packet. ip->ip_hl gives the ip header length
857 * in 4-byte words, so multiply it to get the size in bytes.
859 ip_hl = (ip->ip_hl << 2);
860 th = (struct tcphdr *)((caddr_t)ip + ip_hl);
863 * If the pfil hooks don't provide a pointer to the
864 * inpcb, we need to find it ourselves and lock it.
867 /* Find the corresponding inpcb for this pkt. */
868 inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport,
869 th->th_dport, dir, ss);
874 inp_locally_locked = 1;
877 INP_LOCK_ASSERT(inp);
879 /* Find the TCP control block that corresponds with this packet */
883 * If we can't find the TCP control block (happens occasionaly for a
884 * packet sent during the shutdown phase of a TCP connection),
885 * or we're in the timewait state, bail
887 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) {
889 ss->nskip_in_tcpcb++;
891 ss->nskip_out_tcpcb++;
896 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
900 ss->nskip_in_malloc++;
902 ss->nskip_out_malloc++;
907 siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked);
909 if (siftr_generate_hashes) {
910 if ((*m)->m_pkthdr.csum_flags & CSUM_TCP) {
912 * For outbound packets, the TCP checksum isn't
913 * calculated yet. This is a problem for our packet
914 * hashing as the receiver will calc a different hash
915 * to ours if we don't include the correct TCP checksum
916 * in the bytes being hashed. To work around this
917 * problem, we manually calc the TCP checksum here in
918 * software. We unset the CSUM_TCP flag so the lower
919 * layers don't recalc it.
921 (*m)->m_pkthdr.csum_flags &= ~CSUM_TCP;
924 * Calculate the TCP checksum in software and assign
925 * to correct TCP header field, which will follow the
926 * packet mbuf down the stack. The trick here is that
927 * tcp_output() sets th->th_sum to the checksum of the
928 * pseudo header for us already. Because of the nature
929 * of the checksumming algorithm, we can sum over the
930 * entire IP payload (i.e. TCP header and data), which
931 * will include the already calculated pseduo header
932 * checksum, thus giving us the complete TCP checksum.
934 * To put it in simple terms, if checksum(1,2,3,4)=10,
935 * then checksum(1,2,3,4,5) == checksum(10,5).
936 * This property is what allows us to "cheat" and
937 * checksum only the IP payload which has the TCP
938 * th_sum field populated with the pseudo header's
939 * checksum, and not need to futz around checksumming
940 * pseudo header bytes and TCP header/data in one hit.
941 * Refer to RFC 1071 for more info.
943 * NB: in_cksum_skip(struct mbuf *m, int len, int skip)
944 * in_cksum_skip 2nd argument is NOT the number of
945 * bytes to read from the mbuf at "skip" bytes offset
946 * from the start of the mbuf (very counter intuitive!).
947 * The number of bytes to read is calculated internally
948 * by the function as len-skip i.e. to sum over the IP
949 * payload (TCP header + data) bytes, it is INCORRECT
950 * to call the function like this:
951 * in_cksum_skip(at, ip->ip_len - offset, offset)
952 * Rather, it should be called like this:
953 * in_cksum_skip(at, ip->ip_len, offset)
954 * which means read "ip->ip_len - offset" bytes from
955 * the mbuf cluster "at" at offset "offset" bytes from
956 * the beginning of the "at" mbuf's data pointer.
958 th->th_sum = in_cksum_skip(*m, ntohs(ip->ip_len),
963 * XXX: Having to calculate the checksum in software and then
964 * hash over all bytes is really inefficient. Would be nice to
965 * find a way to create the hash and checksum in the same pass
968 pn->hash = hash_pkt(*m, ip_hl);
971 mtx_lock(&siftr_pkt_queue_mtx);
972 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
973 mtx_unlock(&siftr_pkt_queue_mtx);
977 if (inp_locally_locked)
981 /* Returning 0 ensures pfil will not discard the pkt */
988 siftr_chkpkt6(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
995 struct siftr_stats *ss;
997 int inp_locally_locked;
999 inp_locally_locked = 0;
1003 * m_pullup is not required here because ip6_{input|output}
1004 * already do the heavy lifting for us.
1007 ip6 = mtod(*m, struct ip6_hdr *);
1010 * Only continue processing if the packet is TCP
1011 * XXX: We should follow the next header fields
1012 * as shown on Pg 6 RFC 2460, but right now we'll
1013 * only check pkts that have no extension headers.
1015 if (ip6->ip6_nxt != IPPROTO_TCP)
1019 * If a kernel subsystem reinjects packets into the stack, our pfil
1020 * hook will be called multiple times for the same packet.
1021 * Make sure we only process unique packets.
1023 if (siftr_chkreinject(*m, dir, ss))
1031 ip6_hl = sizeof(struct ip6_hdr);
1034 * Create a tcphdr struct starting at the correct offset
1035 * in the ipv6 packet. ip->ip_hl gives the ip header length
1036 * in 4-byte words, so multiply it to get the size in bytes.
1038 th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl);
1041 * For inbound packets, the pfil hooks don't provide a pointer to the
1042 * inpcb, so we need to find it ourselves and lock it.
1045 /* Find the corresponding inpcb for this pkt. */
1046 inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m,
1047 th->th_sport, th->th_dport, dir, ss);
1052 inp_locally_locked = 1;
1055 /* Find the TCP control block that corresponds with this packet. */
1056 tp = intotcpcb(inp);
1059 * If we can't find the TCP control block (happens occasionaly for a
1060 * packet sent during the shutdown phase of a TCP connection),
1061 * or we're in the timewait state, bail.
1063 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) {
1065 ss->nskip_in_tcpcb++;
1067 ss->nskip_out_tcpcb++;
1072 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
1076 ss->nskip_in_malloc++;
1078 ss->nskip_out_malloc++;
1083 siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked);
1085 /* XXX: Figure out how to generate hashes for IPv6 packets. */
1087 mtx_lock(&siftr_pkt_queue_mtx);
1088 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
1089 mtx_unlock(&siftr_pkt_queue_mtx);
1093 if (inp_locally_locked)
1097 /* Returning 0 ensures pfil will not discard the pkt. */
1100 #endif /* #ifdef SIFTR_IPV6 */
1104 siftr_pfil(int action)
1106 struct pfil_head *pfh_inet;
1108 struct pfil_head *pfh_inet6;
1110 VNET_ITERATOR_DECL(vnet_iter);
1113 VNET_FOREACH(vnet_iter) {
1114 CURVNET_SET(vnet_iter);
1115 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1117 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1120 if (action == HOOK) {
1121 pfil_add_hook(siftr_chkpkt, NULL,
1122 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet);
1124 pfil_add_hook(siftr_chkpkt6, NULL,
1125 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6);
1127 } else if (action == UNHOOK) {
1128 pfil_remove_hook(siftr_chkpkt, NULL,
1129 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet);
1131 pfil_remove_hook(siftr_chkpkt6, NULL,
1132 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6);
1137 VNET_LIST_RUNLOCK();
1144 siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS)
1146 struct alq *new_alq;
1149 error = sysctl_handle_string(oidp, arg1, arg2, req);
1151 /* Check for error or same filename */
1152 if (error != 0 || req->newptr == NULL ||
1153 strncmp(siftr_logfile, arg1, arg2) == 0)
1156 /* Filname changed */
1157 error = alq_open(&new_alq, arg1, curthread->td_ucred,
1158 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1163 * If disabled, siftr_alq == NULL so we simply close
1164 * the alq as we've proved it can be opened.
1165 * If enabled, close the existing alq and switch the old
1168 if (siftr_alq == NULL) {
1171 alq_close(siftr_alq);
1172 siftr_alq = new_alq;
1175 /* Update filename upon success */
1176 strlcpy(siftr_logfile, arg1, arg2);
1182 siftr_manage_ops(uint8_t action)
1184 struct siftr_stats totalss;
1185 struct timeval tval;
1186 struct flow_hash_node *counter, *tmp_counter;
1188 int i, key_index, ret, error;
1189 uint32_t bytes_to_write, total_skipped_pkts;
1190 uint16_t lport, fport;
1191 uint8_t *key, ipver;
1202 total_skipped_pkts = 0;
1204 /* Init an autosizing sbuf that initially holds 200 chars. */
1205 if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL)
1208 if (action == SIFTR_ENABLE) {
1211 * XXX: We should abort if alq_open fails!
1213 alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred,
1214 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
1216 STAILQ_INIT(&pkt_queue);
1220 siftr_exit_pkt_manager_thread = 0;
1222 ret = kthread_add(&siftr_pkt_manager_thread, NULL, NULL,
1223 &siftr_pkt_manager_thr, RFNOWAIT, 0,
1224 "siftr_pkt_manager_thr");
1231 "enable_time_secs=%jd\tenable_time_usecs=%06ld\t"
1232 "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t"
1233 "sysver=%u\tipmode=%u\n",
1234 (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz,
1235 TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE);
1238 alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK);
1240 } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) {
1242 * Remove the pfil hook functions. All threads currently in
1243 * the hook functions are allowed to exit before siftr_pfil()
1248 /* This will block until the pkt manager thread unlocks it. */
1249 mtx_lock(&siftr_pkt_mgr_mtx);
1251 /* Tell the pkt manager thread that it should exit now. */
1252 siftr_exit_pkt_manager_thread = 1;
1255 * Wake the pkt_manager thread so it realises that
1256 * siftr_exit_pkt_manager_thread == 1 and exits gracefully.
1257 * The wakeup won't be delivered until we unlock
1258 * siftr_pkt_mgr_mtx so this isn't racy.
1260 wakeup(&wait_for_pkt);
1262 /* Wait for the pkt_manager thread to exit. */
1263 mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT,
1266 siftr_pkt_manager_thr = NULL;
1267 mtx_unlock(&siftr_pkt_mgr_mtx);
1269 totalss.n_in = DPCPU_VARSUM(ss, n_in);
1270 totalss.n_out = DPCPU_VARSUM(ss, n_out);
1271 totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc);
1272 totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc);
1273 totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx);
1274 totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx);
1275 totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb);
1276 totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb);
1277 totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb);
1278 totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb);
1280 total_skipped_pkts = totalss.nskip_in_malloc +
1281 totalss.nskip_out_malloc + totalss.nskip_in_mtx +
1282 totalss.nskip_out_mtx + totalss.nskip_in_tcpcb +
1283 totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb +
1284 totalss.nskip_out_inpcb;
1289 "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t"
1290 "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t"
1291 "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t"
1292 "num_outbound_skipped_pkts_malloc=%u\t"
1293 "num_inbound_skipped_pkts_mtx=%u\t"
1294 "num_outbound_skipped_pkts_mtx=%u\t"
1295 "num_inbound_skipped_pkts_tcpcb=%u\t"
1296 "num_outbound_skipped_pkts_tcpcb=%u\t"
1297 "num_inbound_skipped_pkts_inpcb=%u\t"
1298 "num_outbound_skipped_pkts_inpcb=%u\t"
1299 "total_skipped_tcp_pkts=%u\tflow_list=",
1300 (intmax_t)tval.tv_sec,
1302 (uintmax_t)totalss.n_in,
1303 (uintmax_t)totalss.n_out,
1304 (uintmax_t)(totalss.n_in + totalss.n_out),
1305 totalss.nskip_in_malloc,
1306 totalss.nskip_out_malloc,
1307 totalss.nskip_in_mtx,
1308 totalss.nskip_out_mtx,
1309 totalss.nskip_in_tcpcb,
1310 totalss.nskip_out_tcpcb,
1311 totalss.nskip_in_inpcb,
1312 totalss.nskip_out_inpcb,
1313 total_skipped_pkts);
1316 * Iterate over the flow hash, printing a summary of each
1317 * flow seen and freeing any malloc'd memory.
1318 * The hash consists of an array of LISTs (man 3 queue).
1320 for (i = 0; i <= siftr_hashmask; i++) {
1321 LIST_FOREACH_SAFE(counter, counter_hash + i, nodes,
1328 memcpy(laddr, key + key_index, sizeof(laddr));
1329 key_index += sizeof(laddr);
1330 memcpy(&lport, key + key_index, sizeof(lport));
1331 key_index += sizeof(lport);
1332 memcpy(faddr, key + key_index, sizeof(faddr));
1333 key_index += sizeof(faddr);
1334 memcpy(&fport, key + key_index, sizeof(fport));
1337 laddr[3] = ntohl(laddr[3]);
1338 faddr[3] = ntohl(faddr[3]);
1340 if (ipver == INP_IPV6) {
1341 laddr[0] = ntohl(laddr[0]);
1342 laddr[1] = ntohl(laddr[1]);
1343 laddr[2] = ntohl(laddr[2]);
1344 faddr[0] = ntohl(faddr[0]);
1345 faddr[1] = ntohl(faddr[1]);
1346 faddr[2] = ntohl(faddr[2]);
1349 "%x:%x:%x:%x:%x:%x:%x:%x;%u-"
1350 "%x:%x:%x:%x:%x:%x:%x:%x;%u,",
1351 UPPER_SHORT(laddr[0]),
1352 LOWER_SHORT(laddr[0]),
1353 UPPER_SHORT(laddr[1]),
1354 LOWER_SHORT(laddr[1]),
1355 UPPER_SHORT(laddr[2]),
1356 LOWER_SHORT(laddr[2]),
1357 UPPER_SHORT(laddr[3]),
1358 LOWER_SHORT(laddr[3]),
1360 UPPER_SHORT(faddr[0]),
1361 LOWER_SHORT(faddr[0]),
1362 UPPER_SHORT(faddr[1]),
1363 LOWER_SHORT(faddr[1]),
1364 UPPER_SHORT(faddr[2]),
1365 LOWER_SHORT(faddr[2]),
1366 UPPER_SHORT(faddr[3]),
1367 LOWER_SHORT(faddr[3]),
1370 laddr[0] = FIRST_OCTET(laddr[3]);
1371 laddr[1] = SECOND_OCTET(laddr[3]);
1372 laddr[2] = THIRD_OCTET(laddr[3]);
1373 laddr[3] = FOURTH_OCTET(laddr[3]);
1374 faddr[0] = FIRST_OCTET(faddr[3]);
1375 faddr[1] = SECOND_OCTET(faddr[3]);
1376 faddr[2] = THIRD_OCTET(faddr[3]);
1377 faddr[3] = FOURTH_OCTET(faddr[3]);
1380 "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,",
1395 free(counter, M_SIFTR_HASHNODE);
1398 LIST_INIT(counter_hash + i);
1401 sbuf_printf(s, "\n");
1406 bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i);
1407 alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK);
1408 i += bytes_to_write;
1409 } while (i < sbuf_len(s));
1411 alq_close(siftr_alq);
1418 * XXX: Should be using ret to check if any functions fail
1419 * and set error appropriately
1427 siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS)
1429 if (req->newptr == NULL)
1432 /* If the value passed in isn't 0 or 1, return an error. */
1433 if (CAST_PTR_INT(req->newptr) != 0 && CAST_PTR_INT(req->newptr) != 1)
1436 /* If we are changing state (0 to 1 or 1 to 0). */
1437 if (CAST_PTR_INT(req->newptr) != siftr_enabled )
1438 if (siftr_manage_ops(CAST_PTR_INT(req->newptr))) {
1439 siftr_manage_ops(SIFTR_DISABLE);
1444 return (sysctl_handle_int(oidp, arg1, arg2, req));
1449 siftr_shutdown_handler(void *arg)
1451 siftr_manage_ops(SIFTR_DISABLE);
1456 * Module is being unloaded or machine is shutting down. Take care of cleanup.
1462 siftr_manage_ops(SIFTR_DISABLE);
1463 hashdestroy(counter_hash, M_SIFTR, siftr_hashmask);
1464 mtx_destroy(&siftr_pkt_queue_mtx);
1465 mtx_destroy(&siftr_pkt_mgr_mtx);
1472 * Module has just been loaded into the kernel.
1477 EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL,
1478 SHUTDOWN_PRI_FIRST);
1480 /* Initialise our flow counter hash table. */
1481 counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR,
1484 mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF);
1485 mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF);
1487 /* Print message to the user's current terminal. */
1488 uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n"
1489 " http://caia.swin.edu.au/urp/newtcp\n\n",
1497 * This is the function that is called to load and unload the module.
1498 * When the module is loaded, this function is called once with
1499 * "what" == MOD_LOAD
1500 * When the module is unloaded, this function is called twice with
1501 * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second
1502 * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command,
1503 * this function is called once with "what" = MOD_SHUTDOWN
1504 * When the system is shut down, the handler isn't called until the very end
1505 * of the shutdown sequence i.e. after the disks have been synced.
1508 siftr_load_handler(module_t mod, int what, void *arg)
1519 ret = deinit_siftr();
1535 static moduledata_t siftr_mod = {
1537 .evhand = siftr_load_handler,
1541 * Param 1: name of the kernel module
1542 * Param 2: moduledata_t struct containing info about the kernel module
1543 * and the execution entry point for the module
1544 * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h
1545 * Defines the module initialisation order
1546 * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h
1547 * Defines the initialisation order of this kld relative to others
1548 * within the same subsystem as defined by param 3
1550 DECLARE_MODULE(siftr, siftr_mod, SI_SUB_SMP, SI_ORDER_ANY);
1551 MODULE_DEPEND(siftr, alq, 1, 1, 1);
1552 MODULE_VERSION(siftr, MODVERSION);