Loading...
Searching...
No Matches
netdev.c File Reference
Netdev

Example lifecycle use of the API: More...

`#include "config.h"`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <inttypes.h>`
`#include <fcntl.h>`
`#include <unistd.h>`
`#include <errno.h>`
`#include <ifaddrs.h>`
`#include <sys/types.h>`
`#include <sys/time.h>`
`#include <sys/ioctl.h>`
`#include <sys/socket.h>`
`#include <sys/stat.h>`
`#include <net/if.h>`
`#include <net/route.h>`
`#include <osmocom/core/utils.h>`
`#include <osmocom/core/select.h>`
`#include <osmocom/core/linuxlist.h>`
`#include <osmocom/core/logging.h>`
`#include <osmocom/core/socket.h>`
`#include <osmocom/core/netns.h>`
`#include <osmocom/core/netdev.h>`

## Data Structures

struct | netdev_netns_ctx
struct | osmo_netdev

## Macros

#define | IFINDEX_UNUSED 0
#define | LOGNETDEV(netdev, lvl, fmt, args ...)
#define | NETDEV_NETNS_ENTER(netdev, switch_state, str_prefix)
#define | NETDEV_NETNS_EXIT(netdev, switch_state, str_prefix)

## Functions

static struct netdev_netns_ctx * | netdev_netns_ctx_alloc (void *ctx, const char *netns_name)
static void | netdev_netns_ctx_free (struct netdev_netns_ctx *netns_ctx)
static int | netdev_netns_ctx_init (struct netdev_netns_ctx *netns_ctx)
static struct netdev_netns_ctx * | netdev_netns_ctx_find_by_netns_name (const char *netns_name)
static struct netdev_netns_ctx * | netdev_netns_ctx_get (const char *netns_name)
static void | netdev_netns_ctx_put (struct netdev_netns_ctx *netns_ctx)
struct osmo_netdev * | osmo_netdev_alloc (void *ctx, const char *name)
| Allocate a new netdev object.
void | osmo_netdev_free (struct osmo_netdev *netdev)
| Free an allocated netdev object.
int | osmo_netdev_register (struct osmo_netdev *netdev)
| Start managing the network device referenced by the netdev object.
int | osmo_netdev_unregister (struct osmo_netdev *netdev)
| Unregister the netdev object (stop managing /moniutoring the interface).
bool | osmo_netdev_is_registered (struct osmo_netdev *netdev)
| Retrieve whether the netdev object is in "registered" state.
void | osmo_netdev_set_priv_data (struct osmo_netdev *netdev, void *priv_data)
| Set private user data pointer on the netdev object.
void * | osmo_netdev_get_priv_data (struct osmo_netdev *netdev)
| Get private user data pointer from the netdev object.
void | osmo_netdev_set_ifupdown_ind_cb (struct osmo_netdev *netdev, osmo_netdev_ifupdown_ind_cb_t ifupdown_ind_cb)
| Set ifupdown_ind_cb callback, called when the link status (UP/DOWN) changes.
void | osmo_netdev_set_dev_name_chg_cb (struct osmo_netdev *netdev, osmo_netdev_dev_name_chg_cb_t dev_name_chg_cb)
| Set dev_name_chg_cb callback, called when a change in the network name is detected.
void | osmo_netdev_set_mtu_chg_cb (struct osmo_netdev *netdev, osmo_netdev_mtu_chg_cb_t mtu_chg_cb)
| Set mtu_chg_cb callback, called when a change in the network name is detected.
const char * | osmo_netdev_get_name (const struct osmo_netdev *netdev)
| Get name used to identify the netdev object.
int | osmo_netdev_set_ifindex (struct osmo_netdev *netdev, unsigned int ifindex)
| Set (specify) interface index identifying the network interface to manage.
unsigned int | osmo_netdev_get_ifindex (const struct osmo_netdev *netdev)
| Get interface index identifying the interface managed by netdev.
int | osmo_netdev_set_netns_name (struct osmo_netdev *netdev, const char *netns_name)
| Set (specify) name of the network namespace where the network interface to manage is located.
const char * | osmo_netdev_get_netns_name (const struct osmo_netdev *netdev)
| Get name of network namespace used when opening the netdev interface.
const char * | osmo_netdev_get_dev_name (const struct osmo_netdev *netdev)
| Get name used to name the network interface created by the netdev object.
int | osmo_netdev_set_mtu (struct osmo_netdev *netdev, uint32_t mtu)
| Set the MTU of the network interface.
int | osmo_netdev_ifupdown (struct osmo_netdev *netdev, bool ifupdown)
| Bring netdev interface UP or DOWN.
int | osmo_netdev_add_addr (struct osmo_netdev *netdev, const struct osmo_sockaddr *addr, uint8_t prefixlen)
| Add IP address to netdev interface.
int | osmo_netdev_add_route (struct osmo_netdev *netdev, const struct osmo_sockaddr *dst_addr, uint8_t dst_prefixlen, const struct osmo_sockaddr *gw_addr)
| Add IP route to netdev interface.

## Variables

static struct llist_head | g_netdev_netns_ctx_list = LLIST_HEAD_INIT(g_netdev_netns_ctx_list)
static struct llist_head | g_netdev_list = LLIST_HEAD_INIT(g_netdev_list)

## Detailed Description

Example lifecycle use of the API:

     struct osmo_sockaddr_str osa_str = {};
     struct osmo_sockaddr osa = {};

     // Allocate object:
     struct osmo_netdev *netdev = osmo_netdev_alloc(parent_talloc_ctx, name);
     OSMO_ASSERT(netdev);

     // Configure object (before registration):
     rc = osmo_netdev_set_netns_name(netdev, "some_netns_name_or_null");
     rc = osmo_netdev_set_ifindex(netdev, if_nametoindex("eth0"));

     // Register object:
     rc = osmo_netdev_register(netdev);
     // The network interface is now being monitored and the network interface
     // can be operated (see below)

     // Add a local IPv4 address:
     rc = osmo_sockaddr_str_from_str2(&osa_str, "192.168.200.1");
     rc = osmo_sockaddr_str_to_sockaddr(&osa_str, &osa.u.sas);
     rc = osmo_netdev_add_addr(netdev, &osa, 24);

     // Bring network interface up:
     rc = osmo_netdev_ifupdown(netdev, true);

     // Add default route (0.0.0.0/0):
     rc = osmo_sockaddr_str_from_str2(&osa_str, "0.0.0.0");
     rc = osmo_sockaddr_str_to_sockaddr(&osa_str, &osa.u.sas);
     rc = osmo_netdev_add_route(netdev, &osa, 0, NULL);

     // Unregister (can be freed directly too):
     rc = osmo_netdev_unregister(netdev);
     // Free the object:
     osmo_netdev_free(netdev);