Debian Codegraph @

drivers/dahdi/voicebus/voicebus_net.c

raw symbols SCIP
1 /*
2 * Voicebus network debug interface
3 *
4 * Written by Shaun Ruffell <[email protected]>
5 *
6 * Copyright (C) 2010-2011 Digium, Inc.
7 *
8 * All rights reserved.
9
10 * VoiceBus is a registered trademark of Digium.
11 *
12 */
13
14 /*
15 * See http://www.asterisk.org for more information about
16 * the Asterisk project. Please do not directly contact
17 * any of the maintainers of this project for assistance;
18 * the project provides a web site, mailing lists and IRC
19 * channels for your use.
20 *
21 * This program is free software, distributed under the terms of
22 * the GNU General Public License Version 2 as published by the
23 * Free Software Foundation. See the LICENSE file included with
24 * this program for more details.
25 */
26
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31
32 #include <dahdi/kernel.h>
33
34 #include "voicebus.h"
35 #include "voicebus_net.h"
36
37 #ifdef VOICEBUS_NET_DEBUG
38
39 struct voicebus_netdev_priv {
40 struct voicebus *vb;
41 };
42
43 static inline struct voicebus *
44 voicebus_from_netdev(struct net_device *netdev)
45 {
46 struct voicebus_netdev_priv *priv;
47 priv = netdev_priv(netdev);
48 return priv->vb;
49 }
50
51 static void *
52 skb_to_vbb(struct voicebus *vb, struct sk_buff *skb)
53 {
54 int res;
55 struct vbb *vbb;
56 const int COMMON_HEADER = 30;
57 dma_addr_t dma_addr;
58
59 if (skb->len != (VOICEBUS_SFRAME_SIZE + COMMON_HEADER)) {
60 dev_warn(&vb->pdev->dev, "Packet of length %d is not the "
61 "required %d.\n", skb->len,
62 VOICEBUS_SFRAME_SIZE + COMMON_HEADER);
63 return NULL;
64 }
65
66 vbb = dma_pool_alloc(vb->pool, GFP_KERNEL, &dma_addr);
67 if (!vbb)
68 return NULL;
69
70 vbb->dma_addr = dma_addr;
71 res = skb_copy_bits(skb, COMMON_HEADER, vbb, VOICEBUS_SFRAME_SIZE);
72 if (res) {
73 dev_warn(&vb->pdev->dev, "Failed call to skb_copy_bits.\n");
74 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
75 return NULL;
76 }
77 return vbb;
78 }
79
80 static int
81 vb_net_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
82 {
83 struct voicebus *vb = voicebus_from_netdev(netdev);
84 void *vbb;
85
86 vbb = skb_to_vbb(vb, skb);
87 if (vbb)
88 voicebus_transmit(vb, vbb);
89
90 dev_kfree_skb_any(skb);
91 return NETDEV_TX_OK;
92 }
93
94 static int vb_net_receive(struct voicebus *vb, int max)
95 {
96 int count = 0;
97 struct sk_buff *skb;
98 WARN_ON(0 == max);
99 while ((skb = skb_dequeue(&vb->captured_packets))) {
100 netif_receive_skb(skb);
101 if (++count >= max)
102 break;
103 }
104 return count;
105 }
106
107 static int vb_net_poll(struct napi_struct *napi, int budget)
108 {
109 struct voicebus *vb = container_of(napi, struct voicebus, napi);
110 int count;
111
112 count = vb_net_receive(vb, budget);
113
114 if (!skb_queue_len(&vb->captured_packets)) {
115 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
116 netif_rx_complete(vb->netdev, &vb->napi);
117 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
118 netif_rx_complete(&vb->napi);
119 #else
120 napi_complete(&vb->napi);
121 #endif
122 }
123 return count;
124 }
125
126 static void vb_net_set_multi(struct net_device *netdev)
127 {
128 struct voicebus *vb = voicebus_from_netdev(netdev);
129 dev_dbg(&vb->pdev->dev, "%s promiscuity:%d\n",
130 __func__, netdev->promiscuity);
131 }
132
133 static int vb_net_up(struct net_device *netdev)
134 {
135 struct voicebus *vb = voicebus_from_netdev(netdev);
136 dev_dbg(&vb->pdev->dev, "%s\n", __func__);
137 napi_enable(&vb->napi);
138 return 0;
139 }
140
141 static int vb_net_down(struct net_device *netdev)
142 {
143 struct voicebus *vb = voicebus_from_netdev(netdev);
144 dev_dbg(&vb->pdev->dev, "%s\n", __func__);
145 napi_disable(&vb->napi);
146 return 0;
147 }
148
149 static struct net_device_stats *
150 vb_net_get_stats(struct net_device *netdev)
151 {
152 struct voicebus *vb = voicebus_from_netdev(netdev);
153 return &vb->net_stats;
154 }
155
156 #ifdef HAVE_NET_DEVICE_OPS
157 static const struct net_device_ops vb_netdev_ops = {
158 .ndo_set_multicast_list = &vb_net_set_multi,
159 .ndo_open = &vb_net_up,
160 .ndo_stop = &vb_net_down,
161 .ndo_start_xmit = &vb_net_hard_start_xmit,
162 .ndo_get_stats = &vb_net_get_stats,
163 };
164 #endif
165
166 /**
167 * vb_net_register - Register a new network interface.
168 * @vb: voicebus card to register the interface for.
169 *
170 * The network interface is primarily used for debugging in order to watch the
171 * traffic between the transcoder and the host.
172 *
173 */
174 int vb_net_register(struct voicebus *vb, const char *board_name)
175 {
176 int res;
177 struct net_device *netdev;
178 struct voicebus_netdev_priv *priv;
179 const char our_mac[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
180
181 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
182 netdev = alloc_netdev(sizeof(*priv), board_name,
183 NET_NAME_UNKNOWN, ether_setup);
184 #else
185 netdev = alloc_netdev(sizeof(*priv), board_name, ether_setup);
186 #endif
187
188 if (!netdev)
189 return -ENOMEM;
190 priv = netdev_priv(netdev);
191 priv->vb = vb;
192 memcpy(netdev->dev_addr, our_mac, sizeof(our_mac));
193 # ifdef HAVE_NET_DEVICE_OPS
194 netdev->netdev_ops = &vb_netdev_ops;
195 # else
196 netdev->set_multicast_list = vb_net_set_multi;
197 netdev->open = vb_net_up;
198 netdev->stop = vb_net_down;
199 netdev->hard_start_xmit = vb_net_hard_start_xmit;
200 netdev->get_stats = vb_net_get_stats;
201 # endif
202
203 netdev->promiscuity = 0;
204 netdev->flags |= IFF_NOARP;
205 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
206 /* 64 is NAPI_POLL_WEIGHT, i.e. the default */
207 netif_napi_add(netdev, &vb->napi, vb_net_poll);
208 #else
209 netif_napi_add(netdev, &vb->napi, vb_net_poll, 64);
210 #endif
211
212 skb_queue_head_init(&vb->captured_packets);
213 res = register_netdev(netdev);
214 if (res) {
215 dev_warn(&vb->pdev->dev,
216 "Failed to register network device %s.\n", board_name);
217 goto error_sw;
218 }
219
220 vb->netdev = netdev;
221
222 dev_dbg(&vb->pdev->dev,
223 "Created network device %s for debug.\n", board_name);
224 return 0;
225
226 error_sw:
227 if (netdev)
228 free_netdev(netdev);
229 return res;
230 }
231
232 void vb_net_unregister(struct voicebus *wc)
233 {
234 struct sk_buff *skb;
235 if (!wc->netdev)
236 return;
237
238 unregister_netdev(wc->netdev);
239
240 while ((skb = skb_dequeue(&wc->captured_packets)))
241 kfree_skb(skb);
242
243 free_netdev(wc->netdev);
244 wc->netdev = NULL;
245 }
246
247 /* Header format for the voicebus network interface. */
248 struct voicebus_net_hdr {
249 struct ethhdr ethhdr;
250 __be16 seq_num;
251 __be32 des0;
252 __be16 tag;
253 __be16 filler[4];
254 } __attribute__((packed));
255
256 static struct sk_buff *
257 vbb_to_skb(struct net_device *netdev, const void *vbb, const int tx,
258 const u32 des0, const u16 tag)
259 {
260 struct voicebus *vb = voicebus_from_netdev(netdev);
261 struct sk_buff *skb;
262 struct voicebus_net_hdr *hdr;
263 /* 0x88B5 is the local experimental ethertype */
264 const u16 VOICEBUS_ETHTYPE = 0x88b5;
265 const u8 BOARD_MAC[6] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
266 const u8 HOST_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
267
268 skb = netdev_alloc_skb(vb->netdev,
269 VOICEBUS_SFRAME_SIZE + sizeof(*hdr) + NET_IP_ALIGN);
270 if (!skb)
271 return NULL;
272
273 skb_reserve(skb, NET_IP_ALIGN);
274 skb->dev = netdev;
275 hdr = (struct voicebus_net_hdr *)skb_put(skb, VOICEBUS_SFRAME_SIZE +
276 sizeof(*hdr));
277 /* Fill in the source and destination mac addresses appropriately
278 * depending on whether this is a packet we are transmitting or a packet
279 * that we have received. */
280 if (tx) {
281 memcpy(hdr->ethhdr.h_dest, BOARD_MAC, sizeof(BOARD_MAC));
282 memcpy(hdr->ethhdr.h_source, HOST_MAC, sizeof(HOST_MAC));
283 hdr->seq_num = cpu_to_be16(atomic_inc_return(
284 &vb->tx_seqnum));
285 } else {
286 memcpy(hdr->ethhdr.h_dest, HOST_MAC, sizeof(HOST_MAC));
287 memcpy(hdr->ethhdr.h_source, BOARD_MAC, sizeof(BOARD_MAC));
288 hdr->seq_num = cpu_to_be16(atomic_inc_return(
289 &vb->rx_seqnum));
290 }
291 memset(hdr->filler, 0, sizeof(hdr->filler));
292 hdr->des0 = cpu_to_be32(des0);
293 hdr->tag = cpu_to_be16(tag);
294 hdr->ethhdr.h_proto = htons(VOICEBUS_ETHTYPE);
295 /* copy the rest of the packet. */
296 memcpy(skb->data + sizeof(*hdr), vbb, VOICEBUS_SFRAME_SIZE);
297 skb->protocol = eth_type_trans(skb, netdev);
298
299 return skb;
300 }
301
302 /**
303 * vb_net_capture_cmd - Send a vbb to the network stack.
304 * @vb: Interface card received the command.
305 * @vbb: Voicebus buffer to pass up..
306 * @tx: 1 if this is a vbb that the driver is sending to the card.
307 *
308 */
309 void vb_net_capture_vbb(struct voicebus *vb, const void *vbb, const int tx,
310 const u32 des0, const u16 tag)
311 {
312 struct sk_buff *skb;
313 struct net_device *netdev = vb->netdev;
314 const int MAX_CAPTURED_PACKETS = 5000;
315
316 if (!netdev)
317 return;
318
319 /* If the interface isn't up, we don't need to capture the packet. */
320 if (!(netdev->flags & IFF_UP))
321 return;
322
323 if (skb_queue_len(&vb->captured_packets) > MAX_CAPTURED_PACKETS) {
324 WARN_ON_ONCE(1);
325 return;
326 }
327
328 skb = vbb_to_skb(netdev, vbb, tx, des0, tag);
329 if (!skb)
330 return;
331
332 skb_queue_tail(&vb->captured_packets, skb);
333 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
334 netif_rx_schedule(netdev, &vb->napi);
335 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
336 netif_rx_schedule(&vb->napi);
337 # else
338 napi_schedule(&vb->napi);
339 # endif
340 return;
341 }
342
343 #endif