libnl: /data/src/libnl/src/nl-link-set.c Source File

libnl 3.12.0

nl-link-set.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include "nl-default.h"
7
8#include <linux/if.h>
9#include <linux/netlink.h>
10
11#include <netlink/cli/utils.h>
12#include <netlink/cli/link.h>
13
14static struct nl_sock *sock;
15static int quiet = 0;
16
17#if 0
18 " changes := [link LINK]\n"
19 " [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
20 " [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
21 " [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
22 " [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
23#endif
24
25static void print_usage(void)
26{
27 printf(
28 "Usage: nl-link-set [OPTION]... [LINK]\n"
29 "\n"
30 "Options\n"
31 " -q, --quiet Do not print informal notifications\n"
32 " -h, --help Show this help\n"
33 " -v, --version Show versioning information\n"
34 "\n"
35 "Selecting the Link\n"
36 " -n, --name=NAME link name\n"
37 " -i, --index interface index\n"
38 "Change Options\n"
39 " --rename=NAME rename interface\n"
40 " --mtu=NUM MTU value\n"
41 " --txqlen=NUM TX queue length\n"
42 " --weight=NUM weight\n"
43 " --ifalias=NAME alias name (SNMP IfAlias)\n"
44 " --state=up/down set interface up/down\n"
45 );
46 exit(0);
47}
48
49static void set_cb(struct nl_object *obj, void *arg)
50{
51 struct rtnl_link *link = nl_object_priv(obj);
52 struct rtnl_link *change = arg;
53 struct nl_dump_params params = {
54 .dp_type = NL_DUMP_LINE,
55 .dp_fd = stdout,
56 };
57 int err;
58
59 if ((err = rtnl_link_change(sock, link, change, 0)) < 0)
60 nl_cli_fatal(err, "Unable to change link: %s",
61 nl_geterror(err));
62
63 if (!quiet) {
64 printf("Changed ");
65 nl_object_dump(OBJ_CAST(link), &params);
66 }
67}
68
69int main(int argc, char *argv[])
70{
71 struct nl_cache *link_cache;
72 struct rtnl_link *link, *change;
73 int ok = 0;
74
75 sock = nl_cli_alloc_socket();
76 nl_cli_connect(sock, NETLINK_ROUTE);
77 link_cache = nl_cli_link_alloc_cache(sock);
78 link = nl_cli_link_alloc();
79 change = nl_cli_link_alloc();
80
81 for (;;) {
82 int c, optidx = 0;
83 enum {
84 ARG_RENAME = 257,
85 ARG_MTU = 258,
86 ARG_TXQLEN,
87 ARG_WEIGHT,
88 ARG_IFALIAS,
89 ARG_STATE,
90 };
91 static struct option long_opts[] = {
92 { "quiet", 0, 0, 'q' },
93 { "help", 0, 0, 'h' },
94 { "version", 0, 0, 'v' },
95 { "name", 1, 0, 'n' },
96 { "index", 1, 0, 'i' },
97 { "rename", 1, 0, ARG_RENAME },
98 { "mtu", 1, 0, ARG_MTU },
99 { "txqlen", 1, 0, ARG_TXQLEN },
100 { "weight", 1, 0, ARG_WEIGHT },
101 { "ifalias", 1, 0, ARG_IFALIAS },
102 { "state", 1, 0, ARG_STATE },
103 { 0, 0, 0, 0 }
104 };
105
106 c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
107 if (c == -1)
108 break;
109
110 switch (c) {
111 case 'q': quiet = 1; break;
112 case 'h': print_usage(); break;
113 case 'v': nl_cli_print_version(); break;
114 case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
115 case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
116 case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
117 case ARG_MTU: nl_cli_link_parse_mtu(change, optarg); break;
118 case ARG_TXQLEN: nl_cli_link_parse_txqlen(change, optarg); break;
119 case ARG_WEIGHT: nl_cli_link_parse_weight(change, optarg); break;
120 case ARG_IFALIAS: nl_cli_link_parse_ifalias(change, optarg); break;
121 case ARG_STATE:
122 if(!strcmp(optarg, "up"))
123 rtnl_link_set_flags(change, IFF_UP);
124 else if(!strcmp(optarg, "down"))
125 rtnl_link_unset_flags(change, IFF_UP);
126 break;
127 }
128 }
129
130 if (!ok)
131 print_usage();
132
133 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
134
135 return 0;
136}
nl_cache_foreach_filter
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition cache.c:1302
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
rtnl_link_unset_flags
void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
Unset flags of link object.
Definition link.c:2114
rtnl_link_set_flags
void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
Set flags of link object.
Definition link.c:2099
rtnl_link_change
int rtnl_link_change(struct nl_sock *sk, struct rtnl_link *orig, struct rtnl_link *changes, int flags)
Change link.
Definition link.c:1813
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition object.c:294
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
nl_dump_params
Dumping parameters.
Definition types.h:32
rtnl_link
Link object.
Definition nl-route.h:29