15#include <netlink-private/netlink.h>
16#include <netlink/netlink.h>
17#include <netlink/attr.h>
18#include <netlink/utils.h>
19#include <netlink/object.h>
20#include <netlink/hashtable.h>
21#include <netlink/data.h>
22#include <netlink/route/rtnl.h>
23#include <netlink/route/link.h>
24#include <netlink-private/route/link/api.h>
25#include <netlink-private/route/link/sriov.h>
26#include <netlink-private/utils.h>
29#define LINK_ATTR_MTU (1 << 0)
30#define LINK_ATTR_LINK (1 << 1)
31#define LINK_ATTR_TXQLEN (1 << 2)
32#define LINK_ATTR_WEIGHT (1 << 3)
33#define LINK_ATTR_MASTER (1 << 4)
34#define LINK_ATTR_QDISC (1 << 5)
35#define LINK_ATTR_MAP (1 << 6)
36#define LINK_ATTR_ADDR (1 << 7)
37#define LINK_ATTR_BRD (1 << 8)
38#define LINK_ATTR_FLAGS (1 << 9)
39#define LINK_ATTR_IFNAME (1 << 10)
40#define LINK_ATTR_IFINDEX (1 << 11)
41#define LINK_ATTR_FAMILY (1 << 12)
42#define LINK_ATTR_ARPTYPE (1 << 13)
43#define LINK_ATTR_STATS (1 << 14)
44#define LINK_ATTR_CHANGE (1 << 15)
45#define LINK_ATTR_OPERSTATE (1 << 16)
46#define LINK_ATTR_LINKMODE (1 << 17)
47#define LINK_ATTR_LINKINFO (1 << 18)
48#define LINK_ATTR_IFALIAS (1 << 19)
49#define LINK_ATTR_NUM_VF (1 << 20)
50#define LINK_ATTR_PROMISCUITY (1 << 21)
51#define LINK_ATTR_NUM_TX_QUEUES (1 << 22)
52#define LINK_ATTR_NUM_RX_QUEUES (1 << 23)
53#define LINK_ATTR_GROUP (1 << 24)
54#define LINK_ATTR_CARRIER (1 << 25)
55#define LINK_ATTR_PROTINFO (1 << 26)
56#define LINK_ATTR_AF_SPEC (1 << 27)
57#define LINK_ATTR_PHYS_PORT_ID (1 << 28)
58#define LINK_ATTR_NS_FD (1 << 29)
59#define LINK_ATTR_NS_PID (1 << 30)
61#define LINK_ATTR_LINK_NETNSID ((uint64_t) 1 << 32)
62#define LINK_ATTR_VF_LIST ((uint64_t) 1 << 33)
63#define LINK_ATTR_CARRIER_CHANGES ((uint64_t) 1 << 34)
64#define LINK_ATTR_PHYS_PORT_NAME ((uint64_t) 1 << 35)
65#define LINK_ATTR_PHYS_SWITCH_ID ((uint64_t) 1 << 36)
66#define LINK_ATTR_GSO_MAX_SEGS ((uint64_t) 1 << 37)
67#define LINK_ATTR_GSO_MAX_SIZE ((uint64_t) 1 << 38)
68#define LINK_ATTR_LINKINFO_SLAVE_KIND ((uint64_t) 1 << 39)
70static struct nl_cache_ops rtnl_link_ops;
71static struct nl_object_ops link_obj_ops;
74struct rtnl_link *link_lookup(
struct nl_cache *cache,
int ifindex)
77 cache = __nl_cache_mngt_require(
"route/link");
85static struct rtnl_link_af_ops *af_lookup_and_alloc(
struct rtnl_link *link,
88 struct rtnl_link_af_ops *af_ops;
102static int af_free(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
103 void *data,
void *arg)
106 ops->ao_free(link, data);
113static int af_request_type(
int af_type,
struct rtnl_link *changes)
115 struct rtnl_link_af_ops *ops;
118 if (ops && ops->ao_override_rtm(changes))
124static int af_clone(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
125 void *data,
void *arg)
130 !(dst->l_af_data[ops->ao_family] = ops->ao_clone(dst, data)))
136static int af_fill(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
137 void *data,
void *arg)
139 struct nl_msg *msg = arg;
140 struct nlattr *af_attr = NULL;
143 if (!ops->ao_fill_af)
146 if (!ops->ao_fill_af_no_nest)
150 if ((err = ops->ao_fill_af(link, arg, data)) < 0)
153 if (!ops->ao_fill_af_no_nest)
159static int af_fill_pi(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
160 void *data,
void *arg)
162 struct nl_msg *msg = arg;
163 struct nlattr *pi_attr;
164 int err, pi_type = IFLA_PROTINFO;
166 if (!ops->ao_fill_pi)
169 if (ops->ao_fill_pi_flags > 0)
170 pi_type |= ops->ao_fill_pi_flags;
175 if ((err = ops->ao_fill_pi(link, arg, data)) < 0)
183static int af_dump_line(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
184 void *data,
void *arg)
194static int af_dump_details(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
195 void *data,
void *arg)
205static int af_dump_stats(
struct rtnl_link *link,
struct rtnl_link_af_ops *ops,
206 void *data,
void *arg)
216static int do_foreach_af(
struct rtnl_link *link,
218 struct rtnl_link_af_ops *,
void *,
void *),
223 for (i = 0; i < AF_MAX; i++) {
224 if (link->l_af_data[i]) {
225 _nl_auto_rtnl_link_af_ops
struct rtnl_link_af_ops *ops = NULL;
230 err = cb(link, ops, link->l_af_data[i], arg);
239static void release_link_info(
struct rtnl_link *link)
241 struct rtnl_link_info_ops *io = link->l_info_ops;
248 BUG_ON(link->l_info);
251 link->l_info_ops = NULL;
255static void link_free_data(
struct nl_object *c)
257 struct rtnl_link *link = nl_object_priv(c);
260 release_link_info(link);
268 free(link->l_ifalias);
269 free(link->l_info_kind);
270 free(link->l_info_slave_kind);
272 do_foreach_af(link, af_free, NULL);
277 if (link->ce_mask & LINK_ATTR_VF_LIST)
278 rtnl_link_sriov_free_data(link);
282static int link_clone(
struct nl_object *_dst,
struct nl_object *_src)
284 struct rtnl_link *dst = nl_object_priv(_dst);
285 struct rtnl_link *src = nl_object_priv(_src);
290 dst->l_info_kind = NULL;
291 dst->l_info_slave_kind = NULL;
292 dst->l_info_ops = NULL;
293 memset(dst->l_af_data, 0, sizeof (dst->l_af_data));
295 dst->l_ifalias = NULL;
296 dst->l_af_ops = NULL;
297 dst->l_phys_port_id = NULL;
298 dst->l_phys_switch_id = NULL;
299 dst->l_vf_list = NULL;
310 if (!(dst->l_ifalias = strdup(src->l_ifalias)))
313 if (src->l_info_kind)
314 if (!(dst->l_info_kind = strdup(src->l_info_kind)))
317 if (src->l_info_slave_kind)
318 if (!(dst->l_info_slave_kind = strdup(src->l_info_slave_kind)))
321 if (src->l_info_ops) {
324 dst->l_info_ops = src->l_info_ops;
326 if (src->l_info_ops->io_clone) {
327 err = src->l_info_ops->io_clone(dst, src);
333 if ((err = do_foreach_af(src, af_clone, dst)) < 0)
337 dst->l_af_ops = af_lookup_and_alloc(dst, src->l_af_ops->ao_family);
339 if (src->l_phys_port_id)
340 if (!(dst->l_phys_port_id =
nl_data_clone(src->l_phys_port_id)))
343 if (src->l_phys_switch_id)
344 if (!(dst->l_phys_switch_id =
nl_data_clone(src->l_phys_switch_id)))
347 if (src->ce_mask & LINK_ATTR_VF_LIST)
348 if ((err = rtnl_link_sriov_clone(dst, src)) < 0)
354struct nla_policy rtln_link_policy[IFLA_MAX+1] = {
356 .maxlen = IFNAMSIZ },
357 [IFLA_MTU] = { .type =
NLA_U32 },
358 [IFLA_TXQLEN] = { .type =
NLA_U32 },
359 [IFLA_LINK] = { .type =
NLA_U32 },
360 [IFLA_WEIGHT] = { .type =
NLA_U32 },
361 [IFLA_MASTER] = { .type =
NLA_U32 },
362 [IFLA_OPERSTATE] = { .type =
NLA_U8 },
363 [IFLA_LINKMODE] = { .type =
NLA_U8 },
366 .maxlen = IFQDISCSIZ },
367 [IFLA_STATS] = { .minlen = _nl_offsetofend (
struct rtnl_link_stats, tx_compressed) },
368 [IFLA_STATS64] = { .minlen = _nl_offsetofend (
struct rtnl_link_stats64, tx_compressed) },
369 [IFLA_MAP] = { .minlen =
sizeof(
struct rtnl_link_ifmap) },
370 [IFLA_IFALIAS] = { .type =
NLA_STRING, .maxlen = IFALIASZ },
371 [IFLA_NUM_VF] = { .type =
NLA_U32 },
374 [IFLA_PROMISCUITY] = { .type =
NLA_U32 },
375 [IFLA_NUM_TX_QUEUES] = { .type =
NLA_U32 },
376 [IFLA_NUM_RX_QUEUES] = { .type =
NLA_U32 },
377 [IFLA_GSO_MAX_SEGS] = { .type =
NLA_U32 },
378 [IFLA_GSO_MAX_SIZE] = { .type =
NLA_U32 },
379 [IFLA_GROUP] = { .type =
NLA_U32 },
380 [IFLA_CARRIER] = { .type =
NLA_U8 },
381 [IFLA_CARRIER_CHANGES] = { .type =
NLA_U32 },
383 [IFLA_PHYS_PORT_NAME] = { .type =
NLA_STRING, .maxlen = IFNAMSIZ },
384 [IFLA_PHYS_SWITCH_ID] = { .type =
NLA_UNSPEC },
385 [IFLA_NET_NS_PID] = { .type =
NLA_U32 },
386 [IFLA_NET_NS_FD] = { .type =
NLA_U32 },
389static struct nla_policy link_info_policy[IFLA_INFO_MAX+1] = {
395int rtnl_link_info_parse(
struct rtnl_link *link,
struct nlattr **tb)
397 if (tb[IFLA_IFNAME] == NULL)
398 return -NLE_MISSING_ATTR;
400 nla_strlcpy(link->l_name, tb[IFLA_IFNAME], IFNAMSIZ);
401 link->ce_mask |= LINK_ATTR_IFNAME;
403 if (tb[IFLA_STATS]) {
404 struct rtnl_link_stats *st =
nla_data(tb[IFLA_STATS]);
436 if (
nla_len(tb[IFLA_STATS]) >= _nl_offsetofend (
struct rtnl_link_stats, rx_nohandler))
441 link->ce_mask |= LINK_ATTR_STATS;
444 if (tb[IFLA_STATS64]) {
452 struct rtnl_link_stats64 st = { 0 };
454 nla_memcpy(&st, tb[IFLA_STATS64],
sizeof (st));
488 link->ce_mask |= LINK_ATTR_STATS;
491 if (tb[IFLA_TXQLEN]) {
493 link->ce_mask |= LINK_ATTR_TXQLEN;
498 link->ce_mask |= LINK_ATTR_MTU;
501 if (tb[IFLA_ADDRESS]) {
503 if (link->l_addr == NULL)
507 link->ce_mask |= LINK_ATTR_ADDR;
510 if (tb[IFLA_BROADCAST]) {
513 if (link->l_bcast == NULL)
517 link->ce_mask |= LINK_ATTR_BRD;
522 link->ce_mask |= LINK_ATTR_LINK;
525 if (tb[IFLA_LINK_NETNSID]) {
526 link->l_link_netnsid =
nla_get_s32(tb[IFLA_LINK_NETNSID]);
527 link->ce_mask |= LINK_ATTR_LINK_NETNSID;
530 if (tb[IFLA_WEIGHT]) {
532 link->ce_mask |= LINK_ATTR_WEIGHT;
535 if (tb[IFLA_QDISC]) {
536 nla_strlcpy(link->l_qdisc, tb[IFLA_QDISC], IFQDISCSIZ);
537 link->ce_mask |= LINK_ATTR_QDISC;
542 sizeof(
struct rtnl_link_ifmap));
543 link->ce_mask |= LINK_ATTR_MAP;
546 if (tb[IFLA_MASTER]) {
548 link->ce_mask |= LINK_ATTR_MASTER;
551 if (tb[IFLA_CARRIER]) {
552 link->l_carrier =
nla_get_u8(tb[IFLA_CARRIER]);
553 link->ce_mask |= LINK_ATTR_CARRIER;
556 if (tb[IFLA_CARRIER_CHANGES]) {
557 link->l_carrier_changes =
nla_get_u32(tb[IFLA_CARRIER_CHANGES]);
558 link->ce_mask |= LINK_ATTR_CARRIER_CHANGES;
561 if (tb[IFLA_OPERSTATE]) {
562 link->l_operstate =
nla_get_u8(tb[IFLA_OPERSTATE]);
563 link->ce_mask |= LINK_ATTR_OPERSTATE;
566 if (tb[IFLA_LINKMODE]) {
567 link->l_linkmode =
nla_get_u8(tb[IFLA_LINKMODE]);
568 link->ce_mask |= LINK_ATTR_LINKMODE;
571 if (tb[IFLA_IFALIAS]) {
572 link->l_ifalias = nla_strdup(tb[IFLA_IFALIAS]);
573 if (link->l_ifalias == NULL)
575 link->ce_mask |= LINK_ATTR_IFALIAS;
578 if (tb[IFLA_NET_NS_FD]) {
580 link->ce_mask |= LINK_ATTR_NS_FD;
583 if (tb[IFLA_NET_NS_PID]) {
585 link->ce_mask |= LINK_ATTR_NS_PID;
591static int link_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
592 struct nlmsghdr *n,
struct nl_parser_param *pp)
594 _nl_auto_rtnl_link
struct rtnl_link *link = NULL;
595 struct nla_policy real_link_policy[ARRAY_SIZE(rtln_link_policy)];
596 struct nla_policy *link_policy = rtln_link_policy;
597 struct rtnl_link_af_ops *af_ops_family;
598 struct ifinfomsg *ifi;
599 struct nlattr *tb[IFLA_MAX+1];
606 link->ce_msgtype = n->nlmsg_type;
608 if (!nlmsg_valid_hdr(n,
sizeof(*ifi)))
609 return -NLE_MSG_TOOSHORT;
612 link->l_family = family = ifi->ifi_family;
613 link->l_arptype = ifi->ifi_type;
614 link->l_index = ifi->ifi_index;
615 link->l_flags = ifi->ifi_flags;
616 link->l_change = ifi->ifi_change;
617 link->ce_mask = (LINK_ATTR_FAMILY |
618 LINK_ATTR_ARPTYPE| LINK_ATTR_IFINDEX |
619 LINK_ATTR_FLAGS | LINK_ATTR_CHANGE);
621 if ((link->l_af_ops = af_lookup_and_alloc(link, family))) {
622 if (link->l_af_ops->ao_protinfo_policy) {
623 _NL_STATIC_ASSERT (
sizeof(rtln_link_policy) ==
sizeof(real_link_policy));
624 memcpy(&real_link_policy, rtln_link_policy,
sizeof(rtln_link_policy));
625 memcpy(&real_link_policy[IFLA_PROTINFO],
626 link->l_af_ops->ao_protinfo_policy,
628 link_policy = real_link_policy;
632 af_ops_family = link->l_af_ops;
634 err =
nlmsg_parse(n,
sizeof(*ifi), tb, IFLA_MAX, link_policy);
638 err = rtnl_link_info_parse(link, tb);
642 if (tb[IFLA_NUM_VF]) {
644 link->ce_mask |= LINK_ATTR_NUM_VF;
645 if (link->l_num_vf && tb[IFLA_VFINFO_LIST]) {
646 if ((err = rtnl_link_sriov_parse_vflist(link, tb)) < 0)
648 link->ce_mask |= LINK_ATTR_VF_LIST;
652 if (tb[IFLA_LINKINFO]) {
653 struct nlattr *li[IFLA_INFO_MAX+1];
660 if (li[IFLA_INFO_KIND]) {
661 struct rtnl_link_info_ops *ops;
669 if ( (af = nl_str2af(kind)) >= 0
671 && (link->l_af_ops = af_lookup_and_alloc(link, af))) {
673 if (link->l_af_ops->ao_protinfo_policy)
674 tb[IFLA_PROTINFO] = (
struct nlattr *)link->l_af_ops->ao_protinfo_policy;
677 if (link->l_info_ops)
678 release_link_info(link);
681 link->l_info_ops = ops;
685 (li[IFLA_INFO_DATA] || li[IFLA_INFO_XSTATS])) {
686 err = ops->io_parse(link, li[IFLA_INFO_DATA],
687 li[IFLA_INFO_XSTATS]);
695 link->ce_mask |= LINK_ATTR_LINKINFO;
698 if (li[IFLA_INFO_SLAVE_KIND]) {
705 link->ce_mask |= LINK_ATTR_LINKINFO_SLAVE_KIND;
709 if ( tb[IFLA_PROTINFO]
711 && link->l_af_ops->ao_parse_protinfo) {
712 err = link->l_af_ops->ao_parse_protinfo(link, tb[IFLA_PROTINFO],
713 link->l_af_data[link->l_family]);
716 link->ce_mask |= LINK_ATTR_PROTINFO;
719 if (tb[IFLA_AF_SPEC]) {
724 && af_ops_family->ao_parse_af_full) {
725 err = af_ops_family->ao_parse_af_full(link,
727 link->l_af_data[af_ops_family->ao_family]);
730 link->ce_mask |= LINK_ATTR_AF_SPEC;
731 }
else if (family == AF_UNSPEC) {
732 struct nlattr *af_attr;
736 _nl_auto_rtnl_link_af_ops
struct rtnl_link_af_ops *af_ops = NULL;
738 af_ops = af_lookup_and_alloc(link,
nla_type(af_attr));
739 if (af_ops && af_ops->ao_parse_af) {
740 char *af_data = link->l_af_data[
nla_type(af_attr)];
742 err = af_ops->ao_parse_af(link, af_attr, af_data);
747 link->ce_mask |= LINK_ATTR_AF_SPEC;
749 NL_DBG(3,
"IFLA_AF_SPEC parsing not implemented for family %d\n",
754 if (tb[IFLA_PROMISCUITY]) {
755 link->l_promiscuity =
nla_get_u32(tb[IFLA_PROMISCUITY]);
756 link->ce_mask |= LINK_ATTR_PROMISCUITY;
759 if (tb[IFLA_NUM_TX_QUEUES]) {
760 link->l_num_tx_queues =
nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
761 link->ce_mask |= LINK_ATTR_NUM_TX_QUEUES;
764 if (tb[IFLA_NUM_RX_QUEUES]) {
765 link->l_num_rx_queues =
nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
766 link->ce_mask |= LINK_ATTR_NUM_RX_QUEUES;
769 if (tb[IFLA_GSO_MAX_SEGS]) {
770 link->l_gso_max_segs =
nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
771 link->ce_mask |= LINK_ATTR_GSO_MAX_SEGS;
774 if (tb[IFLA_GSO_MAX_SIZE]) {
775 link->l_gso_max_size =
nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
776 link->ce_mask |= LINK_ATTR_GSO_MAX_SIZE;
779 if (tb[IFLA_GROUP]) {
781 link->ce_mask |= LINK_ATTR_GROUP;
784 if (tb[IFLA_PHYS_PORT_ID]) {
786 if (link->l_phys_port_id == NULL)
788 link->ce_mask |= LINK_ATTR_PHYS_PORT_ID;
791 if (tb[IFLA_PHYS_PORT_NAME]) {
792 nla_strlcpy(link->l_phys_port_name, tb[IFLA_PHYS_PORT_NAME], IFNAMSIZ);
793 link->ce_mask |= LINK_ATTR_PHYS_PORT_NAME;
796 if (tb[IFLA_PHYS_SWITCH_ID]) {
798 if (link->l_phys_switch_id == NULL)
800 link->ce_mask |= LINK_ATTR_PHYS_SWITCH_ID;
803 return pp->pp_cb((
struct nl_object *) link, pp);
806static int link_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
808 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
809 int family = cache->c_iarg1;
810 struct ifinfomsg hdr = { .ifi_family = family };
811 struct rtnl_link_af_ops *ops;
813 __u32 ext_filter_mask = RTEXT_FILTER_VF;
819 if (
nlmsg_append(msg, &hdr,
sizeof(hdr), NLMSG_ALIGNTO) < 0)
823 if (ops && ops->ao_get_af) {
824 err = ops->ao_get_af(msg, &ext_filter_mask);
829 if (ext_filter_mask) {
830 err =
nla_put(msg, IFLA_EXT_MASK,
sizeof(ext_filter_mask), &ext_filter_mask);
842static void link_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
845 struct nl_cache *cache = obj->ce_cache;
847 int fetched_cache = 0;
854 if (link->l_family != AF_UNSPEC)
855 nl_dump_line(p,
"%s ", nl_af2str(link->l_family, buf,
sizeof(buf)));
857 nl_dump_line(p,
"%s %s ", link->l_name,
858 nl_llproto2str(link->l_arptype, buf,
sizeof(buf)));
863 if (link->ce_mask & LINK_ATTR_MASTER) {
867 nl_dump(p,
"master %s ", master ? master->l_name :
"inv");
869 nl_dump(p,
"master %d ", link->l_master);
872 rtnl_link_flags2str(link->l_flags, buf,
sizeof(buf));
876 if (link->ce_mask & LINK_ATTR_LINK) {
878 && !(link->ce_mask & LINK_ATTR_LINK_NETNSID)) {
881 nl_dump(p,
"slave-of %s ", ll ? ll->l_name :
"NONE");
883 nl_dump(p,
"slave-of %d ", link->l_link);
885 if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
886 nl_dump(p,
"link-netnsid %d ", link->l_link_netnsid);
888 if (link->ce_mask & LINK_ATTR_GROUP)
889 nl_dump(p,
"group %u ", link->l_group);
891 if (link->l_info_ops && link->l_info_ops->io_dump[
NL_DUMP_LINE])
894 do_foreach_af(link, af_dump_line, p);
902static void link_dump_details(
struct nl_object *obj,
struct nl_dump_params *p)
907 link_dump_line(obj, p);
909 nl_dump_line(p,
" mtu %u ", link->l_mtu);
910 nl_dump(p,
"txqlen %u weight %u ", link->l_txqlen, link->l_weight);
912 if (link->ce_mask & LINK_ATTR_QDISC)
913 nl_dump(p,
"qdisc %s ", link->l_qdisc);
915 if (link->ce_mask & LINK_ATTR_MAP && link->l_map.lm_irq)
916 nl_dump(p,
"irq %u ", link->l_map.lm_irq);
918 if (link->ce_mask & LINK_ATTR_IFINDEX)
919 nl_dump(p,
"index %u ", link->l_index);
921 if (link->ce_mask & LINK_ATTR_PROMISCUITY && link->l_promiscuity > 0)
922 nl_dump(p,
"promisc-mode (%u users) ", link->l_promiscuity);
926 if (link->ce_mask & LINK_ATTR_IFALIAS)
927 nl_dump_line(p,
" alias %s\n", link->l_ifalias);
929 nl_dump_line(p,
" ");
931 if (link->ce_mask & LINK_ATTR_NUM_TX_QUEUES)
932 nl_dump(p,
"txq %u ", link->l_num_tx_queues);
934 if (link->ce_mask & LINK_ATTR_NUM_RX_QUEUES)
935 nl_dump(p,
"rxq %u ", link->l_num_rx_queues);
937 if (link->ce_mask & LINK_ATTR_BRD)
941 if ((link->ce_mask & LINK_ATTR_OPERSTATE) &&
942 link->l_operstate != IF_OPER_UNKNOWN) {
943 rtnl_link_operstate2str(link->l_operstate, buf,
sizeof(buf));
947 if (link->ce_mask & LINK_ATTR_NUM_VF)
948 nl_dump(p,
"num-vf %u ", link->l_num_vf);
951 rtnl_link_mode2str(link->l_linkmode, buf,
sizeof(buf)));
954 rtnl_link_carrier2str(link->l_carrier, buf,
sizeof(buf)));
956 if (link->ce_mask & LINK_ATTR_CARRIER_CHANGES)
957 nl_dump(p,
" carrier-changes %u", link->l_carrier_changes);
964 do_foreach_af(link, af_dump_details, p);
966 if (link->ce_mask & LINK_ATTR_VF_LIST)
967 rtnl_link_sriov_dump_details(link, p);
970static void link_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
976 link_dump_details(obj, p);
978 nl_dump_line(p,
" Stats: bytes packets errors "
979 " dropped fifo-err compressed\n");
983 strcpy(fmt,
" RX %X.2f %s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
"\n");
984 fmt[9] = *unit ==
'B' ?
'9' :
'7';
986 nl_dump_line(p, fmt, res, unit,
995 strcpy(fmt,
" TX %X.2f %s %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
" %10" PRIu64
"\n");
996 fmt[9] = *unit ==
'B' ?
'9' :
'7';
998 nl_dump_line(p, fmt, res, unit,
1005 nl_dump_line(p,
" Errors: length over crc "
1006 " frame missed multicast\n");
1008 nl_dump_line(p,
" RX %10" PRIu64
" %10" PRIu64
" %10"
1009 PRIu64
" %10" PRIu64
" %10" PRIu64
" %10"
1018 nl_dump_line(p,
" aborted carrier heartbeat "
1019 " window collision\n");
1021 nl_dump_line(p,
" TX %10" PRIu64
" %10" PRIu64
" %10"
1022 PRIu64
" %10" PRIu64
" %10" PRIu64
"\n",
1029 if (link->l_info_ops && link->l_info_ops->io_dump[
NL_DUMP_STATS])
1032 do_foreach_af(link, af_dump_stats, p);
1034 if (link->ce_mask & LINK_ATTR_VF_LIST)
1035 rtnl_link_sriov_dump_stats(link, p);
1039static int link_handle_event(
struct nl_object *a,
struct rtnl_link_event_cb *cb)
1042 struct nl_cache *c = dp_cache(a);
1045 if (l->l_change == ~0U) {
1046 if (l->ce_msgtype == RTM_NEWLINK)
1049 cb->le_unregister(l);
1054 if (l->l_change & IFF_SLAVE) {
1055 if (l->l_flags & IFF_SLAVE) {
1057 cb->le_new_bonding(l, m);
1061 cb->le_cancel_bonding(l);
1065 if (l->l_change & IFF_UP && l->l_change & IFF_RUNNING)
1066 dp_dump_line(p, line++,
"link %s changed state to %s.\n",
1067 l->l_name, l->l_flags & IFF_UP ?
"up" :
"down");
1069 if (l->l_change & IFF_PROMISC) {
1070 dp_new_line(p, line++);
1071 dp_dump(p,
"link %s %s promiscuous mode.\n",
1072 l->l_name, l->l_flags & IFF_PROMISC ?
"entered" :
"left");
1076 dp_dump_line(p, line++,
"link %s sent unknown event.\n",
1085static void link_keygen(
struct nl_object *obj, uint32_t *hashkey,
1089 unsigned int lkey_sz;
1090 struct link_hash_key {
1093 } __attribute__((packed)) lkey;
1095 lkey_sz =
sizeof(lkey);
1096 lkey.l_index = link->l_index;
1097 lkey.l_family = link->l_family;
1099 *hashkey = nl_hash(&lkey, lkey_sz, 0) % table_sz;
1101 NL_DBG(5,
"link %p key (dev %d fam %d) keysz %d, hash 0x%x\n",
1102 link, lkey.l_index, lkey.l_family, lkey_sz, *hashkey);
1107static uint64_t link_compare(
struct nl_object *_a,
struct nl_object *_b,
1108 uint64_t attrs,
int flags)
1114#define LINK_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, LINK_ATTR_##ATTR, a, b, EXPR)
1116 diff |= LINK_DIFF(IFINDEX, a->l_index != b->l_index);
1117 diff |= LINK_DIFF(MTU, a->l_mtu != b->l_mtu);
1118 diff |= LINK_DIFF(LINK, a->l_link != b->l_link);
1119 diff |= LINK_DIFF(LINK_NETNSID, a->l_link_netnsid != b->l_link_netnsid);
1120 diff |= LINK_DIFF(TXQLEN, a->l_txqlen != b->l_txqlen);
1121 diff |= LINK_DIFF(WEIGHT, a->l_weight != b->l_weight);
1122 diff |= LINK_DIFF(MASTER, a->l_master != b->l_master);
1123 diff |= LINK_DIFF(FAMILY, a->l_family != b->l_family);
1124 diff |= LINK_DIFF(OPERSTATE, a->l_operstate != b->l_operstate);
1125 diff |= LINK_DIFF(LINKMODE, a->l_linkmode != b->l_linkmode);
1126 diff |= LINK_DIFF(QDISC, strcmp(a->l_qdisc, b->l_qdisc));
1127 diff |= LINK_DIFF(IFNAME, strcmp(a->l_name, b->l_name));
1128 diff |= LINK_DIFF(ADDR,
nl_addr_cmp(a->l_addr, b->l_addr));
1129 diff |= LINK_DIFF(BRD,
nl_addr_cmp(a->l_bcast, b->l_bcast));
1130 diff |= LINK_DIFF(IFALIAS, strcmp(a->l_ifalias, b->l_ifalias));
1131 diff |= LINK_DIFF(NUM_VF, a->l_num_vf != b->l_num_vf);
1132 diff |= LINK_DIFF(PROMISCUITY, a->l_promiscuity != b->l_promiscuity);
1133 diff |= LINK_DIFF(NUM_TX_QUEUES,a->l_num_tx_queues != b->l_num_tx_queues);
1134 diff |= LINK_DIFF(NUM_RX_QUEUES,a->l_num_rx_queues != b->l_num_rx_queues);
1135 diff |= LINK_DIFF(GROUP, a->l_group != b->l_group);
1137 if (flags & LOOSE_COMPARISON)
1138 diff |= LINK_DIFF(FLAGS,
1139 (a->l_flags ^ b->l_flags) & b->l_flag_mask);
1141 diff |= LINK_DIFF(FLAGS, a->l_flags != b->l_flags);
1146 if (a->l_family == b->l_family) {
1148 goto protinfo_mismatch;
1156 diff |= LINK_DIFF(PROTINFO, 1);
1162static const struct trans_tbl link_attrs[] = {
1163 __ADD(LINK_ATTR_MTU, mtu),
1164 __ADD(LINK_ATTR_LINK, link),
1165 __ADD(LINK_ATTR_TXQLEN, txqlen),
1166 __ADD(LINK_ATTR_WEIGHT, weight),
1167 __ADD(LINK_ATTR_MASTER, master),
1168 __ADD(LINK_ATTR_QDISC, qdisc),
1169 __ADD(LINK_ATTR_MAP, map),
1170 __ADD(LINK_ATTR_ADDR, address),
1171 __ADD(LINK_ATTR_BRD, broadcast),
1172 __ADD(LINK_ATTR_FLAGS, flags),
1173 __ADD(LINK_ATTR_IFNAME, name),
1174 __ADD(LINK_ATTR_IFINDEX, ifindex),
1175 __ADD(LINK_ATTR_FAMILY, family),
1176 __ADD(LINK_ATTR_ARPTYPE, arptype),
1177 __ADD(LINK_ATTR_STATS, stats),
1178 __ADD(LINK_ATTR_CHANGE, change),
1179 __ADD(LINK_ATTR_OPERSTATE, operstate),
1180 __ADD(LINK_ATTR_LINKMODE, linkmode),
1181 __ADD(LINK_ATTR_IFALIAS, ifalias),
1182 __ADD(LINK_ATTR_NUM_VF, num_vf),
1183 __ADD(LINK_ATTR_PROMISCUITY, promiscuity),
1184 __ADD(LINK_ATTR_NUM_TX_QUEUES, num_tx_queues),
1185 __ADD(LINK_ATTR_NUM_RX_QUEUES, num_rx_queues),
1186 __ADD(LINK_ATTR_GSO_MAX_SEGS, gso_max_segs),
1187 __ADD(LINK_ATTR_GSO_MAX_SIZE, gso_max_size),
1188 __ADD(LINK_ATTR_GROUP, group),
1189 __ADD(LINK_ATTR_CARRIER, carrier),
1190 __ADD(LINK_ATTR_CARRIER_CHANGES, carrier_changes),
1191 __ADD(LINK_ATTR_PHYS_PORT_ID, phys_port_id),
1192 __ADD(LINK_ATTR_PHYS_PORT_NAME, phys_port_name),
1193 __ADD(LINK_ATTR_PHYS_SWITCH_ID, phys_switch_id),
1194 __ADD(LINK_ATTR_NS_FD, ns_fd),
1195 __ADD(LINK_ATTR_NS_PID, ns_pid),
1196 __ADD(LINK_ATTR_LINK_NETNSID, link_netnsid),
1199static char *link_attrs2str(
int attrs,
char *buf,
size_t len)
1201 return __flags2str(attrs, buf, len, link_attrs,
1202 ARRAY_SIZE(link_attrs));
1237 struct nl_cache **result,
unsigned int flags)
1239 struct nl_cache * cache;
1246 cache->c_iarg1 = family;
1309 if (cache->c_ops != &rtnl_link_ops)
1312 nl_list_for_each_entry(link, &cache->c_items, ce_list) {
1313 if (link->l_index == ifindex) {
1342 if (cache->c_ops != &rtnl_link_ops)
1345 nl_list_for_each_entry(link, &cache->c_items, ce_list) {
1346 if (!strcmp(name, link->l_name)) {
1370 struct nl_msg **result)
1372 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
1373 struct ifinfomsg ifi;
1374 __u32 vf_mask = RTEXT_FILTER_VF;
1376 if (ifindex <= 0 && !name) {
1377 APPBUG(
"ifindex or name must be specified");
1378 return -NLE_MISSING_ATTR;
1381 memset(&ifi, 0,
sizeof(ifi));
1387 ifi.ifi_index = ifindex;
1389 _NL_RETURN_ON_PUT_ERR(
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO));
1394 _NL_RETURN_ON_PUT_ERR(
nla_put(msg, IFLA_EXT_MASK,
sizeof(vf_mask), &vf_mask));
1396 *result = _nl_steal_pointer(&msg);
1424 _nl_auto_rtnl_link
struct rtnl_link *link = NULL;
1425 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
1438 if ( syserr == -EINVAL
1446 return -NLE_OPNOTSUPP;
1452 if (err == 0 && link)
1455 *result = _nl_steal_pointer(&link);
1476 _nl_auto_rtnl_link
struct rtnl_link *link = NULL;
1480 _nl_strncpy_trunc(dst, link->l_name, len);
1498 _nl_auto_rtnl_link
struct rtnl_link *link = NULL;
1502 return link->l_index;
1509int rtnl_link_fill_info(
struct nl_msg *msg,
struct rtnl_link *link)
1511 if (link->ce_mask & LINK_ATTR_ADDR)
1514 if (link->ce_mask & LINK_ATTR_BRD)
1517 if (link->ce_mask & LINK_ATTR_MTU)
1520 if (link->ce_mask & LINK_ATTR_TXQLEN)
1523 if (link->ce_mask & LINK_ATTR_WEIGHT)
1526 if (link->ce_mask & LINK_ATTR_IFNAME)
1529 if (link->ce_mask & LINK_ATTR_OPERSTATE)
1530 NLA_PUT_U8(msg, IFLA_OPERSTATE, link->l_operstate);
1532 if (link->ce_mask & LINK_ATTR_CARRIER)
1533 NLA_PUT_U8(msg, IFLA_CARRIER, link->l_carrier);
1535 if (link->ce_mask & LINK_ATTR_LINKMODE)
1536 NLA_PUT_U8(msg, IFLA_LINKMODE, link->l_linkmode);
1538 if (link->ce_mask & LINK_ATTR_IFALIAS)
1541 if (link->ce_mask & LINK_ATTR_LINK)
1544 if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
1545 NLA_PUT_S32(msg, IFLA_LINK_NETNSID, link->l_link_netnsid);
1547 if (link->ce_mask & LINK_ATTR_MASTER)
1550 if (link->ce_mask & LINK_ATTR_NUM_TX_QUEUES)
1551 NLA_PUT_U32(msg, IFLA_NUM_TX_QUEUES, link->l_num_tx_queues);
1553 if (link->ce_mask & LINK_ATTR_NUM_RX_QUEUES)
1554 NLA_PUT_U32(msg, IFLA_NUM_RX_QUEUES, link->l_num_rx_queues);
1556 if (link->ce_mask & LINK_ATTR_NS_FD)
1559 if (link->ce_mask & LINK_ATTR_NS_PID)
1560 NLA_PUT_U32(msg, IFLA_NET_NS_PID, link->l_ns_pid);
1565 return -NLE_MSGSIZE;
1568static int build_link_msg(
int cmd,
struct ifinfomsg *hdr,
1569 struct rtnl_link *link,
int flags,
struct nl_msg **result)
1571 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
1572 struct nlattr *af_spec;
1578 if (
nlmsg_append(msg, hdr,
sizeof(*hdr), NLMSG_ALIGNTO) < 0)
1579 goto nla_put_failure;
1581 if (rtnl_link_fill_info(msg, link))
1582 goto nla_put_failure;
1584 if (link->ce_mask & LINK_ATTR_GROUP)
1587 if (link->ce_mask & (LINK_ATTR_LINKINFO|LINK_ATTR_LINKINFO_SLAVE_KIND)) {
1588 struct nlattr *info;
1591 goto nla_put_failure;
1593 if (link->ce_mask & LINK_ATTR_LINKINFO) {
1596 if (link->l_info_ops) {
1597 if (link->l_info_ops->io_put_attrs &&
1598 link->l_info_ops->io_put_attrs(msg, link) < 0)
1599 goto nla_put_failure;
1603 if (link->ce_mask & LINK_ATTR_LINKINFO_SLAVE_KIND) {
1604 NLA_PUT_STRING(msg, IFLA_INFO_SLAVE_KIND, link->l_info_slave_kind);
1610 if (link->ce_mask & LINK_ATTR_VF_LIST) {
1611 if (rtnl_link_sriov_fill_vflist(msg, link) < 0)
1612 goto nla_put_failure;
1615 if (do_foreach_af(link, af_fill_pi, msg) < 0)
1616 goto nla_put_failure;
1619 goto nla_put_failure;
1621 if (do_foreach_af(link, af_fill, msg) < 0)
1622 goto nla_put_failure;
1626 *result = _nl_steal_pointer(&msg);
1630 return -NLE_MSGSIZE;
1655 struct nl_msg **result)
1657 struct ifinfomsg ifi = {
1658 .ifi_family = link->l_family,
1659 .ifi_index = link->l_index,
1660 .ifi_flags = link->l_flags,
1661 .ifi_change = link->l_flag_mask,
1664 return build_link_msg(RTM_NEWLINK, &ifi, link, flags, result);
1718 struct nl_msg **result)
1720 struct ifinfomsg ifi = {
1721 .ifi_family = orig->l_family,
1722 .ifi_index = orig->l_index,
1726 if (changes->ce_mask & LINK_ATTR_FLAGS) {
1727 ifi.ifi_flags = orig->l_flags & ~changes->l_flag_mask;
1728 ifi.ifi_flags |= changes->l_flags;
1729 ifi.ifi_change = changes->l_flag_mask;
1732 if (changes->l_family && changes->l_family != orig->l_family) {
1733 APPBUG(
"link change: family is immutable");
1734 return -NLE_IMMUTABLE;
1738 if (orig->ce_mask & LINK_ATTR_IFINDEX &&
1739 orig->ce_mask & LINK_ATTR_IFNAME &&
1740 changes->ce_mask & LINK_ATTR_IFNAME &&
1741 !strcmp(orig->l_name, changes->l_name))
1742 changes->ce_mask &= ~LINK_ATTR_IFNAME;
1744 rt = af_request_type(orig->l_family, changes);
1746 if ((err = build_link_msg(rt, &ifi, changes, flags, result)) < 0)
1790 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
1803 err = wait_for_ack(sk);
1804 if ( err == -NLE_OPNOTSUPP
1805 && msg->nm_nlh->nlmsg_type == RTM_NEWLINK) {
1806 msg->nm_nlh->nlmsg_type = RTM_SETLINK;
1838 struct nl_msg **result)
1840 _nl_auto_nl_msg
struct nl_msg *msg = NULL;
1841 struct ifinfomsg ifi = {
1842 .ifi_index = link->l_index,
1845 if (!(link->ce_mask & (LINK_ATTR_IFINDEX | LINK_ATTR_IFNAME))) {
1846 APPBUG(
"ifindex or name must be specified");
1847 return -NLE_MISSING_ATTR;
1853 _NL_RETURN_ON_PUT_ERR(
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO));
1855 if (link->ce_mask & LINK_ATTR_IFNAME)
1856 _NL_RETURN_ON_PUT_ERR(
nla_put_string(msg, IFLA_IFNAME, link->l_name));
1858 *result = _nl_steal_pointer(&msg);
1940 _nl_strncpy_trunc(link->l_name, name,
sizeof(link->l_name));
1941 link->ce_mask |= LINK_ATTR_IFNAME;
1954 return link->ce_mask & LINK_ATTR_IFNAME ? link->l_name : NULL;
1964 link->l_group = group;
1965 link->ce_mask |= LINK_ATTR_GROUP;
1976 return link->l_group;
1979static inline void __assign_addr(
struct rtnl_link *link,
struct nl_addr **pos,
1980 struct nl_addr *
new,
int flag)
1988 link->ce_mask |= flag;
2004 __assign_addr(link, &link->l_addr, addr, LINK_ATTR_ADDR);
2018 return link->ce_mask & LINK_ATTR_ADDR ? link->l_addr : NULL;
2035 __assign_addr(link, &link->l_bcast, addr, LINK_ATTR_BRD);
2049 return link->ce_mask & LINK_ATTR_BRD ? link->l_bcast : NULL;
2062 link->l_flag_mask |= flags;
2063 link->l_flags |= flags;
2064 link->ce_mask |= LINK_ATTR_FLAGS;
2077 link->l_flag_mask |= flags;
2078 link->l_flags &= ~flags;
2079 link->ce_mask |= LINK_ATTR_FLAGS;
2093 return link->l_flags;
2103 link->l_family = family;
2104 link->ce_mask |= LINK_ATTR_FAMILY;
2106 if (link->l_af_ops) {
2107 int ao_family = link->l_af_ops->ao_family;
2109 af_free(link, link->l_af_ops, link->l_af_data[ao_family], NULL);
2110 link->l_af_data[ao_family] = NULL;
2113 link->l_af_ops = af_lookup_and_alloc(link, family);
2125 return link->ce_mask & LINK_ATTR_FAMILY ? link->l_family : AF_UNSPEC;
2139 link->l_arptype = arptype;
2140 link->ce_mask |= LINK_ATTR_ARPTYPE;
2153 if (link->ce_mask & LINK_ATTR_ARPTYPE)
2154 return link->l_arptype;
2169 link->l_index = ifindex;
2170 link->ce_mask |= LINK_ATTR_IFINDEX;
2184 return link->l_index;
2198 link->ce_mask |= LINK_ATTR_MTU;
2226 link->l_txqlen = txqlen;
2227 link->ce_mask |= LINK_ATTR_TXQLEN;
2242 return link->ce_mask & LINK_ATTR_TXQLEN ? link->l_txqlen : 0;
2245void rtnl_link_set_link(
struct rtnl_link *link,
int ifindex)
2247 link->l_link = ifindex;
2248 link->ce_mask |= LINK_ATTR_LINK;
2251int rtnl_link_get_link(
struct rtnl_link *link)
2253 return link->l_link;
2266 link->l_link_netnsid = link_netnsid;
2267 link->ce_mask |= LINK_ATTR_LINK_NETNSID;
2283 if (!(link->ce_mask & LINK_ATTR_LINK_NETNSID))
2286 *out_link_netnsid = link->l_link_netnsid;
2299 link->l_master = ifindex;
2300 link->ce_mask |= LINK_ATTR_MASTER;
2312 return link->l_master;
2324 link->l_carrier = status;
2325 link->ce_mask |= LINK_ATTR_CARRIER;
2337 return link->l_carrier;
2349 if (!(link->ce_mask & LINK_ATTR_CARRIER_CHANGES))
2352 if (carrier_changes)
2353 *carrier_changes = link->l_carrier_changes;
2368 link->l_operstate = status;
2369 link->ce_mask |= LINK_ATTR_OPERSTATE;
2382 return link->l_operstate;
2395 link->l_linkmode = mode;
2396 link->ce_mask |= LINK_ATTR_LINKMODE;
2409 return link->l_linkmode;
2422 return link->l_ifalias;
2439 free(link->l_ifalias);
2442 link->l_ifalias = strdup(alias);
2443 link->ce_mask |= LINK_ATTR_IFALIAS;
2445 link->l_ifalias = NULL;
2446 link->ce_mask &= ~LINK_ATTR_IFALIAS;
2465 _nl_strncpy_trunc(link->l_qdisc, name,
sizeof(link->l_qdisc));
2466 link->ce_mask |= LINK_ATTR_QDISC;
2479 return link->ce_mask & LINK_ATTR_QDISC ? link->l_qdisc : NULL;
2492 if (link->ce_mask & LINK_ATTR_NUM_VF) {
2493 *num_vf = link->l_num_vf;
2496 return -NLE_OPNOTSUPP;
2508 if (
id > RTNL_LINK_STATS_MAX)
2511 return link->l_stats[id];
2526 const uint64_t value)
2528 if (
id > RTNL_LINK_STATS_MAX)
2531 link->l_stats[id] = value;
2550 struct rtnl_link_info_ops *io;
2551 _nl_auto_free
char *kind = NULL;
2554 free(link->l_info_kind);
2555 link->ce_mask &= ~LINK_ATTR_LINKINFO;
2556 release_link_info(link);
2561 kind = strdup(type);
2567 if (io->io_alloc && (err = io->io_alloc(link)) < 0) {
2568 _nl_clear_free(&kind);
2572 link->l_info_ops = io;
2575 link->l_info_kind = _nl_steal_pointer(&kind);
2576 link->ce_mask |= LINK_ATTR_LINKINFO;
2590 return link->l_info_kind;
2608 kind = strdup(type);
2613 free(link->l_info_slave_kind);
2614 link->l_info_slave_kind = kind;
2617 link->ce_mask |= LINK_ATTR_LINKINFO_SLAVE_KIND;
2619 link->ce_mask &= ~LINK_ATTR_LINKINFO_SLAVE_KIND;
2632 return link->l_info_slave_kind;
2647 link->l_promiscuity = count;
2648 link->ce_mask |= LINK_ATTR_PROMISCUITY;
2660 return link->l_promiscuity;
2679 link->l_num_tx_queues = nqueues;
2680 link->ce_mask |= LINK_ATTR_NUM_TX_QUEUES;
2691 return link->l_num_tx_queues;
2710 link->l_num_rx_queues = nqueues;
2711 link->ce_mask |= LINK_ATTR_NUM_RX_QUEUES;
2722 return link->l_num_rx_queues;
2734 if (!(link->ce_mask & LINK_ATTR_GSO_MAX_SEGS))
2738 *gso_max_segs = link->l_gso_max_segs;
2752 if (!(link->ce_mask & LINK_ATTR_GSO_MAX_SIZE))
2756 *gso_max_size = link->l_gso_max_size;
2769 return link->l_phys_port_id;
2780 return link->l_phys_port_name;
2789struct nl_data *rtnl_link_get_phys_switch_id(
struct rtnl_link *link)
2791 return link->l_phys_switch_id;
2794void rtnl_link_set_ns_fd(
struct rtnl_link *link,
int fd)
2797 link->ce_mask |= LINK_ATTR_NS_FD;
2800int rtnl_link_get_ns_fd(
struct rtnl_link *link)
2802 return link->l_ns_fd;
2805void rtnl_link_set_ns_pid(
struct rtnl_link *link, pid_t pid)
2807 link->l_ns_pid = pid;
2808 link->ce_mask |= LINK_ATTR_NS_PID;
2811pid_t rtnl_link_get_ns_pid(
struct rtnl_link *link)
2813 return link->l_ns_pid;
2838 _nl_auto_rtnl_link
struct rtnl_link *link = NULL;
2863 return -NLE_OPNOTSUPP;
2943static const struct trans_tbl link_flags[] = {
2944 __ADD(IFF_LOOPBACK, loopback),
2945 __ADD(IFF_BROADCAST, broadcast),
2946 __ADD(IFF_POINTOPOINT, pointopoint),
2947 __ADD(IFF_MULTICAST, multicast),
2948 __ADD(IFF_NOARP, noarp),
2949 __ADD(IFF_ALLMULTI, allmulti),
2950 __ADD(IFF_PROMISC, promisc),
2951 __ADD(IFF_MASTER, master),
2952 __ADD(IFF_SLAVE, slave),
2953 __ADD(IFF_DEBUG, debug),
2954 __ADD(IFF_DYNAMIC, dynamic),
2955 __ADD(IFF_AUTOMEDIA, automedia),
2956 __ADD(IFF_PORTSEL, portsel),
2957 __ADD(IFF_NOTRAILERS, notrailers),
2959 __ADD(IFF_RUNNING, running),
2960 __ADD(IFF_LOWER_UP, lowerup),
2961 __ADD(IFF_DORMANT, dormant),
2962 __ADD(IFF_ECHO, echo),
2965char *rtnl_link_flags2str(
int flags,
char *buf,
size_t len)
2967 return __flags2str(flags, buf, len, link_flags,
2968 ARRAY_SIZE(link_flags));
2971int rtnl_link_str2flags(
const char *name)
2973 return __str2flags(name, link_flags, ARRAY_SIZE(link_flags));
2976static const struct trans_tbl link_stats[] = {
3044char *rtnl_link_stat2str(
int st,
char *buf,
size_t len)
3046 return __type2str(st, buf, len, link_stats, ARRAY_SIZE(link_stats));
3049int rtnl_link_str2stat(
const char *name)
3051 return __str2type(name, link_stats, ARRAY_SIZE(link_stats));
3054static const struct trans_tbl link_operstates[] = {
3055 __ADD(IF_OPER_UNKNOWN, unknown),
3056 __ADD(IF_OPER_NOTPRESENT, notpresent),
3057 __ADD(IF_OPER_DOWN, down),
3058 __ADD(IF_OPER_LOWERLAYERDOWN, lowerlayerdown),
3059 __ADD(IF_OPER_TESTING, testing),
3060 __ADD(IF_OPER_DORMANT, dormant),
3061 __ADD(IF_OPER_UP, up),
3064char *rtnl_link_operstate2str(uint8_t st,
char *buf,
size_t len)
3066 return __type2str(st, buf, len, link_operstates,
3067 ARRAY_SIZE(link_operstates));
3070int rtnl_link_str2operstate(
const char *name)
3072 return __str2type(name, link_operstates,
3073 ARRAY_SIZE(link_operstates));
3076static const struct trans_tbl link_modes[] = {
3077 __ADD(IF_LINK_MODE_DEFAULT,
default),
3078 __ADD(IF_LINK_MODE_DORMANT, dormant),
3081static const struct trans_tbl carrier_states[] = {
3086char *rtnl_link_mode2str(uint8_t st,
char *buf,
size_t len)
3088 return __type2str(st, buf, len, link_modes, ARRAY_SIZE(link_modes));
3091int rtnl_link_str2mode(
const char *name)
3093 return __str2type(name, link_modes, ARRAY_SIZE(link_modes));
3096char *rtnl_link_carrier2str(uint8_t st,
char *buf,
size_t len)
3098 return __type2str(st, buf, len, carrier_states,
3099 ARRAY_SIZE(carrier_states));
3102int rtnl_link_str2carrier(
const char *name)
3104 return __str2type(name, carrier_states, ARRAY_SIZE(carrier_states));
3107int rtnl_link_has_vf_list(
struct rtnl_link *link) {
3108 if (link->ce_mask & LINK_ATTR_VF_LIST)
3114void rtnl_link_set_vf_list(
struct rtnl_link *link)
3116 if (!rtnl_link_has_vf_list(link))
3117 link->ce_mask |= LINK_ATTR_VF_LIST;
3120void rtnl_link_unset_vf_list(
struct rtnl_link *link)
3122 if (rtnl_link_has_vf_list(link))
3123 link->ce_mask &= ~LINK_ATTR_VF_LIST;
3153 link->l_weight = weight;
3154 link->ce_mask |= LINK_ATTR_WEIGHT;
3162 return link->l_weight;
3167static struct nl_object_ops link_obj_ops = {
3168 .oo_name =
"route/link",
3170 .oo_free_data = link_free_data,
3171 .oo_clone = link_clone,
3177 .oo_compare = link_compare,
3178 .oo_keygen = link_keygen,
3179 .oo_attrs2str = link_attrs2str,
3180 .oo_id_attrs = LINK_ATTR_IFINDEX | LINK_ATTR_FAMILY,
3183static struct nl_af_group link_groups[] = {
3184 { AF_UNSPEC, RTNLGRP_LINK },
3185 { AF_BRIDGE, RTNLGRP_LINK },
3186 { AF_INET6, RTNLGRP_IPV6_IFINFO },
3187 { END_OF_GROUP_LIST },
3190static struct nl_cache_ops rtnl_link_ops = {
3191 .co_name =
"route/link",
3192 .co_hdrsize =
sizeof(
struct ifinfomsg),
3194 { RTM_NEWLINK, NL_ACT_NEW,
"new" },
3195 { RTM_DELLINK, NL_ACT_DEL,
"del" },
3196 { RTM_GETLINK, NL_ACT_GET,
"get" },
3197 { RTM_SETLINK, NL_ACT_CHANGE,
"set" },
3198 END_OF_MSGTYPES_LIST,
3200 .co_protocol = NETLINK_ROUTE,
3201 .co_groups = link_groups,
3202 .co_request_update = link_request_update,
3203 .co_msg_parser = link_msg_parser,
3204 .co_obj_ops = &link_obj_ops,
3207static void __init link_init(
void)
3212static void __exit link_exit(
void)
int nl_addr_iszero(const struct nl_addr *addr)
Returns true if the address consists of all zeros.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
int nl_addr_guess_family(const struct nl_addr *addr)
Guess address family of abstract address based on address size.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
#define NLA_PUT_S32(msg, attrtype, value)
Add 32 bit signed integer attribute to netlink message.
int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
Add string attribute to netlink message.
int nla_type(const struct nlattr *nla)
Return type of the attribute.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
int nla_len(const struct nlattr *nla)
Return length of the payload .
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
Add a unspecific attribute to netlink message.
@ NLA_STRING
NUL terminated character string.
@ NLA_UNSPEC
Unspecified type, binary data chunk.
@ NLA_NESTED
Nested attributes.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
Set cache flags.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
struct nl_data * nl_data_clone(const struct nl_data *src)
Clone an abstract data object.
void nl_data_free(struct nl_data *data)
Free an abstract data object.
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
void rtnl_link_af_ops_put(struct rtnl_link_af_ops *ops)
Give back reference to a set of operations.
void * rtnl_link_af_alloc(struct rtnl_link *link, const struct rtnl_link_af_ops *ops)
Allocate and return data buffer for link address family modules.
int rtnl_link_info_data_compare(struct rtnl_link *a, struct rtnl_link *b, int flags)
Compare link info data.
void rtnl_link_info_ops_get(struct rtnl_link_info_ops *ops)
Take reference to a set of operations.
struct rtnl_link_info_ops * rtnl_link_info_ops_lookup(const char *name)
Return operations of a specific link info type.
int rtnl_link_af_data_compare(struct rtnl_link *a, struct rtnl_link *b, int family)
Compare af data for a link address family.
void rtnl_link_info_ops_put(struct rtnl_link_info_ops *ops)
Give back reference to a set of operations.
struct rtnl_link_af_ops * rtnl_link_af_ops_lookup(const unsigned int family)
Return operations of a specific link address family.
int rtnl_link_release(struct nl_sock *sock, struct rtnl_link *slave)
Release slave link from its master.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
void rtnl_link_set_broadcast(struct rtnl_link *link, struct nl_addr *addr)
Set link layer broadcast address of link object.
void rtnl_link_set_carrier(struct rtnl_link *link, uint8_t status)
Set carrier of link object.
uint8_t rtnl_link_get_linkmode(struct rtnl_link *link)
Return link mode of link object.
uint64_t rtnl_link_get_stat(struct rtnl_link *link, rtnl_link_stat_id_t id)
Return value of link statistics counter.
int rtnl_link_get_master(struct rtnl_link *link)
Return master link of link object.
void rtnl_link_set_mtu(struct rtnl_link *link, unsigned int mtu)
Set Maximum Transmission Unit of link object.
int rtnl_link_set_link_netnsid(struct rtnl_link *link, int32_t link_netnsid)
Set the netnsid of the link.
uint32_t rtnl_link_get_num_rx_queues(struct rtnl_link *link)
Return number of RX queues.
unsigned int rtnl_link_get_weight(struct rtnl_link *link)
int rtnl_link_release_ifindex(struct nl_sock *sock, int slave)
Release slave link from its master.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_set_addr(struct rtnl_link *link, struct nl_addr *addr)
Set link layer address of link object.
void rtnl_link_set_arptype(struct rtnl_link *link, unsigned int arptype)
Set hardware type of link object.
void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
Unset flags of link object.
void rtnl_link_set_linkmode(struct rtnl_link *link, uint8_t mode)
Set link mode of link object.
uint32_t rtnl_link_get_num_tx_queues(struct rtnl_link *link)
Return number of TX queues.
uint32_t rtnl_link_get_group(struct rtnl_link *link)
Return the group identifier of link object.
struct nl_data * rtnl_link_get_phys_port_id(struct rtnl_link *link)
Return physical port id of link object.
const char * rtnl_link_get_slave_type(const struct rtnl_link *link)
Return type of enslaved link.
uint8_t rtnl_link_get_operstate(struct rtnl_link *link)
Return operational status of link object.
int rtnl_link_delete(struct nl_sock *sk, const struct rtnl_link *link)
Delete link.
struct nl_addr * rtnl_link_get_broadcast(struct rtnl_link *link)
Return link layer broadcast address of link object.
int rtnl_link_get_num_vf(struct rtnl_link *link, uint32_t *num_vf)
Return number of PCI virtual functions of link object.
int rtnl_link_set_slave_type(struct rtnl_link *link, const char *type)
Set type of slave link object.
char * rtnl_link_get_phys_port_name(struct rtnl_link *link)
Return physical port name of link object.
int rtnl_link_build_change_request(struct rtnl_link *orig, struct rtnl_link *changes, int flags, struct nl_msg **result)
Build a netlink message requesting the modification of link.
void rtnl_link_set_weight(struct rtnl_link *link, unsigned int weight)
void rtnl_link_set_ifalias(struct rtnl_link *link, const char *alias)
Set alias name of link object (SNMP IfAlias)
uint32_t rtnl_link_get_promiscuity(struct rtnl_link *link)
Return link promiscuity count.
void rtnl_link_set_num_tx_queues(struct rtnl_link *link, uint32_t nqueues)
Set number of TX queues.
int rtnl_link_build_add_request(struct rtnl_link *link, int flags, struct nl_msg **result)
Build a netlink message requesting the addition of new virtual link.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
char * rtnl_link_get_type(struct rtnl_link *link)
Return type of link.
char * rtnl_link_get_qdisc(struct rtnl_link *link)
Return name of queueing discipline of link object.
void rtnl_link_set_num_rx_queues(struct rtnl_link *link, uint32_t nqueues)
Set number of RX queues.
struct rtnl_link * rtnl_link_get_by_name(struct nl_cache *cache, const char *name)
Lookup link in cache by link name.
int rtnl_link_name2i(struct nl_cache *cache, const char *name)
Translate link name to corresponding interface index.
int rtnl_link_alloc_cache(struct nl_sock *sk, int family, struct nl_cache **result)
Allocate link cache and fill in all configured links.
unsigned int rtnl_link_get_arptype(struct rtnl_link *link)
Get hardware type of link object.
void rtnl_link_set_group(struct rtnl_link *link, uint32_t group)
Set the group identifier of a link object.
int rtnl_link_get_family(struct rtnl_link *link)
Return address family of link object.
int rtnl_link_get_kernel(struct nl_sock *sk, int ifindex, const char *name, struct rtnl_link **result)
Get a link object directly from kernel.
void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
Set flags of link object.
void rtnl_link_set_qdisc(struct rtnl_link *link, const char *name)
Set queueing discipline name of link object.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Lookup link in cache by interface index.
unsigned int rtnl_link_get_txqlen(struct rtnl_link *link)
Return transmission queue length.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
int rtnl_link_set_stat(struct rtnl_link *link, rtnl_link_stat_id_t id, const uint64_t value)
Set value of link statistics counter.
int rtnl_link_alloc_cache_flags(struct nl_sock *sk, int family, struct nl_cache **result, unsigned int flags)
Allocate link cache and fill in all configured links.
char * rtnl_link_get_info_type(struct rtnl_link *link)
int rtnl_link_enslave_ifindex(struct nl_sock *sock, int master, int slave)
Enslave slave link to master link.
int rtnl_link_build_delete_request(const struct rtnl_link *link, struct nl_msg **result)
Build a netlink message requesting the deletion of a link.
void rtnl_link_set_operstate(struct rtnl_link *link, uint8_t status)
Set operational status of link object.
int rtnl_link_enslave(struct nl_sock *sock, struct rtnl_link *master, struct rtnl_link *slave)
Enslave slave link to master link.
void rtnl_link_set_promiscuity(struct rtnl_link *link, uint32_t count)
Set link promiscuity count.
int rtnl_link_get_gso_max_segs(struct rtnl_link *link, uint32_t *gso_max_segs)
Return maximum number of segments for generic segmentation offload.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void rtnl_link_set_family(struct rtnl_link *link, int family)
Set address family of link object.
uint8_t rtnl_link_get_carrier(struct rtnl_link *link)
Return carrier status of link object.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
int rtnl_link_set_info_type(struct rtnl_link *link, const char *type)
const char * rtnl_link_get_ifalias(struct rtnl_link *link)
Return alias name of link object (SNMP IfAlias)
int rtnl_link_get_gso_max_size(struct rtnl_link *link, uint32_t *gso_max_size)
Return maximum size for generic segmentation offload.
void rtnl_link_set_master(struct rtnl_link *link, int ifindex)
Set master link of link object.
unsigned int rtnl_link_get_mtu(struct rtnl_link *link)
Return maximum transmission unit of link object.
void rtnl_link_set_txqlen(struct rtnl_link *link, unsigned int txqlen)
Set transmission queue length.
int rtnl_link_build_get_request(int ifindex, const char *name, struct nl_msg **result)
Construct RTM_GETLINK netlink message.
void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex)
Set interface index of link object.
struct nl_addr * rtnl_link_get_addr(struct rtnl_link *link)
Return link layer address of link object.
int rtnl_link_get_carrier_changes(struct rtnl_link *link, uint32_t *carrier_changes)
Return carrier on/off changes of link object.
int rtnl_link_get_link_netnsid(const struct rtnl_link *link, int32_t *out_link_netnsid)
Get the netnsid of the link.
unsigned int rtnl_link_get_flags(struct rtnl_link *link)
Return flags of link object.
int rtnl_link_get_ifindex(struct rtnl_link *link)
Return interface index of link object.
int rtnl_link_change(struct nl_sock *sk, struct rtnl_link *orig, struct rtnl_link *changes, int flags)
Change link.
@ RTNL_LINK_IP6_INBCASTOCTETS
@ RTNL_LINK_RX_COMPRESSED
@ RTNL_LINK_REASM_OVERLAPS
@ RTNL_LINK_IP6_INTRUNCATEDPKTS
@ RTNL_LINK_IP6_INNOROUTES
@ RTNL_LINK_IP6_OUTMCASTOCTETS
@ RTNL_LINK_IP6_INMCASTPKTS
@ RTNL_LINK_IP6_OUTBCASTOCTETS
@ RTNL_LINK_IP6_INDELIVERS
@ RTNL_LINK_IP6_REASMTIMEOUT
@ RTNL_LINK_IP6_OUTOCTETS
@ RTNL_LINK_ICMP6_INERRORS
@ RTNL_LINK_IP6_FRAGFAILS
@ RTNL_LINK_ICMP6_OUTMSGS
@ RTNL_LINK_IP6_CSUMERRORS
@ RTNL_LINK_IP6_INUNKNOWNPROTOS
@ RTNL_LINK_ICMP6_CSUMERRORS
@ RTNL_LINK_IP6_INBCASTPKTS
@ RTNL_LINK_TX_COMPRESSED
@ RTNL_LINK_IP6_INHDRERRORS
@ RTNL_LINK_IP6_REASMREQDS
@ RTNL_LINK_TX_CARRIER_ERR
@ RTNL_LINK_RX_MISSED_ERR
@ RTNL_LINK_IP6_OUTFORWDATAGRAMS
@ RTNL_LINK_IP6_INDISCARDS
@ RTNL_LINK_IP6_NOECTPKTS
@ RTNL_LINK_IP6_OUTNOROUTES
@ RTNL_LINK_IP6_OUTDISCARDS
@ RTNL_LINK_IP6_FRAGCREATES
@ RTNL_LINK_ICMP6_OUTERRORS
@ RTNL_LINK_IP6_INMCASTOCTETS
@ RTNL_LINK_IP6_INADDRERRORS
@ RTNL_LINK_IP6_INTOOBIGERRORS
@ RTNL_LINK_IP6_OUTMCASTPKTS
@ RTNL_LINK_IP6_OUTBCASTPKTS
@ RTNL_LINK_IP6_REASMFAILS
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
int nl_pickup_keep_syserr(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result, int *syserror)
Pickup netlink answer, parse is and return object with preserving system error.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.