-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNetworkBridge.java
More file actions
187 lines (171 loc) · 4.82 KB
/
Copy pathNetworkBridge.java
File metadata and controls
187 lines (171 loc) · 4.82 KB
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
package javaforce.vm;
import java.io.*;
import java.util.*;
import javaforce.*;
/** Network Bridge - "virtual switch".
*
* NOTE : Open vSwitch is required for VLAN tagging guest networks.
*
* https://docs.openvswitch.org/en/latest/howto/libvirt/
*
* delete old bridge:
* brctl delbr virbr0
*
* setup Open vSwitch Bridge:
* ovs-vsctl add-br ovsbr
* ovs-vsctl add-port ovsbr eth0
*
* @author pquiring
*/
public class NetworkBridge extends NetworkConfig implements Serializable {
private static final long serialVersionUID = 1L;
protected NetworkBridge(String name, String type, String iface) {
super(name);
this.type = type;
this.iface = iface;
}
public String type; //br or os
public String iface; //physical nic
public void setInterface(String iface) {
this.iface = iface;
}
private static NetworkBridge[] list_br() {
ShellProcess p = new ShellProcess();
p.keepOutput(true);
String output = p.run(new String[] {"/usr/sbin/brctl", "show"}, true);
if (output == null) return null;
/*
bridge name\tbridge id\tSTP enabled\tinterfaces
virbr0\t8000.xxxxxxxxxxxx\tno\teth0...
*/
String[] lns = output.split("\n");
ArrayList<NetworkBridge> list = new ArrayList<>();
for(int a=1;a<lns.length;a++) {
if (lns[a].length() == 0) continue;
String[] fs = lns[a].split("\t+", -1);
if (fs.length < 4) continue;
String br = fs[0];
String nic = fs[3];
list.add(new NetworkBridge(br, "br", nic));
}
return list.toArray(new NetworkBridge[0]);
}
private static NetworkBridge[] list_os() {
ShellProcess p = new ShellProcess();
p.keepOutput(true);
String output = p.run(new String[] {"/usr/bin/ovs-vsctl", "show"}, true);
if (output == null) return null;
/*
UUID
Bridge virbr0
Port eth0
Interface eth0
Port virbr0
Interface virbr0
Type: internal
Port vnet1
tag: 123
Interface vnet1
ovs_version: "3.1.0"
*/
String[] lns = output.split("\n");
ArrayList<NetworkBridge> list = new ArrayList<>();
String br = null;
String nic = null;
boolean tag = false;
for(int a=1;a<lns.length;a++) {
String ln = lns[a].trim();
if (ln.length() == 0) continue;
if (ln.startsWith("Bridge ")) {
tag = false;
br = ln.substring(7);
list.add(new NetworkBridge(br, "os", ""));
continue;
}
if (ln.startsWith("Port ")) {
tag = false;
continue;
}
if (ln.startsWith("tag:")) {
tag = true;
}
if (ln.startsWith("Interface ")) {
if (br == null) continue;
if (tag) continue;
nic = ln.substring(10);
if (nic.equals(br)) continue;
if (nic.startsWith("vnet")) continue;
for(NetworkBridge nbr : list) {
if (nbr.name.equals(br)) {
nbr.setInterface(nic);
break;
}
}
br = null;
nic = null;
tag = false;
}
}
return list.toArray(new NetworkBridge[0]);
}
public static final int TYPE_OS = 1;
public static final int TYPE_BR = 2;
public static final int TYPE_ALL = 3;
public static NetworkBridge[] list() {
return list(TYPE_ALL);
}
public static NetworkBridge[] list(int flags) {
ArrayList<NetworkBridge> list_all = new ArrayList<>();
if ((flags & TYPE_BR) != 0) {
NetworkBridge[] list = list_br();
if (list != null) {
for(int a=0;a<list.length;a++) {
list_all.add(list[a]);
}
}
}
if ((flags & TYPE_OS) != 0) {
NetworkBridge[] list = list_os();
if (list != null) {
for(int a=0;a<list.length;a++) {
list_all.add(list[a]);
}
}
}
return list_all.toArray(new NetworkBridge[0]);
}
public static NetworkBridge create(String name, String iface) {
{
//create bridge
ShellProcess p = new ShellProcess();
p.keepOutput(true);
p.run(new String[] {"/usr/bin/ovs-vsctl", "--no-wait", "add-br", name}, true);
}
{
//add nic to bridge
ShellProcess p = new ShellProcess();
p.keepOutput(true);
p.run(new String[] {"/usr/bin/ovs-vsctl", "--no-wait", "add-port", name, iface}, true);
}
NetworkBridge nic = new NetworkBridge(name, "os", iface);
nic.link_up();
return nic;
}
private void remove_br() {
ShellProcess p = new ShellProcess();
p.keepOutput(true);
p.run(new String[] {"/usr/sbin/brctl", "delbr", name}, true);
}
private void remove_os() {
ShellProcess p = new ShellProcess();
p.keepOutput(true);
p.run(new String[] {"/usr/bin/ovs-vsctl", "--no-wait", "del-br", name}, true);
}
public boolean remove() {
switch (type) {
case "br": remove_br(); break;
case "os": remove_os(); break;
}
return true;
}
}