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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
108 static int vb_net_poll(struct net_device *netdev, int *budget)
109 {
110 struct voicebus *vb = voicebus_from_netdev(netdev);
111 int count = 0;
112 int quota = min(netdev->quota, *budget);
113
114 count = vb_net_receive(vb, quota);
115
116 *budget -= count;
117 netdev->quota -= count;
118
119 if (!skb_queue_len(&vb->captured_packets)) {
120 netif_rx_complete(netdev);
121 return 0;
122 } else {
123 return -1;
124 }
125 }
126 #else
127 static int vb_net_poll(struct napi_struct *napi, int budget)
128 {
129 struct voicebus *vb = container_of(napi, struct voicebus, napi);
130 int count;
131
132 count = vb_net_receive(vb, budget);
133
134 if (!skb_queue_len(&vb->captured_packets)) {
135 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
136 netif_rx_complete(vb->netdev, &vb->napi);
137 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
138 netif_rx_complete(&vb->napi);
139 #else
140 napi_complete(&vb->napi);
141 #endif
142 }
143 return count;
144 }
145 #endif
146
147 static void vb_net_set_multi(struct net_device *netdev)
148 {
149 struct voicebus *vb = voicebus_from_netdev(netdev);
150 dev_dbg(&vb->pdev->dev, "%s promiscuity:%d\n",
151 __func__, netdev->promiscuity);
152 }
153
154 static int vb_net_up(struct net_device *netdev)
155 {
156 struct voicebus *vb = voicebus_from_netdev(netdev);
157 dev_dbg(&vb->pdev->dev, "%s\n", __func__);
158 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
159 netif_poll_enable(netdev);
160 #else
161 napi_enable(&vb->napi);
162 #endif
163 return 0;
164 }
165
166 static int vb_net_down(struct net_device *netdev)
167 {
168 struct voicebus *vb = voicebus_from_netdev(netdev);
169 dev_dbg(&vb->pdev->dev, "%s\n", __func__);
170 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
171 netif_poll_disable(netdev);
172 #else
173 napi_disable(&vb->napi);
174 #endif
175 return 0;
176 }
177
178 static struct net_device_stats *
179 vb_net_get_stats(struct net_device *netdev)
180 {
181 struct voicebus *vb = voicebus_from_netdev(netdev);
182 return &vb->net_stats;
183 }
184
185 #ifdef HAVE_NET_DEVICE_OPS
186 static const struct net_device_ops vb_netdev_ops = {
187 .ndo_set_multicast_list = &vb_net_set_multi,
188 .ndo_open = &vb_net_up,
189 .ndo_stop = &vb_net_down,
190 .ndo_start_xmit = &vb_net_hard_start_xmit,
191 .ndo_get_stats = &vb_net_get_stats,
192 };
193 #endif
194
195 /**
196 * vb_net_register - Register a new network interface.
197 * @vb: voicebus card to register the interface for.
198 *
199 * The network interface is primarily used for debugging in order to watch the
200 * traffic between the transcoder and the host.
201 *
202 */
203 int vb_net_register(struct voicebus *vb, const char *board_name)
204 {
205 int res;
206 struct net_device *netdev;
207 struct voicebus_netdev_priv *priv;
208 const char our_mac[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
209
210 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
211 netdev = alloc_netdev(sizeof(*priv), board_name,
212 NET_NAME_UNKNOWN, ether_setup);
213 #else
214 netdev = alloc_netdev(sizeof(*priv), board_name, ether_setup);
215 #endif
216
217 if (!netdev)
218 return -ENOMEM;
219 priv = netdev_priv(netdev);
220 priv->vb = vb;
221 memcpy(netdev->dev_addr, our_mac, sizeof(our_mac));
222 # ifdef HAVE_NET_DEVICE_OPS
223 netdev->netdev_ops = &vb_netdev_ops;
224 # else
225 netdev->set_multicast_list = vb_net_set_multi;
226 netdev->open = vb_net_up;
227 netdev->stop = vb_net_down;
228 netdev->hard_start_xmit = vb_net_hard_start_xmit;
229 netdev->get_stats = vb_net_get_stats;
230 # endif
231
232 netdev->promiscuity = 0;
233 netdev->flags |= IFF_NOARP;
234 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
235 netdev->poll = vb_net_poll;
236 netdev->weight = 64;
237 # else
238 netif_napi_add(netdev, &vb->napi, vb_net_poll, 64);
239 # endif
240
241 skb_queue_head_init(&vb->captured_packets);
242 res = register_netdev(netdev);
243 if (res) {
244 dev_warn(&vb->pdev->dev,
245 "Failed to register network device %s.\n", board_name);
246 goto error_sw;
247 }
248
249 vb->netdev = netdev;
250
251 dev_dbg(&vb->pdev->dev,
252 "Created network device %s for debug.\n", board_name);
253 return 0;
254
255 error_sw:
256 if (netdev)
257 free_netdev(netdev);
258 return res;
259 }
260
261 void vb_net_unregister(struct voicebus *wc)
262 {
263 struct sk_buff *skb;
264 if (!wc->netdev)
265 return;
266
267 unregister_netdev(wc->netdev);
268
269 while ((skb = skb_dequeue(&wc->captured_packets)))
270 kfree_skb(skb);
271
272 free_netdev(wc->netdev);
273 wc->netdev = NULL;
274 }
275
276 /* Header format for the voicebus network interface. */
277 struct voicebus_net_hdr {
278 struct ethhdr ethhdr;
279 __be16 seq_num;
280 __be32 des0;
281 __be16 tag;
282 __be16 filler[4];
283 } __attribute__((packed));
284
285 static struct sk_buff *
286 vbb_to_skb(struct net_device *netdev, const void *vbb, const int tx,
287 const u32 des0, const u16 tag)
288 {
289 struct voicebus *vb = voicebus_from_netdev(netdev);
290 struct sk_buff *skb;
291 struct voicebus_net_hdr *hdr;
292 /* 0x88B5 is the local experimental ethertype */
293 const u16 VOICEBUS_ETHTYPE = 0x88b5;
294 const u8 BOARD_MAC[6] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
295 const u8 HOST_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
296
297 skb = netdev_alloc_skb(vb->netdev,
298 VOICEBUS_SFRAME_SIZE + sizeof(*hdr) + NET_IP_ALIGN);
299 if (!skb)
300 return NULL;
301
302 skb_reserve(skb, NET_IP_ALIGN);
303 skb->dev = netdev;
304 hdr = (struct voicebus_net_hdr *)skb_put(skb, VOICEBUS_SFRAME_SIZE +
305 sizeof(*hdr));
306 /* Fill in the source and destination mac addresses appropriately
307 * depending on whether this is a packet we are transmitting or a packet
308 * that we have received. */
309 if (tx) {
310 memcpy(hdr->ethhdr.h_dest, BOARD_MAC, sizeof(BOARD_MAC));
311 memcpy(hdr->ethhdr.h_source, HOST_MAC, sizeof(HOST_MAC));
312 hdr->seq_num = cpu_to_be16(atomic_inc_return(
313 &vb->tx_seqnum));
314 } else {
315 memcpy(hdr->ethhdr.h_dest, HOST_MAC, sizeof(HOST_MAC));
316 memcpy(hdr->ethhdr.h_source, BOARD_MAC, sizeof(BOARD_MAC));
317 hdr->seq_num = cpu_to_be16(atomic_inc_return(
318 &vb->rx_seqnum));
319 }
320 memset(hdr->filler, 0, sizeof(hdr->filler));
321 hdr->des0 = cpu_to_be32(des0);
322 hdr->tag = cpu_to_be16(tag);
323 hdr->ethhdr.h_proto = htons(VOICEBUS_ETHTYPE);
324 /* copy the rest of the packet. */
325 memcpy(skb->data + sizeof(*hdr), vbb, VOICEBUS_SFRAME_SIZE);
326 skb->protocol = eth_type_trans(skb, netdev);
327
328 return skb;
329 }
330
331 /**
332 * vb_net_capture_cmd - Send a vbb to the network stack.
333 * @vb: Interface card received the command.
334 * @vbb: Voicebus buffer to pass up..
335 * @tx: 1 if this is a vbb that the driver is sending to the card.
336 *
337 */
338 void vb_net_capture_vbb(struct voicebus *vb, const void *vbb, const int tx,
339 const u32 des0, const u16 tag)
340 {
341 struct sk_buff *skb;
342 struct net_device *netdev = vb->netdev;
343 const int MAX_CAPTURED_PACKETS = 5000;
344
345 if (!netdev)
346 return;
347
348 /* If the interface isn't up, we don't need to capture the packet. */
349 if (!(netdev->flags & IFF_UP))
350 return;
351
352 if (skb_queue_len(&vb->captured_packets) > MAX_CAPTURED_PACKETS) {
353 WARN_ON_ONCE(1);
354 return;
355 }
356
357 skb = vbb_to_skb(netdev, vbb, tx, des0, tag);
358 if (!skb)
359 return;
360
361 skb_queue_tail(&vb->captured_packets, skb);
362 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
363 netif_rx_schedule(netdev);
364 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
365 netif_rx_schedule(netdev, &vb->napi);
366 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
367 netif_rx_schedule(&vb->napi);
368 # else
369 napi_schedule(&vb->napi);
370 # endif
371 return;
372 }
373
374 #endif