2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016-2017 Netflix, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/filio.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <sys/queue.h>
42 #include <sys/refcount.h>
43 #include <sys/mutex.h>
44 #include <sys/selinfo.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
50 #include <machine/atomic.h>
51 #include <sys/counter.h>
53 #include <dev/tcp_log/tcp_log_dev.h>
55 #ifdef TCPLOG_DEBUG_COUNTERS
56 extern counter_u64_t tcp_log_que_read;
57 extern counter_u64_t tcp_log_que_freed;
60 static struct cdev *tcp_log_dev;
61 static struct selinfo tcp_log_sel;
63 static struct log_queueh tcp_log_dev_queue_head = STAILQ_HEAD_INITIALIZER(tcp_log_dev_queue_head);
64 static struct log_infoh tcp_log_dev_reader_head = STAILQ_HEAD_INITIALIZER(tcp_log_dev_reader_head);
66 MALLOC_DEFINE(M_TCPLOGDEV, "tcp_log_dev", "TCP log device data structures");
68 static int tcp_log_dev_listeners = 0;
70 static struct mtx tcp_log_dev_queue_lock;
72 #define TCP_LOG_DEV_QUEUE_LOCK() mtx_lock(&tcp_log_dev_queue_lock)
73 #define TCP_LOG_DEV_QUEUE_UNLOCK() mtx_unlock(&tcp_log_dev_queue_lock)
74 #define TCP_LOG_DEV_QUEUE_LOCK_ASSERT() mtx_assert(&tcp_log_dev_queue_lock, MA_OWNED)
75 #define TCP_LOG_DEV_QUEUE_UNLOCK_ASSERT() mtx_assert(&tcp_log_dev_queue_lock, MA_NOTOWNED)
76 #define TCP_LOG_DEV_QUEUE_REF(tldq) refcount_acquire(&((tldq)->tldq_refcnt))
77 #define TCP_LOG_DEV_QUEUE_UNREF(tldq) refcount_release(&((tldq)->tldq_refcnt))
79 static void tcp_log_dev_clear_refcount(struct tcp_log_dev_queue *entry);
80 static void tcp_log_dev_clear_cdevpriv(void *data);
81 static int tcp_log_dev_open(struct cdev *dev __unused, int flags,
82 int devtype __unused, struct thread *td __unused);
83 static int tcp_log_dev_write(struct cdev *dev __unused,
84 struct uio *uio __unused, int flags __unused);
85 static int tcp_log_dev_read(struct cdev *dev __unused, struct uio *uio,
87 static int tcp_log_dev_ioctl(struct cdev *dev __unused, u_long cmd,
88 caddr_t data, int fflag __unused, struct thread *td __unused);
89 static int tcp_log_dev_poll(struct cdev *dev __unused, int events,
92 enum tcp_log_dev_queue_lock_state {
97 static struct cdevsw tcp_log_cdevsw = {
98 .d_version = D_VERSION,
99 .d_read = tcp_log_dev_read,
100 .d_open = tcp_log_dev_open,
101 .d_write = tcp_log_dev_write,
102 .d_poll = tcp_log_dev_poll,
103 .d_ioctl = tcp_log_dev_ioctl,
105 .d_mmap = tcp_log_dev_mmap,
111 tcp_log_dev_queue_validate_lock(int lockstate)
117 TCP_LOG_DEV_QUEUE_LOCK_ASSERT();
120 TCP_LOG_DEV_QUEUE_UNLOCK_ASSERT();
123 kassert_panic("%s:%d: unknown queue lock state", __func__,
130 * Clear the refcount. If appropriate, it will remove the entry from the
131 * queue and call the destructor.
133 * This must be called with the queue lock held.
136 tcp_log_dev_clear_refcount(struct tcp_log_dev_queue *entry)
139 KASSERT(entry != NULL, ("%s: called with NULL entry", __func__));
141 TCP_LOG_DEV_QUEUE_LOCK_ASSERT();
143 if (TCP_LOG_DEV_QUEUE_UNREF(entry)) {
144 #ifdef TCPLOG_DEBUG_COUNTERS
145 counter_u64_add(tcp_log_que_freed, 1);
147 /* Remove the entry from the queue and call the destructor. */
148 STAILQ_REMOVE(&tcp_log_dev_queue_head, entry, tcp_log_dev_queue,
150 (*entry->tldq_dtor)(entry);
155 tcp_log_dev_clear_cdevpriv(void *data)
157 struct tcp_log_dev_info *priv;
158 struct tcp_log_dev_queue *entry, *entry_tmp;
160 priv = (struct tcp_log_dev_info *)data;
165 * Lock the queue and drop our references. We hold references to all
166 * the entries starting with tldi_head (or, if tldi_head == NULL, all
167 * entries in the queue).
169 * Because we don't want anyone adding addition things to the queue
170 * while we are doing this, we lock the queue.
172 TCP_LOG_DEV_QUEUE_LOCK();
173 if (priv->tldi_head != NULL) {
174 entry = priv->tldi_head;
175 STAILQ_FOREACH_FROM_SAFE(entry, &tcp_log_dev_queue_head,
176 tldq_queue, entry_tmp) {
177 tcp_log_dev_clear_refcount(entry);
180 tcp_log_dev_listeners--;
181 KASSERT(tcp_log_dev_listeners >= 0,
182 ("%s: tcp_log_dev_listeners is unexpectedly negative", __func__));
183 STAILQ_REMOVE(&tcp_log_dev_reader_head, priv, tcp_log_dev_info,
185 TCP_LOG_DEV_QUEUE_LOCK_ASSERT();
186 TCP_LOG_DEV_QUEUE_UNLOCK();
187 free(priv, M_TCPLOGDEV);
191 tcp_log_dev_open(struct cdev *dev __unused, int flags, int devtype __unused,
192 struct thread *td __unused)
194 struct tcp_log_dev_info *priv;
195 struct tcp_log_dev_queue *entry;
199 * Ideally, we shouldn't see these because of file system
202 if (flags & (FWRITE | FEXEC | FAPPEND | O_TRUNC))
205 /* Allocate space to hold information about where we are. */
206 priv = malloc(sizeof(struct tcp_log_dev_info), M_TCPLOGDEV,
209 /* Stash the private data away. */
210 rv = devfs_set_cdevpriv((void *)priv, tcp_log_dev_clear_cdevpriv);
213 * Increase the listener count, add this reader to the list, and
214 * take references on all current queues.
216 TCP_LOG_DEV_QUEUE_LOCK();
217 tcp_log_dev_listeners++;
218 STAILQ_INSERT_HEAD(&tcp_log_dev_reader_head, priv, tldi_list);
219 priv->tldi_head = STAILQ_FIRST(&tcp_log_dev_queue_head);
220 if (priv->tldi_head != NULL)
221 priv->tldi_cur = priv->tldi_head->tldq_buf;
222 STAILQ_FOREACH(entry, &tcp_log_dev_queue_head, tldq_queue)
223 TCP_LOG_DEV_QUEUE_REF(entry);
224 TCP_LOG_DEV_QUEUE_UNLOCK();
226 /* Free the entry. */
227 free(priv, M_TCPLOGDEV);
233 tcp_log_dev_write(struct cdev *dev __unused, struct uio *uio __unused,
241 tcp_log_dev_rotate_bufs(struct tcp_log_dev_info *priv, int *lockstate)
243 struct tcp_log_dev_queue *entry;
245 KASSERT(priv->tldi_head != NULL,
246 ("%s:%d: priv->tldi_head unexpectedly NULL",
247 __func__, __LINE__));
248 KASSERT(priv->tldi_head->tldq_buf == priv->tldi_cur,
249 ("%s:%d: buffer mismatch (%p vs %p)",
250 __func__, __LINE__, priv->tldi_head->tldq_buf,
252 tcp_log_dev_queue_validate_lock(*lockstate);
254 if (*lockstate == QUEUE_UNLOCKED) {
255 TCP_LOG_DEV_QUEUE_LOCK();
256 *lockstate = QUEUE_LOCKED;
258 entry = priv->tldi_head;
259 priv->tldi_head = STAILQ_NEXT(entry, tldq_queue);
260 tcp_log_dev_clear_refcount(entry);
261 priv->tldi_cur = NULL;
265 tcp_log_dev_read(struct cdev *dev __unused, struct uio *uio, int flags)
267 struct tcp_log_common_header *buf;
268 struct tcp_log_dev_info *priv;
269 struct tcp_log_dev_queue *entry;
273 /* Get our private info. */
274 rv = devfs_get_cdevpriv((void **)&priv);
278 lockstate = QUEUE_UNLOCKED;
280 /* Do we need to get a new buffer? */
281 while (priv->tldi_cur == NULL ||
282 priv->tldi_cur->tlch_length <= priv->tldi_off) {
283 /* Did we somehow forget to rotate? */
284 KASSERT(priv->tldi_cur == NULL,
285 ("%s:%d: tldi_cur is unexpectedly non-NULL", __func__,
287 if (priv->tldi_cur != NULL)
288 tcp_log_dev_rotate_bufs(priv, &lockstate);
291 * Before we start looking at tldi_head, we need a lock on the
292 * queue to make sure tldi_head stays stable.
294 if (lockstate == QUEUE_UNLOCKED) {
295 TCP_LOG_DEV_QUEUE_LOCK();
296 lockstate = QUEUE_LOCKED;
299 /* We need the next buffer. Do we have one? */
300 if (priv->tldi_head == NULL && (flags & FNONBLOCK)) {
304 if (priv->tldi_head == NULL) {
305 /* Sleep and wait for more things we can read. */
306 rv = mtx_sleep(&tcp_log_dev_listeners,
307 &tcp_log_dev_queue_lock, PCATCH, "tcplogdev", 0);
310 if (priv->tldi_head == NULL)
315 * We have an entry to read. We want to try to create a
316 * buffer, if one doesn't already exist.
318 entry = priv->tldi_head;
319 if (entry->tldq_buf == NULL) {
320 TCP_LOG_DEV_QUEUE_LOCK_ASSERT();
321 buf = (*entry->tldq_xform)(entry);
326 entry->tldq_buf = buf;
329 priv->tldi_cur = entry->tldq_buf;
333 /* Copy what we can from this buffer to the output buffer. */
334 if (uio->uio_resid > 0) {
335 /* Drop locks so we can take page faults. */
336 if (lockstate == QUEUE_LOCKED)
337 TCP_LOG_DEV_QUEUE_UNLOCK();
338 lockstate = QUEUE_UNLOCKED;
340 KASSERT(priv->tldi_cur != NULL,
341 ("%s: priv->tldi_cur is unexpectedly NULL", __func__));
343 /* Copy as much as we can to this uio. */
344 len = priv->tldi_cur->tlch_length - priv->tldi_off;
345 if (len > uio->uio_resid)
346 len = uio->uio_resid;
347 rv = uiomove(((uint8_t *)priv->tldi_cur) + priv->tldi_off,
351 priv->tldi_off += len;
352 #ifdef TCPLOG_DEBUG_COUNTERS
353 counter_u64_add(tcp_log_que_read, len);
356 /* Are we done with this buffer? If so, find the next one. */
357 if (priv->tldi_off >= priv->tldi_cur->tlch_length) {
358 KASSERT(priv->tldi_off == priv->tldi_cur->tlch_length,
359 ("%s: offset (%ju) exceeds length (%ju)", __func__,
360 (uintmax_t)priv->tldi_off,
361 (uintmax_t)priv->tldi_cur->tlch_length));
362 tcp_log_dev_rotate_bufs(priv, &lockstate);
365 tcp_log_dev_queue_validate_lock(lockstate);
366 if (lockstate == QUEUE_LOCKED)
367 TCP_LOG_DEV_QUEUE_UNLOCK();
372 tcp_log_dev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
373 int fflag __unused, struct thread *td __unused)
375 struct tcp_log_dev_info *priv;
378 /* Get our private info. */
379 rv = devfs_get_cdevpriv((void **)&priv);
384 * Set things. Here, we are most concerned about the non-blocking I/O
392 if (*(int *)data != 0)
402 tcp_log_dev_poll(struct cdev *dev __unused, int events, struct thread *td)
404 struct tcp_log_dev_info *priv;
408 * Get our private info. If this fails, claim that all events are
409 * ready. That should prod the user to do something that will
410 * make the error evident to them.
412 if (devfs_get_cdevpriv((void **)&priv))
416 if (events & (POLLIN | POLLRDNORM)) {
418 * We can (probably) read right now if we are partway through
419 * a buffer or if we are just about to start a buffer.
420 * Because we are going to read tldi_head, we should acquire
421 * a read lock on the queue.
423 TCP_LOG_DEV_QUEUE_LOCK();
424 if ((priv->tldi_head != NULL && priv->tldi_cur == NULL) ||
425 (priv->tldi_cur != NULL &&
426 priv->tldi_off < priv->tldi_cur->tlch_length))
427 revents = events & (POLLIN | POLLRDNORM);
429 selrecord(td, &tcp_log_sel);
430 TCP_LOG_DEV_QUEUE_UNLOCK();
433 * It only makes sense to poll for reading. So, again, prod the
434 * user to do something that will make the error of their ways
443 tcp_log_dev_add_log(struct tcp_log_dev_queue *entry)
445 struct tcp_log_dev_info *priv;
449 KASSERT(entry->tldq_buf != NULL || entry->tldq_xform != NULL,
450 ("%s: Called with both tldq_buf and tldq_xform set to NULL",
452 KASSERT(entry->tldq_dtor != NULL,
453 ("%s: Called with tldq_dtor set to NULL", __func__));
455 /* Get a lock on the queue. */
456 TCP_LOG_DEV_QUEUE_LOCK();
458 /* If no one is listening, tell the caller to free the resources. */
459 if (tcp_log_dev_listeners == 0) {
464 /* Add this to the end of the tailq. */
465 STAILQ_INSERT_TAIL(&tcp_log_dev_queue_head, entry, tldq_queue);
467 /* Add references for all current listeners. */
468 refcount_init(&entry->tldq_refcnt, tcp_log_dev_listeners);
471 * If any listener is currently stuck on NULL, that means they are
472 * waiting. Point their head to this new entry.
474 wakeup_needed = false;
475 STAILQ_FOREACH(priv, &tcp_log_dev_reader_head, tldi_list)
476 if (priv->tldi_head == NULL) {
477 priv->tldi_head = entry;
478 wakeup_needed = true;
482 selwakeup(&tcp_log_sel);
483 wakeup(&tcp_log_dev_listeners);
489 TCP_LOG_DEV_QUEUE_LOCK_ASSERT();
490 TCP_LOG_DEV_QUEUE_UNLOCK();
495 tcp_log_dev_modevent(module_t mod __unused, int type, void *data __unused)
498 /* TODO: Support intelligent unloading. */
502 printf("tcp_log: tcp_log device\n");
503 memset(&tcp_log_sel, 0, sizeof(tcp_log_sel));
504 memset(&tcp_log_dev_queue_lock, 0, sizeof(struct mtx));
505 mtx_init(&tcp_log_dev_queue_lock, "tcp_log dev",
506 "tcp_log device queues", MTX_DEF);
507 tcp_log_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
508 &tcp_log_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0400,
518 DEV_MODULE(tcp_log_dev, tcp_log_dev_modevent, NULL);
519 MODULE_VERSION(tcp_log_dev, 1);