Skip to content

Commit b5cfad8

Browse files
authored
Merge pull request #1786 from stdweird/network_nmstate_command
ncm-network: support nmstate allowed route types
2 parents a7c44db + 577e46c commit b5cfad8

File tree

9 files changed

+46
-7
lines changed

9 files changed

+46
-7
lines changed

ncm-network/src/main/pan/components/network/types/network/backend/initscripts.pan

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function network_valid_route = {
1616
error("Use either prefix or netmask as route");
1717
};
1818

19+
if (exists(SELF['gateway']) && exists(SELF['type'])) {
20+
error("The route gateway will be ignored when type is defined");
21+
};
22+
1923
if (exists(SELF['prefix'])) {
2024
network_valid_prefix(SELF);
2125
};

ncm-network/src/main/pan/components/network/types/network/backend/nmstate.pan

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function network_valid_route = {
4747
if (exists(SELF['prefix']) && exists(SELF['netmask'])) error("Use either prefix or netmask as route");
4848
};
4949

50+
if (exists(SELF['gateway']) && exists(SELF['type'])) {
51+
error("The route gateway will be ignored when type is defined");
52+
};
53+
5054
if (exists(SELF['prefix'])) {
5155
network_valid_prefix(SELF);
5256
};

ncm-network/src/main/pan/components/network/types/network/route.pan

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ type network_route = {
4141
"onlink" ? boolean
4242
@{route add command options to use (cannot be combined with other options)}
4343
"command" ? string with !match(SELF, '[;]')
44+
@{route types} # only subset, i.e. the nmstate allowed route types
45+
"type" ? choice('blackhole', 'unreachable', 'prohibit')
4446
} with network_valid_route(SELF);

ncm-network/src/main/perl/network.pm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,12 +1236,22 @@ sub make_ifcfg_ip_route
12361236
# in absence of netmask, NetAddr::IP uses 32 or 128
12371237
$ip = NetAddr::IP->new($route->{address}, $route->{netmask});
12381238
}
1239+
12391240
# Generate it
1240-
$route->{command} = "$ip";
1241-
$route->{command} .= " via $route->{gateway}" if $route->{gateway};
1242-
$route->{command} .= " dev $device";
1243-
$route->{command} .= " onlink" if $route->{onlink};
1244-
$route->{command} .= " table $route->{table}" if $route->{table};
1241+
my @commands;
1242+
push(@commands, $route->{type}) if $route->{type};
1243+
push(@commands, $ip);
1244+
1245+
if (!$route->{type}) {
1246+
# This is part of the next-hop config
1247+
push(@commands, "via", $route->{gateway}) if $route->{gateway};
1248+
push(@commands, "dev", $device);
1249+
};
1250+
1251+
push(@commands, "onlink") if $route->{onlink};
1252+
push(@commands, "table", $route->{table}) if $route->{table};
1253+
1254+
$route->{command} = join(" ", @commands);
12451255
}
12461256
push(@text, $route->{command});
12471257
}

ncm-network/src/main/perl/nmstate.pm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,16 @@ sub make_nm_ip_route
190190
$rt{destination} = $route->{address}."/32";
191191
}
192192
}
193+
193194
$rt{'table-id'} = "$routing_table_hash->{$route->{table}}" if $route->{table};
194-
$rt{'next-hop-interface'} = $device;
195-
$rt{'next-hop-address'} = $route->{gateway} if $route->{gateway};
195+
196+
if ($route->{type}) {
197+
$rt{'route-type'} = $route->{type};
198+
} else {
199+
$rt{'next-hop-interface'} = $device;
200+
$rt{'next-hop-address'} = $route->{gateway} if $route->{gateway};
201+
}
202+
196203
$rt{'cwnd'} = int($route->{cwnd}) if $route->{cwnd};
197204
$rt{'initcwnd'} = int($route->{initcwnd}) if $route->{initcwnd};
198205
$rt{'initrwnd'} = int($route->{initrwnd}) if $route->{initrwnd};

ncm-network/src/test/perl/nmstate_route_rule.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ routes:
5454
- destination: 0.0.0.0/0
5555
next-hop-address: 4.3.2.254
5656
next-hop-interface: eth0
57+
- destination: 1.2.3.9/32
58+
route-type: blackhole
5759
EOF
5860

5961
is($cmp->Configure($cfg), 1, "Component runs correctly with a test profile");

ncm-network/src/test/perl/route_rule.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Readonly my $ETH0_ROUTE => <<EOF;
112112
something arbitrary
113113
default via 4.3.2.3 dev eth0 table outside
114114
1.2.3.9/32 via 4.3.2.9 dev eth0 onlink table outside
115+
blackhole 1.2.3.9/32
115116
EOF
116117

117118
Readonly my $ETH0_ROUTE6 => <<EOF;

ncm-network/src/test/resources/nmstate_route_rule.pan

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ variable QUATTOR_TYPES_NETWORK_BACKEND = 'nmstate';
55
include 'simple_base_profile';
66
include 'components/network/config-nmstate';
77

8+
89
# test for nmstate rule parameters on new interface
910
"/hardware/cards/nic/eth0/hwaddr" = "6e:a5:1b:55:77:0a";
11+
12+
prefix "/system/network/interfaces/eth0";
13+
"route/0" = dict(
14+
"address", "1.2.3.9",
15+
"type", "blackhole"
16+
);
17+
1018
prefix "/system/network/interfaces/eth0";
1119
"rule/0" = dict(
1220
"to", "1.2.3.4/24",

ncm-network/src/test/resources/route_rule.pan

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ prefix "/system/network/interfaces/eth0";
1717
"route/7" = dict("command", "something arbitrary with :");
1818
"route/8" = dict("address", "default", "gateway", "4.3.2.3", "table", "outside");
1919
"route/9" = dict("address", "1.2.3.9", "gateway", "4.3.2.9", "table", "outside", "onlink", true);
20+
"route/10" = dict("address", "1.2.3.9", "type", "blackhole");
2021

2122
"rule/0" = dict("command", "something");
2223
"rule/1" = dict("command", "something with ::");

0 commit comments

Comments
 (0)