Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
mxtools
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
15
Pull requests
4
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
6795406
applications-defaults
blink
checktrust
clusterd
crashkernel
cronwrap
desktop-files
etc
fon
forensics
hostconfig
kill-exuser
kvm_monitor
libexec_cron
libexec_startup
logrotate
make-automaps
mkmotd
mozilla-launcher
mxgrub
mxmirror
mxmount
mxnetctl
mxproxmox
mxqi
mxraid
mxrouter
mxshadow
mxsnoop
mxstartup
mxvlan
mxvlanctl
net_qdisc_nfs_low
netlog
nfsdtop
nvidiactl
pdist
pkgadmin
pmirror
prun
put_websafe
serial-log
slowio
unbound
usrlocalpkg
uvpn
vmcontrol
wakeonlan
.gitignore
Makefile
README.md
install.sh
Breadcrumbs
mxtools
/
mxvlan
/
mxvlanctl
Blame
Blame
Latest commit
History
History
executable file
·
175 lines (144 loc) · 4.41 KB
Breadcrumbs
mxtools
/
mxvlan
/
mxvlanctl
Top
File metadata and controls
Code
Blame
executable file
·
175 lines (144 loc) · 4.41 KB
Raw
#! /usr/local/system/perl/bin/perl use strict; use warnings; sub USAGE { "usage: $0 start|stop|restart\n" } my $hostname=`/bin/hostname`;chomp($hostname); $hostname=~s/\.molgen\.mpg\.de$//i; sub sys { my ($cmd)=@_; print "$cmd\n"; system $cmd and exit 1; } sub fix_eth { # fix 'ethX' to 'netXX' if ethX does not exist, but netXX does (old name used in config) my ($dev)=@_; if ($dev=~/^eth(\d+)/ and !-d "/sys/class/net/$dev") { my $netdev=sprintf('net%02d',$1); -d "/sys/class/net/$netdev" and return $netdev; } return $dev; } sub slurpfile { my ($path)=@_; open my $fh,'<',$path or die "$path: $!\n"; return join ('',<$fh>); } sub slurpfile_chomp { my $data=slurpfile($_[0]); chomp($data); return $data; } sub get_netif_flags { my ($dev)=@_;return hex(slurpfile_chomp("/sys/class/net/$dev/flags"));} # see include/uapi/linux/if.h for meaning sub netif_is_up {my ($dev)=@_;return get_netif_flags($dev)&1;} sub read_active_vlans { my $have_vlan={}; # { "eth5.150"=>1, ... } my $have_addr={}; # { "141.14.12.12/24" => "eth5.150", .. } my $file="/proc/net/vlan/config"; open IN,'<',$file or die "$file: $!\n"; while (<IN>) { /^(\S+)\s*\|\s*(\d+)\s*\|\s*(\S+)\s*$/ or next; # eth0.150 | 150 | eth0 my ($vlan_device,$vlan_num,$base_device)=($1,$2,$3); $have_vlan->{$vlan_device}=[$base_device,$vlan_num]; } open IN,'-|','ip','addr' or die "$!\n"; while (<IN>) { if (my ($addr,$dev)=/^\s*inet (\S+) scope global (\S+)/) { exists ($have_vlan->{$dev}) and $have_addr->{$addr}=$dev; } } close IN; return ($have_vlan,$have_addr); } sub read_mxvlans { my $want_vlan={}; # { "eth5.150"=>[eth5,150]", "vlan.github"=>[net02,54], ... } my $want_addr={}; # { "141.14.12.12/24" => "vlan.github", .. } my $file='/etc/mxvlans'; open IN,'<',$file or die "$file: $!\n"; while (<IN>) { s/#.*//; chomp; my @words=split; @words or next; eval { @words==4 or @words==5 or die "format error : $_\n"; my ($host,$base_device,$vlan_num,$vlan_device,$cidr)=@words; $base_device=fix_eth($base_device); # temp accept ethX for netXX if ($host eq $hostname) { $vlan_num=~/^\d+$/ or die "VLAN number not numeric\n"; $want_vlan->{$vlan_device}=[$base_device,$vlan_num]; if (defined $cidr) { $want_addr->{$cidr}=$vlan_device; } } }; if ($@) { die "$file line $. : $@"; } } return ($want_vlan,$want_addr); } sub configure_vlans { my ($want_vlan,$want_addr)=@_; for my $device (sort keys %$want_vlan) { my ($base_device,$vlan_num)=@{$want_vlan->{$device}}; sys("ip link set dev $base_device up") unless netif_is_up($base_device); sys("ip link add link $base_device name $device type vlan id $vlan_num"); } for my $ip (sort keys %$want_addr) { my $device=$want_addr->{$ip}; sys("ip addr add $ip dev $device"); } for my $device (sort keys %$want_vlan) { sys("ip link set dev $device up"); } } sub unconfigure_vlans { my ($have_vlan,$have_addr)=@_; for my $device (sort keys %$have_vlan) { sys("ip link set dev $device down"); } for my $ip (sort keys %$have_addr) { my $device=$have_addr->{$ip}; sys("ip addr delete $ip dev $device"); } for my $device (sort keys %$have_vlan) { my ($base_device,$vlan_num)=$device=~/^([^.]+)\.(.+)$/; sys("ip link delete $device"); } } @ARGV==1 or die USAGE; my ($cmd)=@ARGV; if ($cmd eq 'start' or $cmd eq 'restart') { -e "/proc/net/vlan/config" or sys("modprobe 8021q || true"); my ($want_vlan,$want_addr)=read_mxvlans(); my ($have_vlan,$have_addr)=read_active_vlans(); my ($new_vlan,$new_addr,$del_vlan,$del_addr)=({},{},{},{}); for (keys %$want_vlan) { $new_vlan->{$_}=$want_vlan->{$_} unless exists $have_vlan->{$_} && $have_vlan->{$_}[0] eq $want_vlan->{$_}[0] && $have_vlan->{$_}[1] eq $want_vlan->{$_}[1]; } for (keys %$want_addr) { $new_addr->{$_}=$want_addr->{$_} unless exists $have_addr->{$_} && $have_addr->{$_} eq $want_addr->{$_}; } for (keys %$have_vlan) { $del_vlan->{$_}=$have_vlan->{$_} unless exists $want_vlan->{$_} && $have_vlan->{$_}[0] eq $want_vlan->{$_}[0] && $have_vlan->{$_}[1] eq $want_vlan->{$_}[1]; } for (keys %$have_addr) { $del_addr->{$_}=$have_addr->{$_} unless exists $want_addr->{$_} && $want_addr->{$_} eq $have_addr->{$_}; } unconfigure_vlans($del_vlan,$del_addr); configure_vlans($new_vlan,$new_addr); } elsif ($cmd eq 'stop') { my ($have_vlan,$have_addr)=read_active_vlans(); unconfigure_vlans($have_vlan,$have_addr); } else { die USAGE; }
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
You can’t perform that action at this time.