19#include "nl-default.h"
21#include <linux/if_link.h>
22#include <linux/veth.h>
24#include <netlink/netlink.h>
25#include <netlink/attr.h>
26#include <netlink/utils.h>
27#include <netlink/object.h>
28#include <netlink/route/rtnl.h>
29#include <netlink/route/link/veth.h>
38static int veth_parse(
struct rtnl_link *link,
struct nlattr *data,
39 struct nlattr *xstats)
41 struct nlattr *tb[VETH_INFO_MAX+1];
42 struct nlattr *peer_tb[IFLA_MAX + 1];
46 NL_DBG(3,
"Parsing veth link info\n");
51 if (tb[VETH_INFO_PEER]) {
52 struct nlattr *nla_peer;
53 struct ifinfomsg *ifi;
55 nla_peer = tb[VETH_INFO_PEER];
58 peer->l_family = ifi->ifi_family;
59 peer->l_arptype = ifi->ifi_type;
60 peer->l_index = ifi->ifi_index;
61 peer->l_flags = ifi->ifi_flags;
62 peer->l_change = ifi->ifi_change;
63 err =
nla_parse(peer_tb, IFLA_MAX, (
struct nlattr *)
64 ((
char *)
nla_data(nla_peer) +
sizeof(
struct ifinfomsg)),
65 nla_len(nla_peer) -
sizeof(
struct ifinfomsg),
70 err = rtnl_link_info_parse(peer, peer_tb);
92 nl_dump_line(p,
"%s\n", name);
94 nl_dump_line(p,
"%u\n", peer->l_index);
99 struct rtnl_link *dst_peer = NULL, *src_peer = src->l_info;
104 src_peer->l_info = NULL;
108 src_peer->l_info = src;
109 dst_peer->l_info = dst;
111 dst->l_info = dst_peer;
115static int veth_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
118 struct ifinfomsg ifi;
119 struct nlattr *data, *info_peer;
121 memset(&ifi, 0,
sizeof ifi);
122 ifi.ifi_family = peer->l_family;
123 ifi.ifi_type = peer->l_arptype;
124 ifi.ifi_index = peer->l_index;
125 ifi.ifi_flags = peer->l_flags;
126 ifi.ifi_change = peer->l_change;
132 if (
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO) < 0)
134 rtnl_link_fill_info(msg, peer);
141static int veth_alloc(
struct rtnl_link *link)
166static void veth_free(
struct rtnl_link *link)
180 .io_parse = veth_parse,
185 .io_alloc = veth_alloc,
186 .io_clone = veth_clone,
187 .io_put_attrs = veth_put_attrs,
188 .io_free = veth_free,
193#define IS_VETH_LINK_ASSERT(link) \
194 if ((link)->l_info_ops != &veth_info_ops) { \
195 APPBUG("Link is not a veth link. set type \"veth\" first."); \
231 IS_VETH_LINK_ASSERT(link);
254 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"veth");
272 const char *peer_name, pid_t pid)
275 int err = -NLE_NOMEM;
286 rtnl_link_set_ns_pid(peer, pid);
295static void _nl_init veth_init(
void)
300static void _nl_exit veth_exit(
void)
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
struct rtnl_link * rtnl_link_veth_get_peer(struct rtnl_link *link)
Get the peer link of a veth link.
int rtnl_link_veth_add(struct nl_sock *sock, const char *name, const char *peer_name, pid_t pid)
Create a new kernel veth device.
void rtnl_link_veth_release(struct rtnl_link *link)
Release a veth link and its peer.
int rtnl_link_is_veth(struct rtnl_link *link)
Check if link is a veth link.
struct rtnl_link * rtnl_link_veth_alloc(void)
Allocate link object of type veth.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
uint16_t type
Type of attribute or NLA_UNSPEC.
Available operations to modules implementing a link info type.
char * io_name
Name of link info type, must match name on kernel side.