2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
33 #include "if_wtapvar.h"
34 #include "if_medium.h"
37 init_medium(struct wtap_medium *md)
40 DWTAP_PRINTF("%s\n", __func__);
41 STAILQ_INIT(&md->md_pktbuf);
42 mtx_init(&md->md_mtx, "wtap_medium mtx", NULL, MTX_DEF | MTX_RECURSE);
44 /* Event handler for sending packets between wtaps */
45 struct eventhandler *eh = (struct eventhandler *)
46 malloc(sizeof(struct eventhandler), M_WTAP, M_NOWAIT | M_ZERO);
47 eh->tq = taskqueue_create("wtap_tx_taskq", M_NOWAIT | M_ZERO,
48 taskqueue_thread_enqueue, &eh->tq);
49 taskqueue_start_threads(&eh->tq, 1, PI_NET, "%s taskq", "wtap_medium");
51 /* Mark medium closed by default */
56 deinit_medium(struct wtap_medium *md)
59 DWTAP_PRINTF("%s\n", __func__);
60 taskqueue_free(md->tx_handler->tq);
61 free(md->tx_handler, M_WTAP);
65 medium_transmit(struct wtap_medium *md, int id, struct mbuf*m)
68 mtx_lock(&md->md_mtx);
70 DWTAP_PRINTF("[%d] dropping m=%p\n", id, m);
72 mtx_unlock(&md->md_mtx);
76 DWTAP_PRINTF("[%d] transmiting m=%p\n", id, m);
77 struct packet *p = (struct packet *)malloc(sizeof(struct packet),
78 M_WTAP_PACKET, M_ZERO | M_NOWAIT);
82 STAILQ_INSERT_TAIL(&md->md_pktbuf, p, pf_list);
83 taskqueue_enqueue(md->tx_handler->tq, &md->tx_handler->proc);
84 mtx_unlock(&md->md_mtx);
90 medium_get_next_packet(struct wtap_medium *md)
94 mtx_lock(&md->md_mtx);
95 p = STAILQ_FIRST(&md->md_pktbuf);
97 mtx_unlock(&md->md_mtx);
101 STAILQ_REMOVE_HEAD(&md->md_pktbuf, pf_list);
102 mtx_unlock(&md->md_mtx);
107 medium_open(struct wtap_medium *md)
110 mtx_lock(&md->md_mtx);
112 mtx_unlock(&md->md_mtx);
116 medium_close(struct wtap_medium *md)
119 mtx_lock(&md->md_mtx);
121 mtx_unlock(&md->md_mtx);