]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/wtap/if_medium.c
Fix nda(4) PCIe link status output
[FreeBSD/FreeBSD.git] / sys / dev / wtap / if_medium.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
18  * NO WARRANTY
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.
30  *
31  * $FreeBSD$
32  */
33 #include "if_wtapvar.h"
34 #include "if_medium.h"
35
36 void
37 init_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
55 void
56 deinit_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
64 int
65 medium_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
89 struct packet *
90 medium_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
106 void
107 medium_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
115 void
116 medium_close(struct wtap_medium *md)
117 {
118
119         mtx_lock(&md->md_mtx);
120         md->open = 0;
121         mtx_unlock(&md->md_mtx);
122 }