forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.c
145 lines (125 loc) · 3.42 KB
/
master.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2016 Red Hat, Inc. -- Jiri Benc <[email protected]>,
* Ondrej Hlavaty <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*/
/* Resolves master_index to the actual interface. */
#include "if.h"
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include "master.h"
#include "match.h"
#include "netns.h"
int master_set(struct if_entry *master, struct if_entry *master_of)
{
if (master_of->master != NULL)
node_remove(&master_of->rev_master_node);
master_of->master = master;
if (master_of->master != NULL)
list_append(&master_of->master->rev_master, &master_of->rev_master_node);
return 0;
}
int link_set(struct if_entry *link, struct if_entry *entry)
{
if (!entry)
return 0;
if (entry->link != NULL)
node_remove(&entry->rev_link_node);
entry->link = link;
if (entry->link != NULL)
list_append(&entry->link->rev_link, &entry->rev_link_node);
return 0;
}
int peer_set(struct if_entry *first, struct if_entry *second)
{
if (first) {
if (first->peer)
first->peer->peer = NULL;
first->peer = second;
}
if (second) {
if (second->peer)
second->peer->peer = NULL;
second->peer = first;
}
return 0;
}
static int match_master(struct if_entry *entry, void *arg)
{
struct if_entry *slave = arg;
if (entry->if_index != slave->master_index)
return 0;
if (entry->ns == slave->ns)
return 2;
return 1;
}
static int match_link(struct if_entry *entry, void *arg)
{
struct if_entry *slave = arg;
if (entry->if_index != slave->link_index)
return 0;
if (entry->ns == slave->ns)
return 2;
return 1;
}
static int err_msg(struct match_desc *match, const char *type, struct if_entry *entry)
{
if (match_ambiguous(*match))
return if_add_warning(entry, "has a %s but failed to find it reliably", type);
if (!match_found(*match))
return if_add_warning(entry, "has a %s but failed to find it", type);
return 0;
}
static int process(struct if_entry *entry, struct list *netns_list)
{
int err;
struct match_desc match;
if (!entry->master && entry->master_index) {
match_init(&match);
match.netns_list = netns_list;
match.exclude = entry;
if ((err = match_if(&match, match_master, entry)))
return err;
if ((err = err_msg(&match, "master", entry)))
return err;
if ((err = master_set(match_found(match), entry)))
return err;
}
if (!entry->link && entry->link_index) {
match_init(&match);
match.netns_list = netns_list;
match.exclude = entry;
if ((err = match_if(&match, match_link, entry)))
return err;
if ((err = err_msg(&match, "link", entry)))
return err;
if ((err = link_set(match_found(match), entry)))
return err;
}
return 0;
}
int master_resolve(struct list *netns_list)
{
struct netns_entry *ns;
struct if_entry *entry;
int err;
list_for_each(ns, *netns_list) {
list_for_each(entry, ns->ifaces) {
err = process(entry, netns_list);
if (err)
return err;
}
}
return 0;
}