forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.c
100 lines (89 loc) · 2.35 KB
/
match.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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2014-2015 Red Hat, Inc. -- Jiri Benc <[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.
*/
#include "match.h"
#include <stdlib.h>
#include "if.h"
#include "master.h"
#include "netns.h"
/* Matches only on desc->ns */
static int match_if_ns(struct match_desc *desc, match_callback_f callback, void *arg)
{
struct if_entry *entry;
int res;
list_for_each(entry, desc->ns->ifaces) {
if (entry == desc->exclude)
continue;
res = callback(entry, arg);
if (res < 0)
return -res;
if (res > desc->best) {
desc->found = entry;
desc->best = res;
desc->count = 1;
if (desc->mode == MM_FIRST)
return 0;
} else if (res == desc->best)
desc->count++;
}
return 0;
}
int match_if(struct match_desc *desc, match_callback_f callback, void *arg)
{
struct netns_entry *ns;
int err;
if (desc->netns_list) {
list_for_each(ns, *desc->netns_list) {
desc->ns = ns;
if ((err = match_if_ns(desc, callback, arg)))
return err;
}
desc->ns = NULL;
return 0;
}
return match_if_ns(desc, callback, arg);
}
struct if_entry *match_if_netnsid(unsigned int ifindex, int netnsid,
struct netns_entry *current)
{
struct netns_id *ptr;
struct if_entry *entry;
list_for_each(ptr, current->ids) {
if (ptr->id == netnsid) {
list_for_each(entry, ptr->ns->ifaces) {
if (entry->if_index == ifindex)
return entry;
}
break;
}
}
return NULL;
}
void match_all_netnsid(struct list *netns_list)
{
struct netns_entry *ns;
struct if_entry *entry;
list_for_each(ns, *netns_list) {
list_for_each(entry, ns->ifaces) {
if (entry->link_netnsid >= 0)
link_set(match_if_netnsid(entry->link_index,
entry->link_netnsid,
ns), entry);
if (entry->peer_netnsid >= 0)
peer_set(entry, match_if_netnsid(entry->peer_index,
entry->peer_netnsid,
ns));
}
}
}