File: teamd_events.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 (215 lines) | stat: -rw-r--r-- 5,544 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
/*
 *   teamd_events.c - Infrastructure for watching all sorts of events
 *   Copyright (C) 2012-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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <private/list.h>
#include <private/misc.h>
#include <team.h>

#include "teamd.h"

struct event_watch_item {
	struct list_item list;
	const struct teamd_event_watch_ops *ops;
	void *priv;
};

int teamd_event_port_added(struct teamd_context *ctx,
			   struct teamd_port *tdport)
{
	struct event_watch_item *watch;
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (!watch->ops->port_added)
			continue;
		err = watch->ops->port_added(ctx, tdport, watch->priv);
		if (err)
			return err;
	}
	return 0;
}

void teamd_event_port_removed(struct teamd_context *ctx,
			      struct teamd_port *tdport)
{
	struct event_watch_item *watch;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (!watch->ops->port_removed)
			continue;
		watch->ops->port_removed(ctx, tdport, watch->priv);
	}
}

int teamd_event_port_changed(struct teamd_context *ctx,
			     struct teamd_port *tdport)
{
	struct event_watch_item *watch;
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (!watch->ops->port_changed)
			continue;
		err = watch->ops->port_changed(ctx, tdport, watch->priv);
		if (err)
			return err;
	}
	return 0;
}

int teamd_event_port_link_changed(struct teamd_context *ctx,
				  struct teamd_port *tdport)
{
	struct event_watch_item *watch;
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (!watch->ops->port_link_changed)
			continue;
		err = watch->ops->port_link_changed(ctx, tdport, watch->priv);
		if (err)
			return err;
	}
	return 0;
}

int teamd_event_option_changed(struct teamd_context *ctx,
			       struct team_option *option)
{
	struct event_watch_item *watch;
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (!watch->ops->option_changed)
			continue;
		if (watch->ops->option_changed_match_name &&
		    strcmp(team_get_option_name(option),
			   watch->ops->option_changed_match_name))
			continue;
		err = watch->ops->option_changed(ctx, option, watch->priv);
		if (err)
			return err;
	}
	return 0;
}

int teamd_event_ifinfo_hwaddr_changed(struct teamd_context *ctx,
				      struct team_ifinfo *ifinfo)
{
	struct event_watch_item *watch;
	uint32_t ifindex = team_get_ifinfo_ifindex(ifinfo);
	struct teamd_port *tdport = teamd_get_port(ctx, ifindex);
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (watch->ops->hwaddr_changed && ctx->ifindex == ifindex) {
			err = watch->ops->hwaddr_changed(ctx, watch->priv);
			if (err)
				return err;
		} else if (watch->ops->port_hwaddr_changed && tdport) {
			err = watch->ops->port_hwaddr_changed(ctx, tdport,
							      watch->priv);
			if (err)
				return err;
		}
	}
	return 0;
}

int teamd_event_ifinfo_ifname_changed(struct teamd_context *ctx,
				      struct team_ifinfo *ifinfo)
{
	struct event_watch_item *watch;
	uint32_t ifindex = team_get_ifinfo_ifindex(ifinfo);
	struct teamd_port *tdport = teamd_get_port(ctx, ifindex);
	int err;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (watch->ops->ifname_changed && ctx->ifindex == ifindex) {
			err = watch->ops->ifname_changed(ctx, watch->priv);
			if (err)
				return err;
		} else if (watch->ops->port_ifname_changed && tdport) {
			err = watch->ops->port_ifname_changed(ctx, tdport,
							      watch->priv);
			if (err)
				return err;
		}
	}
	return 0;
}

int teamd_events_init(struct teamd_context *ctx)
{
	list_init(&ctx->event_watch_list);
	return 0;
}

void teamd_events_fini(struct teamd_context *ctx)
{
}

static struct event_watch_item *
__find_event_watch(struct teamd_context *ctx,
		   const struct teamd_event_watch_ops *ops,
		   void *priv)
{
	struct event_watch_item *watch;

	list_for_each_node_entry(watch, &ctx->event_watch_list, list) {
		if (watch->ops == ops && watch->priv == priv)
			return watch;
	}
	return NULL;
}

int teamd_event_watch_register(struct teamd_context *ctx,
			       const struct teamd_event_watch_ops *ops,
			       void *priv)
{
	struct event_watch_item *watch;

	if (__find_event_watch(ctx, ops, priv))
		return -EEXIST;
	watch = malloc(sizeof(*watch));
	if (!watch)
		return -ENOMEM;
	watch->ops = ops;
	watch->priv = priv;
	list_add_tail(&ctx->event_watch_list, &watch->list);
	return 0;
}

void teamd_event_watch_unregister(struct teamd_context *ctx,
				  const struct teamd_event_watch_ops *ops,
				  void *priv)
{
	struct event_watch_item *watch;

	watch = __find_event_watch(ctx, ops, priv);
	if (!watch)
		return;
	list_del(&watch->list);
	free(watch);
}