if_medium.c revision 330897
1145522Sdarrenr/*- 2145522Sdarrenr * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 353642Sguido * 4255332Scy * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB 553642Sguido * All rights reserved. 680482Sdarrenr * 753642Sguido * Redistribution and use in source and binary forms, with or without 853642Sguido * modification, are permitted provided that the following conditions 957126Sguido * are met: 10172776Sdarrenr * 1. Redistributions of source code must retain the above copyright 1153642Sguido * notice, this list of conditions and the following disclaimer, 1253642Sguido * without modification. 1353642Sguido * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1453642Sguido * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 1553642Sguido * redistribution must be conditioned upon including a substantially 16255332Scy * similar Disclaimer requirement for further binary redistribution. 1753642Sguido * 18255332Scy * NO WARRANTY 19145522Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20255332Scy * ``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. 30 * 31 * $FreeBSD: stable/11/sys/dev/wtap/if_medium.c 330897 2018-03-14 03:19:51Z eadler $ 32 */ 33#include "if_wtapvar.h" 34#include "if_medium.h" 35 36void 37init_medium(struct wtap_medium *md) 38{ 39 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); 43 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"); 50 md->tx_handler = eh; 51 /* Mark medium closed by default */ 52 md->open = 0; 53} 54 55void 56deinit_medium(struct wtap_medium *md) 57{ 58 59 DWTAP_PRINTF("%s\n", __func__); 60 taskqueue_free(md->tx_handler->tq); 61 free(md->tx_handler, M_WTAP); 62} 63 64int 65medium_transmit(struct wtap_medium *md, int id, struct mbuf*m) 66{ 67 68 mtx_lock(&md->md_mtx); 69 if (md->open == 0){ 70 DWTAP_PRINTF("[%d] dropping m=%p\n", id, m); 71 m_free(m); 72 mtx_unlock(&md->md_mtx); 73 return 0; 74 } 75 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); 79 p->id = id; 80 p->m = m; 81 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); 85 86 return 0; 87} 88 89struct packet * 90medium_get_next_packet(struct wtap_medium *md) 91{ 92 struct packet *p; 93 94 mtx_lock(&md->md_mtx); 95 p = STAILQ_FIRST(&md->md_pktbuf); 96 if (p == NULL){ 97 mtx_unlock(&md->md_mtx); 98 return NULL; 99 } 100 101 STAILQ_REMOVE_HEAD(&md->md_pktbuf, pf_list); 102 mtx_unlock(&md->md_mtx); 103 return p; 104} 105 106void 107medium_open(struct wtap_medium *md) 108{ 109 110 mtx_lock(&md->md_mtx); 111 md->open = 1; 112 mtx_unlock(&md->md_mtx); 113} 114 115void 116medium_close(struct wtap_medium *md) 117{ 118 119 mtx_lock(&md->md_mtx); 120 md->open = 0; 121 mtx_unlock(&md->md_mtx); 122} 123