libosmocore 1.13.1.1-afb4 Osmocom core library

Data Structures | Macros | Functions
Tun

tun network device (interface) convenience functions More...

## Data Structures
struct | osmo_tundev

## Macros
#define | TUN_DEV_PATH "/dev/net/tun"
#define | TUN_PACKET_MAX 8196
#define | LOGTUN(tun, lvl, fmt, args ...)

## Functions
static int | tundev_decaps (struct osmo_tundev *tundev)
static int | tundev_read_cb (struct osmo_fd *fd)
static int | tundev_write_cb (struct osmo_fd *fd, struct msgb *msg)
static int | tundev_ifupdown_ind_cb (struct osmo_netdev *netdev, bool ifupdown)
static int | tundev_dev_name_chg_cb (struct osmo_netdev *netdev, const char *new_dev_name)
static int | tundev_mtu_chg_cb (struct osmo_netdev *netdev, uint32_t new_mtu)
struct osmo_tundev * | osmo_tundev_alloc (void *ctx, const char *name)
| Allocate a new tundev object. More...
void | osmo_tundev_free (struct osmo_tundev *tundev)
| Free an allocated tundev object. More...
int | osmo_tundev_open (struct osmo_tundev *tundev)
| Open the tunnel device owned by the tundev object. More...
int | osmo_tundev_close (struct osmo_tundev *tundev)
| Close the tunnel device owned by the tundev object. More...
bool | osmo_tundev_is_open (struct osmo_tundev *tundev)
| Retrieve whether the tundev object is in "opened" state. More...
void | osmo_tundev_set_priv_data (struct osmo_tundev *tundev, void *priv_data)
| Set private user data pointer on the tundev object. More...
void * | osmo_tundev_get_priv_data (struct osmo_tundev *tundev)
| Get private user data pointer from the tundev object. More...
void | osmo_tundev_set_data_ind_cb (struct osmo_tundev *tundev, osmo_tundev_data_ind_cb_t data_ind_cb)
| Set data_ind_cb callback, called when a new packet is received on the tun interface. More...
const char * | osmo_tundev_get_name (const struct osmo_tundev *tundev)
| Get name used to identify the tundev object. More...
int | osmo_tundev_get_fd (const struct osmo_tundev *tundev)
| Get FD associated to the tundev object. More...
int | osmo_tundev_set_dev_name (struct osmo_tundev *tundev, const char *dev_name)
| Set name used to name the tunnel interface created by the tundev object. More...
const char * | osmo_tundev_get_dev_name (const struct osmo_tundev *tundev)
| Get name used to name the tunnel interface created by the tundev object. More...
int | osmo_tundev_set_netns_name (struct osmo_tundev *tundev, const char *netns_name)
| Set name of the network namespace to use when opening the tunnel interface. More...
const char * | osmo_tundev_get_netns_name (const struct osmo_tundev *tundev)
| Get name of network namespace used when opening the tunnel interface. More...
struct osmo_netdev * | osmo_tundev_get_netdev (struct osmo_tundev *tundev)
| Get netdev managing the tunnel interface of tundev. More...
int | osmo_tundev_send (struct osmo_tundev *tundev, struct msgb *msg)
| Submit a packet to the tunnel device managed by the tundev object. More...

## Detailed Description

tun network device (interface) convenience functions

## Macro Definition Documentation

## ◆ LOGTUN

#define LOGTUN | ( |  | tun,
|  |  | lvl,
|  |  | fmt,
|  |  | args ...
| ) |  |

Value:
LOGP(DLGLOBAL, lvl, "TUN(%s,if=%s/%u,ns=%s): " fmt, \
(tun)->name, (tun)->dev_name ? : "", \
(tun)->ifindex, (tun)->netns_name ? : "", ## args)
name
const char * name
DLGLOBAL
#define DLGLOBAL
global logging
Definition: logging.h:129
LOGP
#define LOGP(ss, level, fmt, args...)
Log a new message through the Osmocom logging framework.
Definition: logging.h:49

## ◆ TUN_DEV_PATH

#define TUN_DEV_PATH "/dev/net/tun"

## ◆ TUN_PACKET_MAX

#define TUN_PACKET_MAX 8196

## Function Documentation

## ◆ osmo_tundev_alloc()

struct osmo_tundev * osmo_tundev_alloc | ( | void * | ctx,
|  | const char * | name
| ) |  |

Allocate a new tundev object.

Parameters

  [in] | ctx | talloc context to use as a parent when allocating the tundev object
  [in] | name | A name providen to identify the tundev object

Returns
  newly allocated tundev object on success; NULL on error

References osmo_wqueue::bfd, name, osmo_tundev::name, osmo_tundev::netdev, OSMO_FD_READ, osmo_fd_setup(), osmo_netdev_alloc(), osmo_netdev_set_dev_name_chg_cb(), osmo_netdev_set_ifupdown_ind_cb(), osmo_netdev_set_mtu_chg_cb(), osmo_netdev_set_priv_data(), osmo_wqueue_bfd_cb(), osmo_wqueue_init(), osmo_wqueue::read_cb, tundev_dev_name_chg_cb(), tundev_ifupdown_ind_cb(), tundev_mtu_chg_cb(), tundev_read_cb(), tundev_write_cb(), osmo_tundev::wqueue, and osmo_wqueue::write_cb.

## ◆ osmo_tundev_close()

int osmo_tundev_close | ( | struct osmo_tundev * | tundev | ) |

Close the tunnel device owned by the tundev object.

Parameters

  [in] | tundev | The tundev object to close

Returns
  0 on success; negative on error

References osmo_wqueue::bfd, osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, osmo_fd::fd, osmo_tundev::netdev, osmo_tundev::opened, osmo_fd_unregister(), osmo_netdev_unregister(), osmo_wqueue_clear(), and osmo_tundev::wqueue.

Referenced by osmo_tundev_free().

## ◆ osmo_tundev_free()

void osmo_tundev_free | ( | struct osmo_tundev * | tundev | ) |

Free an allocated tundev object.

Parameters

  [in] | tundev | The tundev object to free

References osmo_tundev::netdev, osmo_netdev_free(), and osmo_tundev_close().

## ◆ osmo_tundev_get_dev_name()

const char * osmo_tundev_get_dev_name | ( | const struct osmo_tundev * | tundev | ) |

Get name used to name the tunnel interface created by the tundev object.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The current value of the configured tunnel interface name to use

References osmo_tundev::dev_name.

## ◆ osmo_tundev_get_fd()

int osmo_tundev_get_fd | ( | const struct osmo_tundev * | tundev | ) |

Get FD associated to the tundev object.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The fd of the currently open tundev, negative otherwise

References osmo_wqueue::bfd, osmo_fd::fd, and osmo_tundev::wqueue.

## ◆ osmo_tundev_get_name()

const char * osmo_tundev_get_name | ( | const struct osmo_tundev * | tundev | ) |

Get name used to identify the tundev object.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The current value of the name used to identify the tundev object

References osmo_tundev::name.

## ◆ osmo_tundev_get_netdev()

struct osmo_netdev * osmo_tundev_get_netdev | ( | struct osmo_tundev * | tundev | ) |

Get netdev managing the tunnel interface of tundev.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The netdev objet managing the tun interface

References osmo_tundev::netdev.

## ◆ osmo_tundev_get_netns_name()

const char * osmo_tundev_get_netns_name | ( | const struct osmo_tundev * | tundev | ) |

Get name of network namespace used when opening the tunnel interface.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The current value of the configured network namespace

References osmo_tundev::netns_name.

## ◆ osmo_tundev_get_priv_data()

void * osmo_tundev_get_priv_data | ( | struct osmo_tundev * | tundev | ) |

Get private user data pointer from the tundev object.

Parameters

  [in] | tundev | The tundev object from where to retrieve the field

Returns
  The current value of the priv_data field.

References osmo_tundev::priv_data.

## ◆ osmo_tundev_is_open()

bool osmo_tundev_is_open | ( | struct osmo_tundev * | tundev | ) |

Retrieve whether the tundev object is in "opened" state.

Parameters

  [in] | tundev | The tundev object to check

Returns
  true if in state "opened"; false otherwise

References osmo_tundev::opened.

## ◆ osmo_tundev_open()

int osmo_tundev_open | ( | struct osmo_tundev * | tundev | ) |

Open the tunnel device owned by the tundev object.

Parameters

  [in] | tundev | The tundev object to open

Returns
  0 on success; negative on error

References osmo_wqueue::bfd, DLGLOBAL, ENOTSUP, osmo_fd::fd, osmo_tundev::ifindex, LOGL_ERROR, LOGL_INFO, LOGP, LOGTUN, osmo_tundev::netdev, osmo_tundev::netns_name, osmo_tundev::opened, osmo_fd_register(), osmo_netdev_register(), osmo_netdev_set_ifindex(), osmo_netdev_set_netns_name(), osmo_netdev_unregister(), osmo_netns_open_fd(), osmo_netns_switch_enter(), osmo_netns_switch_exit(), and osmo_tundev::wqueue.

## ◆ osmo_tundev_send()

int osmo_tundev_send | ( | struct osmo_tundev * | tundev,
|  | struct msgb * | msg
| ) |  |

Submit a packet to the tunnel device managed by the tundev object.

Parameters

  [in] | tundev | The tundev object owning the tunnel device where to inject the packet
  [in] | msg | The msgb containg the packet to transfer

Returns
  0 on success; negative on error

This function takes the ownership of msg in all cases.

References LOGL_ERROR, LOGTUN, msg, msgb_free(), OSMO_ASSERT, osmo_wqueue_enqueue(), and osmo_tundev::wqueue.

## ◆ osmo_tundev_set_data_ind_cb()

void osmo_tundev_set_data_ind_cb | ( | struct osmo_tundev * | tundev,
|  | osmo_tundev_data_ind_cb_t | data_ind_cb
| ) |  |

Set data_ind_cb callback, called when a new packet is received on the tun interface.

Parameters

  [in] | tundev | The tundev object where the field is set
  [in] | data_ind_cb | the user provided function to be called when a new packet is received

References osmo_tundev::data_ind_cb.

## ◆ osmo_tundev_set_dev_name()

int osmo_tundev_set_dev_name | ( | struct osmo_tundev * | tundev,
|  | const char * | dev_name
| ) |  |

Set name used to name the tunnel interface created by the tundev object.

Parameters

  [in] | tundev | The tundev object where the field is set
  [in] | dev_name | The tunnel interface name to use

Returns
  0 on success; negative on error

This is used during osmo_tundev_open() time, and hence shouldn't be changed when the tundev object is in "opened" state. If left as NULL (default), the system will pick a suitable name during osmo_tundev_open(), and the field will be updated to the system-selected name, which can be retrieved later with osmo_tundev_get_dev_name().

References osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, osmo_tundev::opened, and osmo_talloc_replace_string().

## ◆ osmo_tundev_set_netns_name()

int osmo_tundev_set_netns_name | ( | struct osmo_tundev * | tundev,
|  | const char * | netns_name
| ) |  |

Set name of the network namespace to use when opening the tunnel interface.

Parameters

  [in] | tundev | The tundev object where the field is set
  [in] | netns_name | The network namespace to use during tunnel interface creation

Returns
  0 on success; negative on error

This is used during osmo_tundev_open() time, and hence shouldn't be changed when the tundev object is in "opened" state. If left as NULL (default), the system will stay in the current network namespace.

References osmo_tundev::netns_name, osmo_tundev::opened, and osmo_talloc_replace_string().

## ◆ osmo_tundev_set_priv_data()

void osmo_tundev_set_priv_data | ( | struct osmo_tundev * | tundev,
|  | void * | priv_data
| ) |  |

Set private user data pointer on the tundev object.

Parameters

  [in] | tundev | The tundev object where the field is set

References osmo_tundev::priv_data.

## ◆ tundev_decaps()

static int tundev_decaps | ( | struct osmo_tundev * | tundev | ) |
| static

References osmo_wqueue::bfd, osmo_tundev::data_ind_cb, osmo_fd::fd, LOGL_ERROR, LOGTUN, msg, msgb_alloc(), msgb_data(), msgb_free(), msgb_put(), TUN_PACKET_MAX, and osmo_tundev::wqueue.

Referenced by tundev_read_cb().

## ◆ tundev_dev_name_chg_cb()

static int tundev_dev_name_chg_cb | ( | struct osmo_netdev * | netdev,
|  | const char * | new_dev_name
| ) |  |
| static

References osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, LOGL_NOTICE, LOGTUN, osmo_tundev::netdev, osmo_netdev_get_dev_name(), osmo_netdev_get_priv_data(), and osmo_talloc_replace_string().

Referenced by osmo_tundev_alloc().

## ◆ tundev_ifupdown_ind_cb()

static int tundev_ifupdown_ind_cb | ( | struct osmo_netdev * | netdev,
|  | bool | ifupdown
| ) |  |
| static

References LOGL_NOTICE, LOGTUN, osmo_tundev::netdev, osmo_netdev_get_priv_data(), osmo_wqueue_clear(), and osmo_tundev::wqueue.

Referenced by osmo_tundev_alloc().

## ◆ tundev_mtu_chg_cb()

static int tundev_mtu_chg_cb | ( | struct osmo_netdev * | netdev,
|  | uint32_t | new_mtu
| ) |  |
| static

References LOGL_NOTICE, LOGTUN, osmo_tundev::netdev, and osmo_netdev_get_priv_data().

Referenced by osmo_tundev_alloc().

## ◆ tundev_read_cb()

static int tundev_read_cb | ( | struct osmo_fd * | fd | ) |
| static

References osmo_fd::data, and tundev_decaps().

Referenced by osmo_tundev_alloc().

## ◆ tundev_write_cb()

static int tundev_write_cb | ( | struct osmo_fd * | fd,
|  | struct msgb * | msg
| ) |  |
| static

References osmo_wqueue::bfd, osmo_fd::data, osmo_fd::fd, LOGL_ERROR, LOGTUN, msg, msgb_data(), msgb_length(), and osmo_tundev::wqueue.

Referenced by osmo_tundev_alloc().