can_eth_gw Gateway Module  0.1
A bidirectional CAN to Ethernet Gateway (Kernel Module)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
ce_gw_dev.c File Reference

Control Area Network - Ethernet - Gateway - Device. More...

#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <uapi/linux/can.h>
#include "ce_gw_dev.h"
#include "ce_gw_main.h"
#include <asm-generic/errno-base.h>
#include <asm-generic/errno.h>
Include dependency graph for ce_gw_dev.c:

Go to the source code of this file.

Data Structures

struct  ce_gw_dev_list
 internal list of all registered and allocated devices. More...
 
static struct kmem_cache
*ce_gw_dev_cache 
__read_mostly
 
static struct net_device_ops ce_gw_ops
 Defined Functions of Ethernet device. More...
 
 HLIST_HEAD (ce_gw_dev_allocated)
 
 HLIST_HEAD (ce_gw_dev_registered)
 
int ce_gw_dev_open (struct net_device *dev)
 called by the OS on device up More...
 
int ce_gw_dev_stop (struct net_device *dev)
 called by the OS on device down More...
 
static int ce_gw_dev_start_xmit (struct sk_buff *skb, struct net_device *dev)
 called by the OS if a package is sent to the device More...
 
int ce_gw_dev_init (struct net_device *dev)
 called by the OS on device registered More...
 
int ce_gw_is_allocated_dev (struct net_device *eth_dev)
 check if the param eth_dev is allocated by this module More...
 
int ce_gw_is_registered_dev (struct net_device *eth_dev)
 check if the param eth_dev is registered by this module More...
 
int ce_gw_has_min_mtu (struct net_device *dev, enum ce_gw_type type, u32 flags)
 checks if the given device has enough mtu to use the type and flags More...
 
void ce_gw_dev_job_src_add (struct ce_gw_job *job)
 Adds an pointer to the net_device internal list where it is the src. More...
 
void ce_gw_dev_job_dst_add (struct ce_gw_job *job)
 Adds an pointer to the net_device internal list where it is the dst. More...
 
int ce_gw_dev_job_add (struct net_device *eth_dev, struct ce_gw_job *job)
 Adds an pointer to the net_device internal list where it is part of. More...
 
void ce_gw_dev_job_remove (struct ce_gw_job *job)
 Removes the pointer to param job from the list in ethernet net_device. More...
 
struct net_device * ce_gw_dev_alloc (char *dev_name)
 
void ce_gw_dev_free (struct net_device *eth_dev)
 Free virtual ethernet device and remove from internal lists. More...
 
void ce_gw_dev_setup (struct net_device *dev, enum ce_gw_type type, __u32 flags)
 Sets the default attributes for the Gateway Ethernet device. More...
 
struct net_device * ce_gw_dev_create (enum ce_gw_type type, __u32 flags, char *dev_name)
 
int ce_gw_dev_register (struct net_device *eth_dev)
 Register a virtual ethernet device on the OS. More...
 
void ce_gw_dev_unregister (struct net_device *eth_dev)
 Unregister virtual ethernet device and remove from internal lists. More...
 
int ce_gw_dev_init_module (void)
 Initialise all objects, wich are needed by the other functions. More...
 
void ce_gw_dev_cleanup (void)
 Deleting all objects created by ce_gw_dev_init_module() and all devices. More...
 

Detailed Description

Control Area Network - Ethernet - Gateway - Device.

Author
Fabian Raab (fabia.nosp@m.n.ra.nosp@m.ab@tu.nosp@m.m.de)
Date
May, 2013

Definition in file ce_gw_dev.c.

Function Documentation

struct net_device* ce_gw_dev_alloc ( char *  dev_name)
read

Definition at line 279 of file ce_gw_dev.c.

References ce_gw_dev_list::dev, ce_gw_job_info::job_dst, ce_gw_job_info::job_src, and ce_gw_dev_list::list_alloc.

Referenced by ce_gw_dev_create().

279  {
280  pr_debug("ce_gw_dev: Alloc Device\n");
281  struct net_device *dev;
282 
283  dev = alloc_netdev(sizeof(struct ce_gw_job_info),
284  dev_name, ether_setup);
285  if (dev == NULL) {
286  pr_err("ce_gw_dev: Error allocation etherdev.");
287  goto ce_gw_dev_create_error;
288  }
289 
290  /* initialize private field */
291  struct ce_gw_job_info *priv = netdev_priv(dev);
292  memset(priv, 0, sizeof(struct ce_gw_job_info));
293  priv->job_src.first = NULL;
294  priv->job_dst.first = NULL;
295 
296  /* create list entry and add */
297  struct ce_gw_dev_list *dl;
298  dl = kmem_cache_alloc(ce_gw_dev_cache, GFP_KERNEL);
299  if (dl == NULL) {
300  pr_err("ce_gw_dev: cache alloc failed");
301  goto ce_gw_dev_create_error_cache;
302  }
303 
304  dl->dev = dev;
305 
306  hlist_add_head_rcu(&dl->list_alloc, &ce_gw_dev_allocated);
307 
308  return dev;
309 
310 ce_gw_dev_create_error_cache:
311  kmem_cache_free(ce_gw_dev_cache, dl);
312 
313 ce_gw_dev_create_error:
314  free_netdev(dev);
315  return NULL;
316 }

Here is the caller graph for this function:

struct net_device* ce_gw_dev_create ( enum ce_gw_type  type,
__u32  flags,
char *  dev_name 
)
read

Definition at line 381 of file ce_gw_dev.c.

References ce_gw_dev_alloc(), and ce_gw_dev_setup().

Referenced by ce_gw_netlink_add().

382  {
383  struct net_device *dev;
384 
385  dev = ce_gw_dev_alloc(dev_name);
386 
387  ce_gw_dev_setup(dev, type, flags);
388 
389  return dev;
390 }

Here is the call graph for this function:

Here is the caller graph for this function:

int ce_gw_has_min_mtu ( struct net_device *  dev,
enum ce_gw_type  type,
u32  flags 
)

checks if the given device has enough mtu to use the type and flags

Parameters
devThe net_device which schould be checked
typeThe type which sould be compared to
flagsneeded dor the CE_GW_F_CAN_FD flag, to check if the type is CANfd capable
Return values
trueThe mtu of dev is bigger then the nedded mtu by the type
falseif not

Definition at line 213 of file ce_gw_dev.c.

References CE_GW_F_CAN_FD, CE_GW_TYPE_ETH, CE_GW_TYPE_NET, CE_GW_TYPE_NONE, CE_GW_TYPE_TCP, and CE_GW_TYPE_UDP.

Referenced by ce_gw_create_route().

213  {
214 
215  int mtu = ETH_DATA_LEN; /* Standart Ethernet Value */
216 
217  switch (type) {
218  case CE_GW_TYPE_NONE:
219  /* Do Nothing (default Ethernet MTU will be set) */
220  break;
221  case CE_GW_TYPE_ETH:
222  if ((flags & CE_GW_F_CAN_FD) == CE_GW_F_CAN_FD) {
223  mtu = CANFD_MAX_DLEN;
224  } else {
225  mtu = CAN_MAX_DLEN;
226  }
227  break;
228  case CE_GW_TYPE_NET:
229  if ((flags & CE_GW_F_CAN_FD) == CE_GW_F_CAN_FD) {
230  mtu = sizeof(struct canfd_frame);
231  } else {
232  mtu = sizeof(struct can_frame);
233  }
234  break;
235  case CE_GW_TYPE_TCP:
236  /* TODO nothing yet */
237  break;
238  case CE_GW_TYPE_UDP:
239  /* TODO nothing yet */
240  break;
241  default:
242  pr_err("ce_gw_dev: Type not defined.");
243  }
244 
245  if (dev->mtu >= mtu) {
246  return true;
247  }
248 
249  return false;
250 }

Here is the caller graph for this function:

HLIST_HEAD ( ce_gw_dev_allocated  )

list of all allocated ethernet devices

HLIST_HEAD ( ce_gw_dev_registered  )

list of all registered ethernet devices

Variable Documentation

struct kmem_cache* ce_gw_dev_cache __read_mostly
static

cache for lists

Definition at line 54 of file ce_gw_dev.c.

struct net_device_ops ce_gw_ops
static
Initial value:
= {
.ndo_init = ce_gw_dev_init,
.ndo_open = ce_gw_dev_open,
.ndo_stop = ce_gw_dev_stop,
.ndo_start_xmit = ce_gw_dev_start_xmit,
0
}

Defined Functions of Ethernet device.

Definition at line 164 of file ce_gw_dev.c.

Referenced by ce_gw_dev_setup().