|
|
struct rtnl_link * | rtnl_link_alloc (void) |
| |
|
void | rtnl_link_put (struct rtnl_link *link) |
| |
|
| struct nl_cache * | rtnl_link_alloc_cache (struct nl_handle *handle) |
| | Allocate link cache and fill in all configured links. More...
|
| |
| struct rtnl_link * | rtnl_link_get (struct nl_cache *cache, int ifindex) |
| | Look up link by interface index in the provided cache. More...
|
| |
| struct rtnl_link * | rtnl_link_get_by_name (struct nl_cache *cache, const char *name) |
| | Look up link by link name in the provided cache. More...
|
| |
|
| struct nl_msg * | rtnl_link_build_change_request (struct rtnl_link *old, struct rtnl_link *tmpl, int flags) |
| | Builds a netlink change request message to change link attributes. More...
|
| |
| int | rtnl_link_change (struct nl_handle *handle, struct rtnl_link *old, struct rtnl_link *tmpl, int flags) |
| | Change link attributes. More...
|
| |
|
| char * | rtnl_link_i2name (struct nl_cache *cache, int ifindex, char *dst, size_t len) |
| | Translate an interface index to the corresponding link name. More...
|
| |
| int | rtnl_link_name2i (struct nl_cache *cache, const char *name) |
| | Translate a link name to the corresponding interface index. More...
|
| |
|
|
char * | rtnl_link_flags2str (int flags, char *buf, size_t len) |
| |
|
int | rtnl_link_str2flags (const char *name) |
| |
|
|
char * | rtnl_link_stat2str (int st, char *buf, size_t len) |
| |
|
int | rtnl_link_str2stat (const char *name) |
| |
|
|
char * | rtnl_link_operstate2str (int st, char *buf, size_t len) |
| |
|
int | rtnl_link_str2operstate (const char *name) |
| |
|
|
char * | rtnl_link_mode2str (int st, char *buf, size_t len) |
| |
|
int | rtnl_link_str2mode (const char *name) |
| |
|
|
void | rtnl_link_set_qdisc (struct rtnl_link *link, const char *qdisc) |
| |
|
char * | rtnl_link_get_qdisc (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_name (struct rtnl_link *link, const char *name) |
| |
|
char * | rtnl_link_get_name (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_addr (struct rtnl_link *link, struct nl_addr *addr) |
| |
|
struct nl_addr * | rtnl_link_get_addr (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_broadcast (struct rtnl_link *link, struct nl_addr *brd) |
| |
|
struct nl_addr * | rtnl_link_get_broadcast (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_flags (struct rtnl_link *link, unsigned int flags) |
| |
|
void | rtnl_link_unset_flags (struct rtnl_link *link, unsigned int flags) |
| |
|
unsigned int | rtnl_link_get_flags (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_family (struct rtnl_link *link, int family) |
| |
|
int | rtnl_link_get_family (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_arptype (struct rtnl_link *link, unsigned int arptype) |
| |
|
unsigned int | rtnl_link_get_arptype (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_ifindex (struct rtnl_link *link, int ifindex) |
| |
|
int | rtnl_link_get_ifindex (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_mtu (struct rtnl_link *link, unsigned int mtu) |
| |
|
unsigned int | rtnl_link_get_mtu (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_txqlen (struct rtnl_link *link, unsigned int txqlen) |
| |
|
unsigned int | rtnl_link_get_txqlen (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_weight (struct rtnl_link *link, unsigned int weight) |
| |
|
unsigned int | rtnl_link_get_weight (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_link (struct rtnl_link *link, int ifindex) |
| |
|
int | rtnl_link_get_link (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_master (struct rtnl_link *link, int ifindex) |
| |
|
int | rtnl_link_get_master (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_operstate (struct rtnl_link *link, uint8_t operstate) |
| |
|
uint8_t | rtnl_link_get_operstate (struct rtnl_link *link) |
| |
|
void | rtnl_link_set_linkmode (struct rtnl_link *link, uint8_t linkmode) |
| |
|
uint8_t | rtnl_link_get_linkmode (struct rtnl_link *link) |
| |
|
uint64_t | rtnl_link_get_stat (struct rtnl_link *link, int id) |
| |
| int | rtnl_link_set_info_type (struct rtnl_link *link, const char *type) |
| | Specify the info type of a link. More...
|
| |
| char * | rtnl_link_get_info_type (struct rtnl_link *link) |
| | Return info type of a link. More...
|
| |
- Link Identification
- A link can be identified by either its interface index or by its name. The kernel favours the interface index but falls back to the interface name if the interface index is lesser-than 0 for kernels >= 2.6.11. Therefore you can request changes without mapping a interface name to the corresponding index first.
- Changeable Attributes
- Link layer address
- Link layer broadcast address
- device mapping (ifmap) (>= 2.6.9)
- MTU (>= 2.6.9)
- Transmission queue length (>= 2.6.9)
- Weight (>= 2.6.9)
- Link name (only via access through interface index) (>= 2.6.9)
- Flags (>= 2.6.9)
- IFF_DEBUG
- IFF_NOTRAILERS
- IFF_NOARP
- IFF_DYNAMIC
- IFF_MULTICAST
- IFF_PORTSEL
- IFF_AUTOMEDIA
- IFF_UP
- IFF_PROMISC
- IFF_ALLMULTI
- Link Flags (linux/if.h)
IFF_UP Status of link (up|down)
IFF_BROADCAST Indicates this link allows broadcasting
IFF_MULTICAST Indicates this link allows multicasting
IFF_ALLMULTI Indicates this link is doing multicast routing
IFF_DEBUG Tell the driver to do debugging (currently unused)
IFF_LOOPBACK This is the loopback link
IFF_POINTOPOINT Point-to-point link
IFF_NOARP Link is unable to perform ARP
IFF_PROMISC Status of promiscious mode flag
IFF_MASTER Used by teql
IFF_SLAVE Used by teql
IFF_PORTSEL Indicates this link allows port selection
IFF_AUTOMEDIA Indicates this link selects port automatically
IFF_DYNAMIC Indicates the address of this link is dynamic
IFF_RUNNING Link is running and carrier is ok.
IFF_NOTRAILERS Unused, BSD compat.
- Notes on IFF_PROMISC and IFF_ALLMULTI flags
- Although you can query the status of IFF_PROMISC and IFF_ALLMULTI they do not represent the actual state in the kernel but rather whether the flag has been enabled/disabled by userspace. The link may be in promiscious mode even if IFF_PROMISC is not set in a link dump request response because promiscity might be needed by the driver for a period of time.
- Note
- The unit of the transmission queue length depends on the link type, a common unit is packets.
- 1) Retrieving information about available links
- 2) Changing link attributes
struct rtnl_link *request = rtnl_link_alloc();
rtnl_link_set_weight(request, 300);
rtnl_link_set_mtu(request, 1360);
rtnl_link_unset_flags(request, rtnl_link_str2flags("up"));
rtnl_link_put(old);
- 3) Link Type Specific Attributes
struct rtnl_link *request = rtnl_link_alloc();
rtnl_link_vlan_set_id(request, 10);
rtnl_link_vlan_set_ingress_map(request, 2, 8);
if (!strcmp(type, "vlan"))
int id = rtnl_link_vlan_get_id(link);
◆ rtnl_link_alloc_cache()
| struct nl_cache* rtnl_link_alloc_cache |
( |
struct nl_handle * |
handle | ) |
|
- Parameters
-
Allocates a new link cache, initializes it properly and updates it to include all links currently configured in the kernel.
- Note
- Free the memory after usage.
- Returns
- Newly allocated cache or NULL if an error occured.
Definition at line 869 of file link.c.
871 struct nl_cache * cache;
References nl_cache_alloc().
◆ rtnl_link_get()
| struct rtnl_link* rtnl_link_get |
( |
struct nl_cache * |
cache, |
|
|
int |
ifindex |
|
) |
| |
- Parameters
-
| cache | link cache |
| ifindex | link interface index |
The caller owns a reference on the returned object and must give the object back via rtnl_link_put().
- Returns
- pointer to link inside the cache or NULL if no match was found.
Definition at line 895 of file link.c.
897 struct rtnl_link *link;
899 if (cache->c_ops != &rtnl_link_ops)
902 nl_list_for_each_entry(link, &cache->c_items, ce_list) {
903 if (link->l_index == ifindex) {
Referenced by rtnl_link_i2name().
◆ rtnl_link_get_by_name()
| struct rtnl_link* rtnl_link_get_by_name |
( |
struct nl_cache * |
cache, |
|
|
const char * |
name |
|
) |
| |
- Parameters
-
| cache | link cache |
| name | link name |
The caller owns a reference on the returned object and must give the object back via rtnl_link_put().
- Returns
- pointer to link inside the cache or NULL if no match was found.
Definition at line 922 of file link.c.
925 struct rtnl_link *link;
927 if (cache->c_ops != &rtnl_link_ops)
930 nl_list_for_each_entry(link, &cache->c_items, ce_list) {
931 if (!strcmp(name, link->l_name)) {
Referenced by rtnl_link_name2i().
◆ rtnl_link_build_change_request()
| struct nl_msg* rtnl_link_build_change_request |
( |
struct rtnl_link * |
old, |
|
|
struct rtnl_link * |
tmpl, |
|
|
int |
flags |
|
) |
| |
- Parameters
-
| old | link to be changed |
| tmpl | template with requested changes |
| flags | additional netlink message flags |
Builds a new netlink message requesting a change of link attributes. The netlink message header isn't fully equipped with all relevant fields and must be sent out via nl_send_auto_complete() or supplemented as needed. old must point to a link currently configured in the kernel and tmpl must contain the attributes to be changed set via rtnl_link_set_* functions.
- Returns
- New netlink message
- Note
- Not all attributes can be changed, see Changeable Attributes for more details.
Definition at line 965 of file link.c.
970 struct ifinfomsg ifi = {
971 .ifi_family = old->l_family,
972 .ifi_index = old->l_index,
975 if (tmpl->ce_mask & LINK_ATTR_FLAGS) {
976 ifi.ifi_flags = old->l_flags & ~tmpl->l_flag_mask;
977 ifi.ifi_flags |= tmpl->l_flags;
982 goto nla_put_failure;
984 if (
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO) < 0)
985 goto nla_put_failure;
987 if (tmpl->ce_mask & LINK_ATTR_ADDR)
990 if (tmpl->ce_mask & LINK_ATTR_BRD)
993 if (tmpl->ce_mask & LINK_ATTR_MTU)
996 if (tmpl->ce_mask & LINK_ATTR_TXQLEN)
999 if (tmpl->ce_mask & LINK_ATTR_WEIGHT)
1002 if (tmpl->ce_mask & LINK_ATTR_IFNAME)
1005 if (tmpl->ce_mask & LINK_ATTR_OPERSTATE)
1006 NLA_PUT_U8(msg, IFLA_OPERSTATE, tmpl->l_operstate);
1008 if (tmpl->ce_mask & LINK_ATTR_LINKMODE)
1009 NLA_PUT_U8(msg, IFLA_LINKMODE, tmpl->l_linkmode);
1011 if ((tmpl->ce_mask & LINK_ATTR_LINKINFO) && tmpl->l_info_ops &&
1012 tmpl->l_info_ops->io_put_attrs) {
1013 struct nlattr *info;
1016 goto nla_put_failure;
1020 if (tmpl->l_info_ops->io_put_attrs(msg, tmpl) < 0)
1021 goto nla_put_failure;
References nla_nest_end(), nla_nest_start(), NLA_PUT_ADDR, NLA_PUT_STRING, NLA_PUT_U32, NLA_PUT_U8, nlmsg_alloc_simple(), nlmsg_append(), and nlmsg_free().
Referenced by rtnl_link_change().
◆ rtnl_link_change()
| int rtnl_link_change |
( |
struct nl_handle * |
handle, |
|
|
struct rtnl_link * |
old, |
|
|
struct rtnl_link * |
tmpl, |
|
|
int |
flags |
|
) |
| |
◆ rtnl_link_i2name()
| char* rtnl_link_i2name |
( |
struct nl_cache * |
cache, |
|
|
int |
ifindex, |
|
|
char * |
dst, |
|
|
size_t |
len |
|
) |
| |
- Parameters
-
| cache | link cache |
| ifindex | link interface index |
| dst | destination buffer |
| len | length of destination buffer |
Translates the specified interface index to the corresponding link name and stores the name in the destination buffer.
- Returns
- link name or NULL if no match was found.
Definition at line 1085 of file link.c.
1091 strncpy(dst, link->l_name, len - 1);
1092 rtnl_link_put(link);
References rtnl_link_get().
◆ rtnl_link_name2i()
| int rtnl_link_name2i |
( |
struct nl_cache * |
cache, |
|
|
const char * |
name |
|
) |
| |
- Parameters
-
| cache | link cache |
| name | link name |
- Returns
- interface index or RTNL_LINK_NOT_FOUND if no match was found.
Definition at line 1106 of file link.c.
1109 struct rtnl_link *link;
1113 ifindex = link->l_index;
1114 rtnl_link_put(link);
References rtnl_link_get_by_name(), and RTNL_LINK_NOT_FOUND.
◆ rtnl_link_set_info_type()
| int rtnl_link_set_info_type |
( |
struct rtnl_link * |
link, |
|
|
const char * |
type |
|
) |
| |
- Parameters
-
| link | link object |
| type | info type |
Looks up the info type and prepares the link to store info type specific attributes. If an info type has been assigned already it will be released with all changes lost.
- Returns
- 0 on success or a negative errror code.
Definition at line 1501 of file link.c.
1506 if ((io = rtnl_link_info_ops_lookup(type)) == NULL)
1507 return nl_error(ENOENT,
"No such link info type exists");
1509 if (link->l_info_ops)
1510 release_link_info(link);
1512 if ((err = io->
io_alloc(link)) < 0)
1515 link->l_info_ops = io;
◆ rtnl_link_get_info_type()
| char* rtnl_link_get_info_type |
( |
struct rtnl_link * |
link | ) |
|
- Parameters
-
- Note
- The returned pointer is only valid as long as the link exists
- Returns
- Info type name or NULL if unknown.
Definition at line 1527 of file link.c.
1529 if (link->l_info_ops)
1530 return link->l_info_ops->io_name;
int rtnl_link_name2i(struct nl_cache *cache, const char *name)
Translate a link name to the corresponding interface index.
int nl_wait_for_ack(struct nl_handle *handle)
Wait for ACK.
int nla_nest_end(struct nl_msg *n, struct nlattr *start)
Finalize nesting of attributes.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
#define RTNL_LINK_NOT_FOUND
Special interface index stating the link was not found.
int rtnl_link_change(struct nl_handle *handle, struct rtnl_link *old, struct rtnl_link *tmpl, int flags)
Change link attributes.
int rtnl_link_set_info_type(struct rtnl_link *link, const char *type)
Specify the info type of a link.
#define NLA_PUT_U32(n, attrtype, value)
Add a u32 netlink attribute to a netlink message.
int(* io_alloc)(struct rtnl_link *)
Called to assign an info type to a link.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Look up link by interface index in the provided cache.
struct nl_cache * rtnl_link_alloc_cache(struct nl_handle *handle)
Allocate link cache and fill in all configured links.
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
struct nl_msg * rtnl_link_build_change_request(struct rtnl_link *old, struct rtnl_link *tmpl, int flags)
Builds a netlink change request message to change link attributes.
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
#define NLA_PUT_ADDR(n, attrtype, addr)
Add a address attribute to a netlink message.
#define NLA_PUT_STRING(n, attrtype, value)
Add a character string netlink attribute to a netlink message.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
char * rtnl_link_get_info_type(struct rtnl_link *link)
Return info type of a link.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
struct rtnl_link * rtnl_link_get_by_name(struct nl_cache *cache, const char *name)
Look up link by link name in the provided cache.
#define NLA_PUT_U8(n, attrtype, value)
Add a u8 netlink attribute to a netlink message.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
struct nlattr * nla_nest_start(struct nl_msg *n, int attrtype)
Start a new level of nested attributes.