Debian Codegraph @

perllib/qosient/config/nm.pm

raw
1 package qosient::config::nm;
2
3 use strict;
4 use warnings;
5 use Exporter;
6 use Scalar::Util qw(looks_like_number);
7 use vars qw($VERSION @ISA @EXPORT);
8
9 $VERSION = "1.00";
10 @ISA = qw(Exporter);
11 @EXPORT = qw(nm_setup_monitor_dev
12 nm_disable_dev
13 nm_setup_mgmt_dev
14 nm_setup_directattach_dev);
15
16 my @modify = qw/nmcli con modify --temporary/;
17 my @conup = qw/nmcli --wait 0 con up/;
18 my @condown = qw/nmcli con down/;
19 my @disablev4 = ('ipv4.addresses', '', 'ipv4.gateway', '', 'ipv4.dns', '', 'ipv4.method', 'disabled');
20 my @disablev6 = ('ipv6.addresses', '', 'ipv6.gateway', '', 'ipv4.dns', '', 'ipv6.method', 'ignore');
21 my @llv6 = ('ipv6.addresses', '', 'ipv6.gateway', '', 'ipv6.method', 'link-local');
22
23 # Prepare an interface for use as a monitoring device. Disable both
24 # address families, set mtu to 9000 and bring the interface up in this
25 # new configuration. The interface name is really the networkmanager
26 # connection name. Will need to add some magic for the cases where that
27 # is not the same as the interface name.
28 sub nm_setup_monitor_dev {
29 my ($itf) = @_;
30
31 system(@modify, ${itf}, @disablev4);
32 system(@modify, ${itf}, @disablev6);
33 system(@modify, ${itf}, '802-3-ethernet.mtu', '9000');
34 system(@modify, ${itf}, 'connection.autoconnect', 'yes');
35 system(@conup, ${itf});
36 return 1;
37 }
38
39 sub nm_disable_dev {
40 my ($itf) = @_;
41
42 system(@modify, ${itf}, 'connection.autoconnect', 'no');
43 system(@modify, ${itf}, @disablev4);
44 system(@modify, ${itf}, @disablev6);
45 system(@condown, ${itf});
46 return 1;
47 }
48
49 # $ipv4_addrs and $ipv6_addrs are array references
50 sub nm_setup_mgmt_dev {
51 my ($itf, $ipv4_addrs, $ipv6_addrs, $ipv4_next_hop, $ipv6_next_hop,
52 $ipv4_disable, $ipv6_disable, $ipv4_dns, $ipv6_dns) = @_;
53
54 my @ipv4_gateway = ();
55 if (scalar @$ipv4_next_hop == 1) {
56 @ipv4_gateway = ('ipv4.gateway', @$ipv4_next_hop);
57 }
58
59 my @ipv6_gateway = ();
60 if (scalar @$ipv6_next_hop == 1) {
61 @ipv6_gateway = ('ipv6.gateway', @$ipv6_next_hop);
62 }
63
64 if ( !$ipv4_disable ) {
65 if (scalar(@$ipv4_addrs) > 0) {
66 my @args = ('ipv4.method', 'manual',
67 'ipv4.addresses', join(', ', @$ipv4_addrs),
68 @ipv4_gateway);
69 if ( defined $ipv4_dns && scalar($ipv4_dns) > 0 ) {
70 push @args, 'ipv4.dns', join(',', @$ipv4_dns);
71 }
72 system(@modify, ${itf}, @args);
73 } else {
74 system(@modify, ${itf}, 'ipv4.method', 'auto', 'ipv4.addresses', '');
75 }
76 }
77
78 if ( !$ipv6_disable ) {
79 if (scalar(@$ipv6_addrs > 0)) {
80 my @args = ('ipv6.method', 'manual',
81 'ipv6.addresses', join(', ', @$ipv6_addrs),
82 @ipv6_gateway);
83 if ( defined $ipv6_dns && scalar(@$ipv6_dns) > 0 ) {
84 push @args, 'ipv6.dns', join(', ', @$ipv6_dns);
85 }
86 system(@modify, ${itf}, @args);
87 } else {
88 system(@modify, ${itf}, 'ipv6.method', 'auto', 'ipv6.addresses', '');
89 }
90 }
91
92 system(@conup, ${itf});
93 return 1;
94 }
95
96 sub nm_setup_directattach_dev {
97 my ($itf) = @_;
98
99 system(@modify, ${itf}, @disablev4);
100 system(@modify, ${itf}, @llv6);
101 system(@modify, ${itf}, '802-3-ethernet.mtu', '1500');
102 system(@modify, ${itf}, 'connection.autoconnect', 'yes');
103 system(@conup, ${itf});
104 return 1;
105 }
106
107 1;