\doxysection{src/core/netdev.c File Reference}
\label{netdev_8c}\index{src/core/netdev.c@{src/core/netdev.c}}


Example lifecycle use of the API\+:  


{\ttfamily \#include \char`\"{}config.\+h\char`\"{}}\newline
{\ttfamily \#include $<$stdio.\+h$>$}\newline
{\ttfamily \#include $<$stdlib.\+h$>$}\newline
{\ttfamily \#include $<$inttypes.\+h$>$}\newline
{\ttfamily \#include $<$fcntl.\+h$>$}\newline
{\ttfamily \#include $<$unistd.\+h$>$}\newline
{\ttfamily \#include $<$errno.\+h$>$}\newline
{\ttfamily \#include $<$ifaddrs.\+h$>$}\newline
{\ttfamily \#include $<$sys/types.\+h$>$}\newline
{\ttfamily \#include $<$sys/time.\+h$>$}\newline
{\ttfamily \#include $<$sys/ioctl.\+h$>$}\newline
{\ttfamily \#include $<$sys/socket.\+h$>$}\newline
{\ttfamily \#include $<$sys/stat.\+h$>$}\newline
{\ttfamily \#include $<$net/if.\+h$>$}\newline
{\ttfamily \#include $<$net/route.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/utils.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/select.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/linuxlist.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/logging.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/socket.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/netns.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/netdev.\+h$>$}\newline
\doxysubsection*{Data Structures}
\begin{DoxyCompactItemize}
\item 
struct \textbf{ netdev\+\_\+netns\+\_\+ctx}
\item 
struct \textbf{ osmo\+\_\+netdev}
\end{DoxyCompactItemize}
\doxysubsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\#define \textbf{ IFINDEX\+\_\+\+UNUSED}~0
\item 
\#define \textbf{ LOGNETDEV}(netdev,  lvl,  fmt,  args ...)
\item 
\#define \textbf{ NETDEV\+\_\+\+NETNS\+\_\+\+ENTER}(netdev,  switch\+\_\+state,  str\+\_\+prefix)
\item 
\#define \textbf{ NETDEV\+\_\+\+NETNS\+\_\+\+EXIT}(netdev,  switch\+\_\+state,  str\+\_\+prefix)
\end{DoxyCompactItemize}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
static struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$ \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+alloc} (void $\ast$ctx, const char $\ast$netns\+\_\+name)
\item 
static void \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+free} (struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$netns\+\_\+ctx)
\item 
static int \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+init} (struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$netns\+\_\+ctx)
\item 
static struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$ \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+find\+\_\+by\+\_\+netns\+\_\+name} (const char $\ast$netns\+\_\+name)
\item 
static struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$ \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+get} (const char $\ast$netns\+\_\+name)
\item 
static void \textbf{ netdev\+\_\+netns\+\_\+ctx\+\_\+put} (struct \textbf{ netdev\+\_\+netns\+\_\+ctx} $\ast$netns\+\_\+ctx)
\item 
struct \textbf{ osmo\+\_\+netdev} $\ast$ \textbf{ osmo\+\_\+netdev\+\_\+alloc} (void $\ast$ctx, const char $\ast$\textbf{ name})
\begin{DoxyCompactList}\small\item\em Allocate a new netdev object. \end{DoxyCompactList}\item 
void \textbf{ osmo\+\_\+netdev\+\_\+free} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Free an allocated netdev object. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+register} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Start managing the network device referenced by the netdev object. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+unregister} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Unregister the netdev object (stop managing /moniutoring the interface) \end{DoxyCompactList}\item 
bool \textbf{ osmo\+\_\+netdev\+\_\+is\+\_\+registered} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Retrieve whether the netdev object is in \char`\"{}registered\char`\"{} state. \end{DoxyCompactList}\item 
void \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+priv\+\_\+data} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, void $\ast$priv\+\_\+data)
\begin{DoxyCompactList}\small\item\em Set private user data pointer on the netdev object. \end{DoxyCompactList}\item 
void $\ast$ \textbf{ osmo\+\_\+netdev\+\_\+get\+\_\+priv\+\_\+data} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Get private user data pointer from the netdev object. \end{DoxyCompactList}\item 
void \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+ifupdown\+\_\+ind\+\_\+cb} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, \textbf{ osmo\+\_\+netdev\+\_\+ifupdown\+\_\+ind\+\_\+cb\+\_\+t} ifupdown\+\_\+ind\+\_\+cb)
\begin{DoxyCompactList}\small\item\em Set ifupdown\+\_\+ind\+\_\+cb callback, called when the link status (UP/\+DOWN) changes. \end{DoxyCompactList}\item 
void \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+dev\+\_\+name\+\_\+chg\+\_\+cb} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, \textbf{ osmo\+\_\+netdev\+\_\+dev\+\_\+name\+\_\+chg\+\_\+cb\+\_\+t} dev\+\_\+name\+\_\+chg\+\_\+cb)
\begin{DoxyCompactList}\small\item\em Set dev\+\_\+name\+\_\+chg\+\_\+cb callback, called when a change in the network name is detected. \end{DoxyCompactList}\item 
void \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+mtu\+\_\+chg\+\_\+cb} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, \textbf{ osmo\+\_\+netdev\+\_\+mtu\+\_\+chg\+\_\+cb\+\_\+t} mtu\+\_\+chg\+\_\+cb)
\begin{DoxyCompactList}\small\item\em Set mtu\+\_\+chg\+\_\+cb callback, called when a change in the network name is detected. \end{DoxyCompactList}\item 
const char $\ast$ \textbf{ osmo\+\_\+netdev\+\_\+get\+\_\+name} (const struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Get name used to identify the netdev object. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+ifindex} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, unsigned int ifindex)
\begin{DoxyCompactList}\small\item\em Set (specify) interface index identifying the network interface to manage. \end{DoxyCompactList}\item 
unsigned int \textbf{ osmo\+\_\+netdev\+\_\+get\+\_\+ifindex} (const struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Get interface index identifying the interface managed by netdev. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+netns\+\_\+name} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, const char $\ast$netns\+\_\+name)
\begin{DoxyCompactList}\small\item\em Set (specify) name of the network namespace where the network interface to manage is located. \end{DoxyCompactList}\item 
const char $\ast$ \textbf{ osmo\+\_\+netdev\+\_\+get\+\_\+netns\+\_\+name} (const struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Get name of network namespace used when opening the netdev interface. \end{DoxyCompactList}\item 
const char $\ast$ \textbf{ osmo\+\_\+netdev\+\_\+get\+\_\+dev\+\_\+name} (const struct \textbf{ osmo\+\_\+netdev} $\ast$netdev)
\begin{DoxyCompactList}\small\item\em Get name used to name the network interface created by the netdev object. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+set\+\_\+mtu} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, uint32\+\_\+t mtu)
\begin{DoxyCompactList}\small\item\em Set the MTU of the network interface. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+ifupdown} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, bool ifupdown)
\begin{DoxyCompactList}\small\item\em Bring netdev interface UP or DOWN. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+add\+\_\+addr} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$addr, uint8\+\_\+t prefixlen)
\begin{DoxyCompactList}\small\item\em Add IP address to netdev interface. \end{DoxyCompactList}\item 
int \textbf{ osmo\+\_\+netdev\+\_\+add\+\_\+route} (struct \textbf{ osmo\+\_\+netdev} $\ast$netdev, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$dst\+\_\+addr, uint8\+\_\+t dst\+\_\+prefixlen, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$gw\+\_\+addr)
\begin{DoxyCompactList}\small\item\em Add IP route to netdev interface. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
static struct \textbf{ llist\+\_\+head} \textbf{ g\+\_\+netdev\+\_\+netns\+\_\+ctx\+\_\+list} = \textbf{ LLIST\+\_\+\+HEAD\+\_\+\+INIT}(g\+\_\+netdev\+\_\+netns\+\_\+ctx\+\_\+list)
\item 
static struct \textbf{ llist\+\_\+head} \textbf{ g\+\_\+netdev\+\_\+list} = \textbf{ LLIST\+\_\+\+HEAD\+\_\+\+INIT}(g\+\_\+netdev\+\_\+list)
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
Example lifecycle use of the API\+: 

\begin{DoxyVerb} 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);
\end{DoxyVerb}
 