IgH EtherCAT Master  1.5.2
debug.c
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
6 *
7 * This file is part of the IgH EtherCAT Master.
8 *
9 * The IgH EtherCAT Master is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2, as
11 * published by the Free Software Foundation.
12 *
13 * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with the IgH EtherCAT Master; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * ---
23 *
24 * The license mentioned above concerns the source code only. Using the
25 * EtherCAT technology and brand is only permitted in compliance with the
26 * industrial property and similar rights of Beckhoff Automation GmbH.
27 *
28 *****************************************************************************/
29
35/*****************************************************************************/
36
37#include <linux/version.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40
41#include "globals.h"
42#include "master.h"
43#include "debug.h"
44
45/*****************************************************************************/
46
47// net_device functions
48int ec_dbgdev_open(struct net_device *);
49int ec_dbgdev_stop(struct net_device *);
50int ec_dbgdev_tx(struct sk_buff *, struct net_device *);
51struct net_device_stats *ec_dbgdev_stats(struct net_device *);
52
53#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
56static const struct net_device_ops ec_dbg_netdev_ops =
57{
58 .ndo_open = ec_dbgdev_open,
59 .ndo_stop = ec_dbgdev_stop,
60 .ndo_start_xmit = ec_dbgdev_tx,
61 .ndo_get_stats = ec_dbgdev_stats,
62};
63#endif
64
65/*****************************************************************************/
66
75 ec_debug_t *dbg,
76 ec_device_t *device,
77 const char *name
78 )
79{
80 dbg->device = device;
81 dbg->registered = 0;
82 dbg->opened = 0;
83
84 memset(&dbg->stats, 0, sizeof(struct net_device_stats));
85
86#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
87 dbg->dev = alloc_netdev(sizeof(ec_debug_t *), name, NET_NAME_UNKNOWN, ether_setup);
88#else
89 dbg->dev = alloc_netdev(sizeof(ec_debug_t *), name, ether_setup);
90#endif
91 if (!(dbg->dev))
92 {
93 EC_MASTER_ERR(device->master, "Unable to allocate net_device"
94 " for debug object!\n");
95 return -ENODEV;
96 }
97
98 // initialize net_device
99#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
100 dbg->dev->netdev_ops = &ec_dbg_netdev_ops;
101#else
102 dbg->dev->open = ec_dbgdev_open;
103 dbg->dev->stop = ec_dbgdev_stop;
104 dbg->dev->hard_start_xmit = ec_dbgdev_tx;
105 dbg->dev->get_stats = ec_dbgdev_stats;
106#endif
107
108 // initialize private data
109 *((ec_debug_t **) netdev_priv(dbg->dev)) = dbg;
110
111 return 0;
112}
113
114/*****************************************************************************/
115
121 ec_debug_t *dbg
122 )
123{
125 free_netdev(dbg->dev);
126}
127
128/*****************************************************************************/
129
133 ec_debug_t *dbg,
134 const struct net_device *net_dev
135 )
136{
137 int result;
138
140
141 // use the Ethernet address of the physical device for the debug device
142#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
143 eth_hw_addr_set(dbg->dev, net_dev->dev_addr);
144#else
145 memcpy(dbg->dev->dev_addr, net_dev->dev_addr, ETH_ALEN);
146#endif
147
148 // connect the net_device to the kernel
149 if ((result = register_netdev(dbg->dev))) {
150 EC_MASTER_WARN(dbg->device->master, "Unable to register net_device:"
151 " error %i\n", result);
152 } else {
153 dbg->registered = 1;
154 }
155}
156
157/*****************************************************************************/
158
162 ec_debug_t *dbg
163 )
164{
165 if (dbg->registered) {
166 dbg->opened = 0;
167 dbg->registered = 0;
168 unregister_netdev(dbg->dev);
169 }
170}
171
172/*****************************************************************************/
173
177 ec_debug_t *dbg,
178 const uint8_t *data,
179 size_t size
180 )
181{
182 struct sk_buff *skb;
183
184 if (!dbg->opened)
185 return;
186
187 // allocate socket buffer
188 if (!(skb = dev_alloc_skb(size))) {
189 dbg->stats.rx_dropped++;
190 return;
191 }
192
193 // copy frame contents into socket buffer
194 memcpy(skb_put(skb, size), data, size);
195
196 // update device statistics
197 dbg->stats.rx_packets++;
198 dbg->stats.rx_bytes += size;
199
200 // pass socket buffer to network stack
201 skb->dev = dbg->dev;
202 skb->protocol = eth_type_trans(skb, dbg->dev);
203 skb->ip_summed = CHECKSUM_UNNECESSARY;
204 netif_rx(skb);
205}
206
207/******************************************************************************
208 * NET_DEVICE functions
209 *****************************************************************************/
210
216 struct net_device *dev
217 )
218{
219 ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
220 dbg->opened = 1;
221 EC_MASTER_INFO(dbg->device->master, "Debug interface %s opened.\n",
222 dev->name);
223 return 0;
224}
225
226/*****************************************************************************/
227
233 struct net_device *dev
234 )
235{
236 ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
237 dbg->opened = 0;
238 EC_MASTER_INFO(dbg->device->master, "Debug interface %s stopped.\n",
239 dev->name);
240 return 0;
241}
242
243/*****************************************************************************/
244
250 struct sk_buff *skb,
251 struct net_device *dev
252 )
253{
254 ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
255
256 dev_kfree_skb(skb);
257 dbg->stats.tx_dropped++;
258 return 0;
259}
260
261/*****************************************************************************/
262
267struct net_device_stats *ec_dbgdev_stats(
268 struct net_device *dev
269 )
270{
271 ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
272 return &dbg->stats;
273}
274
275/*****************************************************************************/
static const struct net_device_ops ec_dbg_netdev_ops
Device operations for debug interfaces.
Definition: debug.c:56
int ec_dbgdev_open(struct net_device *)
Opens the virtual network device.
Definition: debug.c:215
void ec_debug_clear(ec_debug_t *dbg)
Debug interface destructor.
Definition: debug.c:120
void ec_debug_unregister(ec_debug_t *dbg)
Unregister debug interface.
Definition: debug.c:161
int ec_dbgdev_stop(struct net_device *)
Stops the virtual network device.
Definition: debug.c:232
void ec_debug_register(ec_debug_t *dbg, const struct net_device *net_dev)
Register debug interface.
Definition: debug.c:132
int ec_dbgdev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: debug.c:249
int ec_debug_init(ec_debug_t *dbg, ec_device_t *device, const char *name)
Debug interface constructor.
Definition: debug.c:74
struct net_device_stats * ec_dbgdev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition: debug.c:267
void ec_debug_send(ec_debug_t *dbg, const uint8_t *data, size_t size)
Sends frame data to the interface.
Definition: debug.c:176
Network interface for debugging purposes.
Global definitions and macros.
EtherCAT master structure.
#define EC_MASTER_INFO(master, fmt, args...)
Convenience macro for printing master-specific information to syslog.
Definition: master.h:73
#define EC_MASTER_ERR(master, fmt, args...)
Convenience macro for printing master-specific errors to syslog.
Definition: master.h:85
#define EC_MASTER_WARN(master, fmt, args...)
Convenience macro for printing master-specific warnings to syslog.
Definition: master.h:97
Debugging network interface.
Definition: debug.h:47
struct net_device * dev
net_device for virtual ethernet device
Definition: debug.h:49
ec_device_t * device
Parent device.
Definition: debug.h:48
uint8_t opened
net_device is opened
Definition: debug.h:52
struct net_device_stats stats
device statistics
Definition: debug.h:50
uint8_t registered
net_device is opened
Definition: debug.h:51
EtherCAT device.
Definition: device.h:82
ec_master_t * master
EtherCAT master.
Definition: device.h:83