-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtRoute.c
152 lines (123 loc) · 3.84 KB
/
virtRoute.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
146
147
148
149
150
151
/*
* Copyright (C) 2014 Joshua Hare, Lance Hartung, and Suman Banerjee.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/slab.h>
#include <linux/errno.h>
#include "virt.h"
#include "virtNetwork.h"
#include "virtRoute.h"
int virt_init_vroute_table(struct virt_priv *virt)
{
if(!virt)
return -ENODEV;
INIT_LIST_HEAD(&virt->vroute_table);
return 0;
}
void virt_free_vroute_table(struct virt_priv *virt)
{
struct vroute *vroute;
struct vroute *tmp;
if(!virt)
return;
list_for_each_entry_safe(vroute, tmp, &virt->vroute_table, vroute_list) {
list_del(&vroute->vroute_list);
kfree(vroute);
}
}
int virt_add_vroute(struct virt_priv *virt,
__be32 dest, __be32 netmask, __be32 node_ip)
{
struct remote_node *node;
struct vroute *vroute;
struct vroute *new_vroute;
struct list_head *insert;
uint32_t h_netmask;
if(!virt)
return -ENODEV;
if((dest & netmask) != dest)
return -EINVAL;
/* Check that the destination node exists. */
node = find_remote_node_by_ip(&virt->network, (struct in_addr *)&node_ip);
if(node)
remote_node_put(node);
else
return -ENOENT;
new_vroute = kzalloc(sizeof(struct vroute), GFP_KERNEL);
if(!new_vroute)
return -ENOMEM;
new_vroute->dest = dest;
new_vroute->netmask = netmask;
new_vroute->node_ip = node_ip;
h_netmask = ntohl(netmask);
if(list_empty(&virt->vroute_table)) {
list_add(&new_vroute->vroute_list, &virt->vroute_table);
return 0;
}
insert = &virt->vroute_table;
list_for_each_entry(vroute, &virt->vroute_table, vroute_list) {
/* Entries are stored in descending order, so the first time this
* occurs will be the insertion point. */
if(h_netmask >= ntohl(vroute->netmask)) {
insert = &vroute->vroute_list;
break;
}
}
/* Prevent duplicate entries. */
list_for_each_entry_from(vroute, &virt->vroute_table, vroute_list) {
if(netmask == vroute->netmask) {
if(dest == vroute->dest) {
kfree(new_vroute);
return -EEXIST;
}
} else {
break;
}
}
list_add_tail(&new_vroute->vroute_list, insert);
return 0;
}
int virt_delete_vroute(struct virt_priv *virt,
__be32 dest, __be32 netmask, __be32 node_ip)
{
struct vroute *vroute;
struct vroute *tmp;
if(!virt)
return -ENODEV;
if((dest & netmask) != dest)
return -EINVAL;
list_for_each_entry_safe(vroute, tmp, &virt->vroute_table, vroute_list) {
if(vroute->dest == dest &&
vroute->netmask == netmask &&
vroute->node_ip == node_ip) {
list_del(&vroute->vroute_list);
kfree(vroute);
return 0;
}
}
return -ENOENT;
}
__be32 virt_route(struct virt_priv *virt, __be32 dest)
{
struct vroute *vroute;
if(!virt)
return 0;
list_for_each_entry(vroute, &virt->vroute_table, vroute_list) {
if((vroute->netmask & dest) == vroute->dest)
return vroute->node_ip;
}
return 0;
}