Skip to content

Commit d30c45f

Browse files
committed
incus/server/device/nic_routed/go: Added host_tables
signed-off-by: avipatel805 <[email protected]>
1 parent e975684 commit d30c45f

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

internal/server/device/nic_routed.go

+45-23
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,16 @@ func (d *nicRouted) validateConfig(instConf instance.ConfigReader) error {
193193
// type: integer
194194
// shortdesc: The custom policy routing table ID to add IPv4 static routes to (in addition to the main routing table)
195195

196-
"ipv4.host_table",
196+
"ipv4.host_table", // deprecated
197197
// gendoc:generate(entity=devices, group=nic_routed, key=ipv6.host_table)
198198
//
199199
// ---
200200
// type: integer
201201
// shortdesc: The custom policy routing table ID to add IPv6 static routes to (in addition to the main routing table)
202202

203-
"ipv6.host_table",
203+
"ipv6.host_table", // deprecated
204+
"ipv4.host_tables",
205+
"ipv6.host_tables",
204206
// gendoc:generate(entity=devices, group=nic_routed, key=gvrp)
205207
//
206208
// ---
@@ -241,6 +243,10 @@ func (d *nicRouted) validateConfig(instConf instance.ConfigReader) error {
241243
// shortdesc: Comma-delimited list of IPv6 static addresses to add to the instance
242244
rules["ipv6.address"] = validate.Optional(validate.IsListOf(validate.IsNetworkAddressV6))
243245

246+
rules["ipv4.host_tables"] = validate.Optional(validate.IsListOf(validate.IsInRange(0, 255)))
247+
248+
rules["ipv6.host_tables"] = validate.Optional(validate.IsListOf(validate.IsInRange(0, 255)))
249+
244250
rules["gvrp"] = validate.Optional(validate.IsBool)
245251

246252
// gendoc:generate(entity=devices, group=nic_routed, key=ipv4.neighbor_probe)
@@ -584,36 +590,53 @@ func (d *nicRouted) Start() (*deviceConfig.RunConfig, error) {
584590
}
585591
}
586592

587-
table := "main"
588-
if d.config["vrf"] != "" {
589-
table = ""
593+
getTables := func() []string {
594+
// New plural form ­– honour exactly what the user gives.
595+
if v := d.config[fmt.Sprintf("%s.hosttables", keyPrefix)]; v != "" {
596+
tbls := util.SplitNTrimSpace(v, ",", -1, true)
597+
return tbls
598+
}
599+
600+
// Legacy – single key: include it plus 254.
601+
if v := d.config[fmt.Sprintf("%s.host_table", keyPrefix)]; v != "" {
602+
if v == "254" {
603+
return []string{"254"} // user asked for main only
604+
}
605+
606+
return []string{v, "254"} // custom + main
607+
}
608+
609+
// Default – main only.
610+
return []string{"254"}
590611
}
591612

613+
tables := getTables()
614+
592615
// Perform per-address host-side configuration (static routes and neighbour proxy entries).
593616
for _, addrStr := range addresses {
594617
// Apply host-side static routes to main routing table or VRF.
595-
r := ip.Route{
596-
DevName: saveData["host_name"],
597-
Route: fmt.Sprintf("%s/%d", addrStr, subnetSize),
598-
Table: table,
599-
Family: ipFamilyArg,
600-
VRF: d.config["vrf"],
601-
}
618+
// If a VRF is set we still add a route into the VRF's own table (empty Table value).
619+
if d.config["vrf"] != "" {
620+
r := ip.Route{
621+
DevName: saveData["host_name"],
622+
Route: fmt.Sprintf("%s/%d", addrStr, subnetSize),
623+
Table: "",
624+
Family: ipFamilyArg,
625+
VRF: d.config["vrf"],
626+
}
602627

603-
err = r.Add()
604-
if err != nil {
605-
return nil, fmt.Errorf("Failed adding host route %q: %w", r.Route, err)
628+
err = r.Add()
629+
if err != nil {
630+
return nil, fmt.Errorf("Failed adding host route %q: %w", r.Route, err)
631+
}
606632
}
607633

608-
// Add host-side static routes to instance IPs to custom routing table if specified.
609-
// This is in addition to the static route added to the main routing table, which is still
610-
// critical to ensure that reverse path filtering doesn't kick in blocking traffic from
611-
// the instance.
612-
if d.config[fmt.Sprintf("%s.host_table", keyPrefix)] != "" {
634+
// Add routes to all requested tables.
635+
for _, tbl := range tables {
613636
r := ip.Route{
614637
DevName: saveData["host_name"],
615638
Route: fmt.Sprintf("%s/%d", addrStr, subnetSize),
616-
Table: d.config[fmt.Sprintf("%s.host_table", keyPrefix)],
639+
Table: tbl,
617640
Family: ipFamilyArg,
618641
}
619642

@@ -651,7 +674,7 @@ func (d *nicRouted) Start() (*deviceConfig.RunConfig, error) {
651674
r := ip.Route{
652675
DevName: saveData["host_name"],
653676
Route: routeStr,
654-
Table: table,
677+
Table: "",
655678
Family: ipFamilyArg,
656679
Via: addresses[0],
657680
VRF: d.config["vrf"],
@@ -720,7 +743,6 @@ func (d *nicRouted) Start() (*deviceConfig.RunConfig, error) {
720743
}
721744

722745
reverter.Success()
723-
724746
return &runConf, nil
725747
}
726748

0 commit comments

Comments
 (0)