[etherlab-users] Bringing up the ethercat debug network interface

Florian Pose fp at igh-essen.com
Wed Nov 5 15:51:11 CET 2008


On Wed, Nov 05, 2008 at 08:58:14AM -0500, Joe Brandt wrote:
> This works now.
> 
> The problem was that for some reason the ecdbgm0 interface
> ethernet address is not set correctly after the module is loaded.
> If I first set the address, then bring the interface up, it works as
> expected.

A solution would be to set the Ethernet address of the debug interface
to that of the physical device, since this isn't used for an interface
anyway. Please try the attached patch.

-- 
Best regards,
Florian Pose

http://etherlab.org
-------------- next part --------------
Index: master/debug.c
===================================================================
--- master/debug.c	(Revision 1552)
+++ master/debug.c	(Arbeitskopie)
@@ -54,19 +54,18 @@
 
 /*****************************************************************************/
 
-/**
-   Debug constructor.
-   Initializes the debug object, creates a net_device and registeres it.
-*/
-
+/** Debug interface constructor.
+ *
+ * Initializes the debug object, creates a net_device and registeres it.
+ */
 int ec_debug_init(
         ec_debug_t *dbg, /**< debug object */
         const char *name /**< interface name */
         )
 {
-    int result;
+    dbg->registered = 0;
+    dbg->opened = 0;
 
-    dbg->opened = 0;
     memset(&dbg->stats, 0, sizeof(struct net_device_stats));
 
     if (!(dbg->dev =
@@ -84,46 +83,73 @@
     // initialize private data
     *((ec_debug_t **) netdev_priv(dbg->dev)) = dbg;
 
-    // connect the net_device to the kernel
-    if ((result = register_netdev(dbg->dev))) {
-        EC_ERR("Unable to register net_device: error %i\n", result);
-        goto out_free;
-    }
-
     return 0;
 
- out_free:
-    free_netdev(dbg->dev);
-    dbg->dev = NULL;
  out_return:
     return -1;
 }
 
 /*****************************************************************************/
 
-/**
-   Debug destructor.
-   Unregisteres the net_device and frees allocated memory.
-*/
+/** Debug interface destructor.
+ *
+ * Unregisters the net_device and frees allocated memory.
+ */
+void ec_debug_clear(
+        ec_debug_t *dbg /**< debug object */
+        )
+{
+    ec_debug_unregister(dbg);
+    free_netdev(dbg->dev);
+}
 
-void ec_debug_clear(ec_debug_t *dbg /**< debug object */)
+/*****************************************************************************/
+
+/** Register debug interface.
+ */
+void ec_debug_register(
+        ec_debug_t *dbg, /**< debug object */
+        const struct net_device *net_dev /**< 'Real' Ethernet device. */
+        )
 {
-    if (dbg->dev) {
+    int result;
+
+    ec_debug_unregister(dbg);
+
+    // use the Ethernet address of the physical device for the debug device
+    memcpy(dbg->dev->dev_addr, net_dev->dev_addr, ETH_ALEN);
+
+    // connect the net_device to the kernel
+    if ((result = register_netdev(dbg->dev))) {
+        EC_WARN("Unable to register net_device: error %i\n", result);
+    } else {
+        dbg->registered = 1;
+    }
+}
+
+/*****************************************************************************/
+
+/** Unregister debug interface.
+ */
+void ec_debug_unregister(
+        ec_debug_t *dbg /**< debug object */
+        )
+{
+    if (dbg->registered) {
         unregister_netdev(dbg->dev);
-        free_netdev(dbg->dev);
+        dbg->registered = 0;
     }
 }
 
 /*****************************************************************************/
 
-/**
-   Sends frame data to the interface.
-*/
-
-void ec_debug_send(ec_debug_t *dbg, /**< debug object */
-                   const uint8_t *data, /**< frame data */
-                   size_t size /**< size of the frame data */
-                   )
+/** Sends frame data to the interface.
+ */
+void ec_debug_send(
+        ec_debug_t *dbg, /**< debug object */
+        const uint8_t *data, /**< frame data */
+        size_t size /**< size of the frame data */
+        )
 {
     struct sk_buff *skb;
 
@@ -153,11 +179,11 @@
  *  NET_DEVICE functions
  *****************************************************************************/
 
-/**
-   Opens the virtual network device.
-*/
-
-int ec_dbgdev_open(struct net_device *dev /**< debug net_device */)
+/** Opens the virtual network device.
+ */
+int ec_dbgdev_open(
+        struct net_device *dev /**< debug net_device */
+        )
 {
     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
     dbg->opened = 1;
@@ -167,11 +193,11 @@
 
 /*****************************************************************************/
 
-/**
-   Stops the virtual network device.
-*/
-
-int ec_dbgdev_stop(struct net_device *dev /**< debug net_device */)
+/** Stops the virtual network device.
+ */
+int ec_dbgdev_stop(
+        struct net_device *dev /**< debug net_device */
+        )
 {
     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
     dbg->opened = 0;
@@ -181,13 +207,12 @@
 
 /*****************************************************************************/
 
-/**
-   Transmits data via the virtual network device.
-*/
-
-int ec_dbgdev_tx(struct sk_buff *skb, /**< transmit socket buffer */
-                 struct net_device *dev /**< EoE net_device */
-                 )
+/** Transmits data via the virtual network device.
+ */
+int ec_dbgdev_tx(
+        struct sk_buff *skb, /**< transmit socket buffer */
+        struct net_device *dev /**< EoE net_device */
+        )
 {
     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
 
@@ -198,12 +223,11 @@
 
 /*****************************************************************************/
 
-/**
-   Gets statistics about the virtual network device.
-*/
-
-struct net_device_stats *ec_dbgdev_stats(struct net_device *dev
-                                         /**< debug net_device */)
+/** Gets statistics about the virtual network device.
+ */
+struct net_device_stats *ec_dbgdev_stats(
+        struct net_device *dev /**< debug net_device */
+        )
 {
     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
     return &dbg->stats;
Index: master/debug.h
===================================================================
--- master/debug.h	(Revision 1552)
+++ master/debug.h	(Arbeitskopie)
@@ -50,6 +50,7 @@
 {
     struct net_device *dev; /**< net_device for virtual ethernet device */
     struct net_device_stats stats; /**< device statistics */
+    uint8_t registered; /**< net_device is opened */
     uint8_t opened; /**< net_device is opened */
 }
 ec_debug_t;
@@ -58,6 +59,8 @@
 
 int ec_debug_init(ec_debug_t *, const char *);
 void ec_debug_clear(ec_debug_t *);
+void ec_debug_register(ec_debug_t *, const struct net_device *);
+void ec_debug_unregister(ec_debug_t *);
 void ec_debug_send(ec_debug_t *, const uint8_t *, size_t);
 
 /*****************************************************************************/
Index: master/device.c
===================================================================
--- master/device.c	(Revision 1552)
+++ master/device.c	(Arbeitskopie)
@@ -170,6 +170,10 @@
         eth = (struct ethhdr *) (device->tx_skb[i]->data);
         memcpy(eth->h_source, net_dev->dev_addr, ETH_ALEN);
     }
+
+#ifdef EC_DEBUG_IF
+    ec_debug_register(&device->dbg, net_dev);
+#endif
 }
 
 /*****************************************************************************/
@@ -182,6 +186,10 @@
 {
     unsigned int i;
 
+#ifdef EC_DEBUG_IF
+    ec_debug_unregister(&device->dbg);
+#endif
+
     device->dev = NULL;
     device->poll = NULL;
     device->module = NULL;


More information about the Etherlab-users mailing list