File: team_manual_control.c

package info (click to toggle)
libteam 1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,320 kB
  • ctags: 2,189
  • sloc: ansic: 16,808; sh: 1,190; python: 613; makefile: 110
file content (220 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *   team_manual_control.c - Network team device dummy manual control
 *   Copyright (C) 2011-2013 Jiri Pirko <jiri@resnulli.us>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <team.h>

#define APPNAME "team_manual_control"

static void usage(void)
{
	fprintf(stderr,
"Usage: " APPNAME " help\n"
"       " APPNAME " TEAMDEV dumplist { ports }\n"
"       " APPNAME " TEAMDEV get { mode | activeport }\n"
"       " APPNAME " TEAMDEV set { mode | activeport } value\n");
	exit(1);
}

static char *get_port_name(struct team_handle *th, uint32_t ifindex)
{
	static char ifname[32];

	return team_ifindex2ifname(th, ifindex, ifname, sizeof(ifname));
}

static int cmd_dumplist(struct team_handle *th, int argc, char **argv)
{
	char *opt;

	if (argc == 1) {
		fprintf(stderr, "List name not specified\n");
		usage();
	}

	opt = argv[1];

	if (strcmp(opt, "ports") == 0) {
		struct team_port *port;

		team_for_each_port(port, th) {
			uint32_t ifindex = team_get_port_ifindex(port);

			printf("ifname %s, linkup %d, changed %d, speed %d, "
			       "duplex %d\n",
			       get_port_name(th, ifindex),
			       team_is_port_link_up(port),
			       team_is_port_changed(port),
			       team_get_port_speed(port),
			       team_get_port_duplex(port));
		}
	} else {
		fprintf(stderr, "Unknown option name \"%s\"\n", opt);
		usage();
	}
	return 0;
}

static int cmd_get(struct team_handle *th, int argc, char **argv)
{
	int err;
	char *opt;

	if (argc == 1) {
		fprintf(stderr, "Option name not specified\n");
		usage();
	}

	opt = argv[1];
	if (strcmp(opt, "mode") == 0) {
		char *mode_name;

		err = team_get_mode_name(th, &mode_name);
		if (err) {
			fprintf(stderr, "Get mode failed, %d\n", err);
			return 1;
		}
		printf("%s\n", mode_name);
	} else if (strcmp(opt, "activeport") == 0) {
		uint32_t ifindex;

		err = team_get_active_port(th, &ifindex);
		if (err) {
			fprintf(stderr, "Get active port failed, %d\n", err);
			return 1;
		}
		printf("%s\n", ifindex ? get_port_name(th, ifindex) : "NONE");
	} else {
		fprintf(stderr, "Unknown option name \"%s\"\n", opt);
		usage();
	}
	return 0;
}

static int cmd_set(struct team_handle *th, int argc, char **argv)
{
	int err;
	char *opt;
	char *val;

	if (argc == 1) {
		fprintf(stderr, "Option name not specified\n");
		usage();
	}
	opt = argv[1];
	argc--;	argv++;
	val = argv[1];
	if (argc == 1) {
		fprintf(stderr, "Option value to be set not specified\n");
		usage();
	}

	if (strcmp(opt, "mode") == 0) {
		err = team_set_mode_name(th, val);
		if (err) {
			fprintf(stderr, "Set mode failed, %d\n", err);
			return 1;
		}
	} else if (strcmp(opt, "activeport") == 0) {
		uint32_t ifindex;

		ifindex = team_ifname2ifindex(th, val);
		if (!ifindex) {
			fprintf(stderr, "Netdevice %s not found.\n", val);
			usage();
		}

		err = team_set_active_port(th, ifindex);
		if (err) {
			fprintf(stderr, "Set active port failed, %d\n", err);
			return 1;
		}
	} else {
		fprintf(stderr, "Unknown option name \"%s\"\n", opt);
		usage();
	}
	return 0;
}

int main(int argc, char **argv)
{
	struct team_handle *th;
	int err;
	char *ifname = NULL;
	uint32_t ifindex;
	char *cmd = NULL;

	while (argc > 1) {
		char *opt = argv[1];

		argc--;	argv++;
		if (strcmp(opt, "help") == 0) {
			usage();
		} else if (!ifname) {
			ifname = opt;
		} else {
			cmd = opt;
			break;
		}
	}

	if (!ifname) {
		fprintf(stderr, "Team device name not specified.\n");
		usage();
	}

	if (!cmd) {
		fprintf(stderr, "Command not specified.\n");
		usage();
	}

	th = team_alloc();
	if (!th) {
		fprintf(stderr, "Team alloc failed.\n");
		return 1;
	}

	ifindex = team_ifname2ifindex(th, ifname);
	if (!ifindex) {
		fprintf(stderr, "Netdevice %s not found.\n", ifname);
		return 1;
	}

	err = team_init(th, ifindex);
	if (err) {
		fprintf(stderr, "Team init failed.\n");
		return 1;
	}

	if (strcmp(cmd, "dumplist") == 0) {
		err = cmd_dumplist(th, argc, argv);
	} else if (strcmp(cmd, "set") == 0) {
		err = cmd_set(th, argc, argv);
	} else if (strcmp(cmd, "get") == 0) {
		err = cmd_get(th, argc, argv);
	} else {
		fprintf(stderr, "Unknown command \"%s\".\n", cmd);
		usage();
	}

	team_free(th);
	return 0;
}