File: libteamdctl.c

package info (click to toggle)
libteam 1.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,344 kB
  • sloc: ansic: 17,494; sh: 1,242; python: 613; makefile: 102
file content (637 lines) | stat: -rw-r--r-- 15,705 bytes parent folder | download | duplicates (3)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 *   libteamdctl.c - Teamd daemon control library
 *   Copyright (C) 2013-2015 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
 */

/**
 * @defgroup libteamdctl Libteamdctl
 * Teamd daemon control library
 *
 * @{
 *
 * Header
 * ------
 * ~~~~{.c}
 * #include <teamdctl.h>
 * ~~~~
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <syslog.h>
#include <errno.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <private/misc.h>
#include <private/list.h>
#include <teamdctl.h>

#include "config.h"
#include "teamdctl_private.h"

/**
 * SECTION: logging
 */
void teamdctl_log(struct teamdctl *tdc, int priority,
		  const char *file, int line, const char *fn,
		  const char *format, ...)
{
	va_list args;

	va_start(args, format);
	tdc->log_fn(tdc, priority, file, line, fn, format, args);
	va_end(args);
}

static void log_stderr(struct teamdctl *tdc, int priority,
		       const char *file, int line, const char *fn,
		       const char *format, va_list args)
{
	fprintf(stderr, "libteamdctl: %s: ", fn);
	vfprintf(stderr, format, args);
	fprintf(stderr, "\n");
}

static int log_priority(const char *priority)
{
	char *endptr;
	int prio;

	prio = strtol(priority, &endptr, 10);
	if (endptr[0] == '\0' || isspace(endptr[0]))
		return prio;
	if (strncmp(priority, "err", 3) == 0)
		return LOG_ERR;
	if (strncmp(priority, "info", 4) == 0)
		return LOG_INFO;
	if (strncmp(priority, "debug", 5) == 0)
		return LOG_DEBUG;
	return 0;
}

/**
 * SECTION: reply cache
 */

static void reply_cache_clean(struct teamdctl *tdc)
{
	struct teamdctl_reply_cache_item *rcitem;
	struct teamdctl_reply_cache_item *tmp;

	list_for_each_node_entry_safe(rcitem, tmp,
				      &tdc->reply_cache_list, list) {
		list_del(&rcitem->list);
		free(rcitem->reply);
		free(rcitem);
	}
}

static void replace_str(char **p_str, char *new)
{
	if (*p_str)
		free(*p_str);
	*p_str = new;
}

static struct teamdctl_reply_cache_item *find_rcitem(struct teamdctl *tdc,
						     const char *id)
{
	struct teamdctl_reply_cache_item *rcitem;

	list_for_each_node_entry(rcitem, &tdc->reply_cache_list, list) {
		if (!strcmp(rcitem->id, id))
			return rcitem;
	}
	return NULL;
}

static char *reply_cache_query(struct teamdctl *tdc, const char *id)
{
	struct teamdctl_reply_cache_item *rcitem;

	rcitem = find_rcitem(tdc, id);
	if (rcitem)
		return rcitem->reply;
	return NULL;
}

static char *reply_cache_update(struct teamdctl *tdc, const char *id,
				char *reply)
{
	struct teamdctl_reply_cache_item *rcitem;

	rcitem = find_rcitem(tdc, id);
	if (rcitem)
		goto skip_create;

	rcitem = myzalloc(sizeof(*rcitem) + strlen(id) + 1);
	if (!rcitem) {
		free(reply);
		return NULL;
	}
	strcpy(rcitem->id, id);
	list_add_tail(&tdc->reply_cache_list, &rcitem->list);

skip_create:
	replace_str(&rcitem->reply, reply);
	return reply;
}

/**
 * @details Allocates library context and does initial setup.
 *
 * @return New libteam library context.
 **/
TEAMDCTL_EXPORT
struct teamdctl *teamdctl_alloc(void)
{
	struct teamdctl *tdc;
	const char *env;

	tdc = myzalloc(sizeof(*tdc));
	if (!tdc)
		return NULL;

	list_init(&tdc->reply_cache_list);
	tdc->log_fn = log_stderr;
	tdc->log_priority = LOG_ERR;
	/* environment overwrites config */
	env = getenv("TEAMDCTL_LOG");
	if (env != NULL)
		teamdctl_set_log_priority(tdc, log_priority(env));

	dbg(tdc, "teamdctl %p created.", tdc);
	dbg(tdc, "log_priority=%d", tdc->log_priority);
	return tdc;
}

/**
 * @param tdc		libteam library context
 *
 * @details Do library context cleanup.
 **/
TEAMDCTL_EXPORT
void teamdctl_free(struct teamdctl *tdc)
{
	reply_cache_clean(tdc);
	free(tdc);
}

/**
 * @param tdc		libteamdctl library context
 * @param log_fn	function to be called for logging messages
 *
 * @details The built-in logging writes to stderr. It can be overridden
 *	    by a custom function, to plug log messages into the user's
 *	    logging functionality.
 **/
TEAMDCTL_EXPORT
void teamdctl_set_log_fn(struct teamdctl *tdc,
			 void (*log_fn)(struct teamdctl *tdc, int priority,
					const char *file, int line,
					const char *fn, const char *format,
					va_list args))
{
	tdc->log_fn = log_fn;
	dbg(tdc, "Custom logging function %p registered.", log_fn);
}

/**
 * @param tdc		libteamdctl library context
 *
 * @return The current logging priority.
 **/
TEAMDCTL_EXPORT
int teamdctl_get_log_priority(struct teamdctl *tdc)
{
	return tdc->log_priority;
}

/**
 * @param tdc		libteamdctl library context
 * @param priority	the new logging priority
 *
 * @details Set the current logging priority. The value controls which messages
 *	    are logged.
 **/
TEAMDCTL_EXPORT
void teamdctl_set_log_priority(struct teamdctl *tdc, int priority)
{
	tdc->log_priority = priority;
}

static int cli_method_call(struct teamdctl *tdc, const char *method_name,
			   char **p_reply, const char *fmt, ...)
{
	va_list ap;
	int err;

	va_start(ap, fmt);
	err = tdc->cli->method_call(tdc, method_name, p_reply,
				    tdc->cli_priv, fmt, ap);
	va_end(ap);
	return err;
}

static int cli_init(struct teamdctl *tdc, const char *team_name)
{
	int err;

	if (tdc->cli->priv_size) {
		tdc->cli_priv = myzalloc(tdc->cli->priv_size);
		if (!tdc->cli_priv)
			return -ENOMEM;
	}
	err = tdc->cli->init(tdc, team_name, tdc->cli_priv);
	if (err)
		goto free_priv;
	if (tdc->cli->test_method_call_required) {
		err = cli_method_call(tdc, "ConfigDump", NULL, "");
		if (err)
			goto free_priv;
	}
	return 0;
free_priv:
	if (tdc->cli->priv_size)
		free(tdc->cli_priv);
	return err;
}

static void cli_fini(struct teamdctl *tdc)
{
	tdc->cli->fini(tdc, tdc->cli_priv);
	free(tdc->cli_priv);
}

/**
 * @param tdc		libteamdctl library context
 * @param team_name	team device name
 * @param addr		address (for zeromq only)
 * @param cli_type	client type
 *
 * @details Connect to teamd instance controlling team driver instance
 *	    with interface name team_name. Use client type cli_type to connect.
 *	    That can be either "dbus" for connection over D-Bus, "usock" which
 *	    will use unix domain socket or NULL to select the type
 *	    automatically.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_connect(struct teamdctl *tdc, const char *team_name,
		     const char *addr, const char *cli_type)
{
	int err;
	int i;
	const struct teamdctl_cli *teamdctl_cli_list[] = {
		teamdctl_cli_usock_get(),
#ifdef ENABLE_DBUS
		teamdctl_cli_dbus_get(),
#endif
#ifdef ENABLE_ZMQ
		teamdctl_cli_zmq_get(),
#endif
	};
#define TEAMDCTL_CLI_LIST_SIZE ARRAY_SIZE(teamdctl_cli_list)

	if (tdc->cli)
		return -EBUSY;

	for (i = 0; i < TEAMDCTL_CLI_LIST_SIZE; i++) {
		const struct teamdctl_cli *cli = teamdctl_cli_list[i];
		int orig_log_prio = teamdctl_get_log_priority(tdc);

		if (cli_type && strcmp(cli_type, cli->name))
			continue;
		tdc->cli = cli;

		/* In case cli_type is not specified, we will try to connect
		 * using all avaivable clis. Once some of the cli connects,
		 * it will be selected. In that case, silence error messages
		 * that can appear during cli init by setting log priority to
		 * LOG_EMERG.
		 */
		if (!cli_type && orig_log_prio < LOG_DEBUG)
			teamdctl_set_log_priority(tdc, LOG_EMERG);

		tdc->addr = (char *) addr;

		err = cli_init(tdc, team_name);

		/* restore original log priority */
		teamdctl_set_log_priority(tdc, orig_log_prio);

		if (!err)
			break; /* usable cli found */

		if (cli_type) {
			err(tdc, "Failed to connect using CLI \"%s\".",
			    cli->name);
			goto err_out;
		}
	}
	if (i == TEAMDCTL_CLI_LIST_SIZE) {
		if (!cli_type)
			err(tdc, "Failed to connect using all CLIs.");
		else
			err(tdc, "Failed to connect using unknown CLI \"%s\".",
				 cli_type);
		err = -EINVAL;
		goto err_out;
	}

	dbg(tdc, "Connected using CLI \"%s\".", tdc->cli->name);

	err = teamdctl_refresh(tdc);
	if (err)
		goto err_out;
	return 0;
err_out:
	tdc->cli = NULL;
	return err;
}

/**
 * @param tdc		libteamdctl library context
 *
 * @details Disconnect from teamd instance.
 **/
TEAMDCTL_EXPORT
void teamdctl_disconnect(struct teamdctl *tdc)
{
	cli_fini(tdc);
	tdc->cli = NULL;
}


static int cache_config(struct teamdctl *tdc, const char *id, char **p_reply)
{
	int err;
	char *reply;

	err = cli_method_call(tdc, id, &reply, "");
	if (err)
		return err;
	reply = reply_cache_update(tdc, id, reply);
	if (!reply)
		return -ENOMEM;
	if (p_reply)
		*p_reply = reply;
	return 0;
}

/**
 * @param tdc		libteamdctl library context
 *
 * @details Refresh cache.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_refresh(struct teamdctl *tdc)
{
	int err;

	err = cache_config(tdc, "ConfigDump", NULL);
	if (err)
		return err;
	err = cache_config(tdc, "ConfigDumpActual", NULL);
	if (err)
		return err;
	err = cache_config(tdc, "StateDump", NULL);
	if (err)
		return err;
	return 0;
}

/**
 * @param tdc		libteamdctl library context
 * @param port_devname	port device name
 *
 * @details Adds specified port to team.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_port_add(struct teamdctl *tdc, const char *port_devname)
{
	return cli_method_call(tdc, "PortAdd", NULL, "s", port_devname);
}

/**
 * @param tdc		libteamdctl library context
 * @param port_devname	port device name
 *
 * @details Removes specified port from team.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_port_remove(struct teamdctl *tdc, const char *port_devname)
{
	return cli_method_call(tdc, "PortRemove", NULL, "s", port_devname);
}

/**
 * @param tdc			libteamdctl library context
 * @param port_devname		port device name
 * @param port_config_raw	port config
 *
 * @details Update config for specified port.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_port_config_update_raw(struct teamdctl *tdc,
				    const char *port_devname,
				    const char *port_config_raw)
{
	return cli_method_call(tdc, "PortConfigUpdate", NULL,
			       "ss", port_devname, port_config_raw);
}

/**
 * @param tdc		libteamdctl library context
 * @param port_devname	port device name
 * @param p_cfg		pointer to string which will be set
 *
 * @details Gets raw port config string.
 *	    Does direct method call avoiding possible stale data in the cache.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_port_config_get_raw_direct(struct teamdctl *tdc,
					const char *port_devname,
					char **p_cfg)
{
	int err;
	char *reply;
#define PC_ID_PREFIX "_portcnf_"
	char id[sizeof(PC_ID_PREFIX) + IFNAMSIZ + 1];

	if (strlen(port_devname) > IFNAMSIZ)
		return -EINVAL;
	err = cli_method_call(tdc, "PortConfigDump", &reply, "s", port_devname);
	if (err)
		return err;
	sprintf(id, "%s%s", PC_ID_PREFIX, port_devname);
	reply = reply_cache_update(tdc, id, reply);
	if (!reply)
		return -ENOMEM;
	if (p_cfg)
		*p_cfg = reply;
	return 0;
}

/**
 * @param tdc		libteamdctl library context
 *
 * @details Gets raw config string.
 *	    Using reply cache. Return value is never NULL.
 *	    To refresh the cache, use teamdctl_refresh function.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * Return Pointer to cached config string.
 **/
TEAMDCTL_EXPORT
char *teamdctl_config_get_raw(struct teamdctl *tdc)
{
	return reply_cache_query(tdc, "ConfigDump");
}

/**
 * @param tdc		libteamdctl library context
 * @param p_cfg		pointer to string which will be set
 *
 * @details Gets raw config string.
 *	    Does direct method call avoiding possible stale data in the cache.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_config_get_raw_direct(struct teamdctl *tdc, char **p_cfg)
{
	return cache_config(tdc, "ConfigDump", p_cfg);
}

/**
 * @param tdc		libteamdctl library context
 *
 * @details Gets raw actual config string.
 *	    Using reply cache. Return value is never NULL.
 *	    To refresh the cache, use teamdctl_refresh function.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Pointer to cached actual config string.
 **/
TEAMDCTL_EXPORT
char *teamdctl_config_actual_get_raw(struct teamdctl *tdc)
{
	return reply_cache_query(tdc, "ConfigDumpActual");
}

/**
 * @param tdc		libteamdctl library context
 * @param p_cfg		pointer to string which will be set
 *
 * @details Gets raw actual config string.
 *	    Does direct method call avoiding possible stale data in the cache.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_config_actual_get_raw_direct(struct teamdctl *tdc, char **p_cfg)
{
	return cache_config(tdc, "ConfigDumpActual", p_cfg);
}

/**
 * @param tdc		libteamdctl library context
 *
 * @details Gets raw state string.
 *	    Using reply cache. Return value is never NULL.
 *	    To refresh the cache, use teamdctl_refresh function.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Pointer to cached state string.
 **/
TEAMDCTL_EXPORT
char *teamdctl_state_get_raw(struct teamdctl *tdc)
{
	return reply_cache_query(tdc, "StateDump");
}

/**
 * @param tdc		libteamdctl library context
 * @param p_cfg		pointer to string which will be set
 *
 * @details Gets raw state string.
 *	    Does direct method call avoiding possible stale data in the cache.
 *	    Note: the obtained string should not be modified or freed by caller.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_state_get_raw_direct(struct teamdctl *tdc, char **p_cfg)
{
	return cache_config(tdc, "StateDump", p_cfg);
}

/**
 * @param tdc		libteamdctl library context
 * @param item_path	path to item
 * @param p_value	pointer where reply string will be stored
 *
 * @details Get state item value. Note that caller is responsible to
 *	    free *p_value.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_state_item_value_get(struct teamdctl *tdc, const char *item_path,
				  char **p_value)
{
	return cli_method_call(tdc, "StateItemValueGet", p_value,
			       "s", item_path);
}
/**
 * @param tdc		libteamdctl library context
 * @param item_path	path to item
 * @param value		new value to be set
 *
 * @details Set state item value.
 *
 * @return Zero on success or negative number in case of an error.
 **/
TEAMDCTL_EXPORT
int teamdctl_state_item_value_set(struct teamdctl *tdc, const char *item_path,
				  const char *value)
{
	return cli_method_call(tdc, "StateItemValueSet", NULL,
			       "ss", item_path, value);
}

/**
 * @}
 */