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 netif_napi_add(netdev, &vb->napi, vb_net_poll, 64);
206
207 skb_queue_head_init(&vb->captured_packets);
208 res = register_netdev(netdev);
209 if (res) {
210 dev_warn(&vb->pdev->dev,
211 "Failed to register network device %s.\n", board_name);
212 goto error_sw;
213 }
214
215 vb->netdev = netdev;
216
217 dev_dbg(&vb->pdev->dev,
218 "Created network device %s for debug.\n", board_name);
219 return 0;
220
221 error_sw:
222 if (netdev)
223 free_netdev(netdev);
224 return res;
225 }
226
227 void vb_net_unregister(struct voicebus *wc)
228 {
229 struct sk_buff *skb;
230 if (!wc->netdev)
231 return;
232
233 unregister_netdev(wc->netdev);
234
235 while ((skb = skb_dequeue(&wc->captured_packets)))
236 kfree_skb(skb);
237
238 free_netdev(wc->netdev);
239 wc->netdev = NULL;
240 }
241
242 /* Header format for the voicebus network interface. */
243 struct voicebus_net_hdr {
244 struct ethhdr ethhdr;
245 __be16 seq_num;
246 __be32 des0;
247 __be16 tag;
248 __be16 filler[4];
249 } __attribute__((packed));
250
251 static struct sk_buff *
252 vbb_to_skb(struct net_device *netdev, const void *vbb, const int tx,
253 const u32 des0, const u16 tag)
254 {
255 struct voicebus *vb = voicebus_from_netdev(netdev);
256 struct sk_buff *skb;
257 struct voicebus_net_hdr *hdr;
258 /* 0x88B5 is the local experimental ethertype */
259 const u16 VOICEBUS_ETHTYPE = 0x88b5;
260 const u8 BOARD_MAC[6] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
261 const u8 HOST_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
262
263 skb = netdev_alloc_skb(vb->netdev,
264 VOICEBUS_SFRAME_SIZE + sizeof(*hdr) + NET_IP_ALIGN);
265 if (!skb)
266 return NULL;
267
268 skb_reserve(skb, NET_IP_ALIGN);
269 skb->dev = netdev;
270 hdr = (struct voicebus_net_hdr *)skb_put(skb, VOICEBUS_SFRAME_SIZE +
271 sizeof(*hdr));
272 /* Fill in the source and destination mac addresses appropriately
273 * depending on whether this is a packet we are transmitting or a packet
274 * that we have received. */
275 if (tx) {
276 memcpy(hdr->ethhdr.h_dest, BOARD_MAC, sizeof(BOARD_MAC));
277 memcpy(hdr->ethhdr.h_source, HOST_MAC, sizeof(HOST_MAC));
278 hdr->seq_num = cpu_to_be16(atomic_inc_return(
279 &vb->tx_seqnum));
280 } else {
281 memcpy(hdr->ethhdr.h_dest, HOST_MAC, sizeof(HOST_MAC));
282 memcpy(hdr->ethhdr.h_source, BOARD_MAC, sizeof(BOARD_MAC));
283 hdr->seq_num = cpu_to_be16(atomic_inc_return(
284 &vb->rx_seqnum));
285 }
286 memset(hdr->filler, 0, sizeof(hdr->filler));
287 hdr->des0 = cpu_to_be32(des0);
288 hdr->tag = cpu_to_be16(tag);
289 hdr->ethhdr.h_proto = htons(VOICEBUS_ETHTYPE);
290 /* copy the rest of the packet. */
291 memcpy(skb->data + sizeof(*hdr), vbb, VOICEBUS_SFRAME_SIZE);
292 skb->protocol = eth_type_trans(skb, netdev);
293
294 return skb;
295 }
296
297 /**
298 * vb_net_capture_cmd - Send a vbb to the network stack.
299 * @vb: Interface card received the command.
300 * @vbb: Voicebus buffer to pass up..
301 * @tx: 1 if this is a vbb that the driver is sending to the card.
302 *
303 */
304 void vb_net_capture_vbb(struct voicebus *vb, const void *vbb, const int tx,
305 const u32 des0, const u16 tag)
306 {
307 struct sk_buff *skb;
308 struct net_device *netdev = vb->netdev;
309 const int MAX_CAPTURED_PACKETS = 5000;
310
311 if (!netdev)
312 return;
313
314 /* If the interface isn't up, we don't need to capture the packet. */
315 if (!(netdev->flags & IFF_UP))
316 return;
317
318 if (skb_queue_len(&vb->captured_packets) > MAX_CAPTURED_PACKETS) {
319 WARN_ON_ONCE(1);
320 return;
321 }
322
323 skb = vbb_to_skb(netdev, vbb, tx, des0, tag);
324 if (!skb)
325 return;
326
327 skb_queue_tail(&vb->captured_packets, skb);
328 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
329 netif_rx_schedule(netdev, &vb->napi);
330 # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
331 netif_rx_schedule(&vb->napi);
332 # else
333 napi_schedule(&vb->napi);
334 # endif
335 return;
336 }
337
338 #endif