Skip to content

Commit 557ddcd

Browse files
committed
loader: attach datapath to IPIP tunnel devices
This change adds the ability to attach the loader to IPIP tunnel devices. cil_from_netdev and cil_to_netdev datapath programs are loaded like for native devices.
1 parent d2967da commit 557ddcd

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

pkg/datapath/loader/loader.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,55 @@ func (l *loader) bpfMasqAddrs(ifName string) (masq4, masq6 netip.Addr) {
177177
return
178178
}
179179

180+
// patchIPIPdevDatapath calculates the changes necessary
181+
// to attach the IPIP endpoint datapath to different interfaces.
182+
func (l *loader) patchIPIPdevDatapath(ep datapath.Endpoint, ifName string) (map[string]uint64, map[string]string, error) {
183+
opts := ELFVariableSubstitutions(ep)
184+
strings := ELFMapSubstitutions(ep)
185+
186+
iface, err := safenetlink.LinkByName(ifName)
187+
if err != nil {
188+
return nil, nil, err
189+
}
190+
191+
// The THIS_INTERFACE_MAC value is specific to each attachment interface.
192+
mac := mac.MAC(iface.Attrs().HardwareAddr)
193+
if mac == nil {
194+
// L2-less device
195+
mac = make([]byte, 6)
196+
}
197+
opts["THIS_INTERFACE_MAC_1"] = uint64(sliceToBe32(mac[0:4]))
198+
opts["THIS_INTERFACE_MAC_2"] = uint64(sliceToBe16(mac[4:6]))
199+
200+
ifIndex := uint32(iface.Attrs().Index)
201+
202+
if !option.Config.EnableHostLegacyRouting {
203+
opts["SECCTX_FROM_IPCACHE"] = uint64(secctxFromIpcacheEnabled)
204+
} else {
205+
opts["SECCTX_FROM_IPCACHE"] = uint64(secctxFromIpcacheDisabled)
206+
}
207+
208+
opts["NATIVE_DEV_IFINDEX"] = uint64(ifIndex)
209+
210+
if option.Config.EnableBPFMasquerade && ifName != defaults.SecondHostDevice {
211+
ipv4, ipv6 := l.bpfMasqAddrs(ifName)
212+
213+
if option.Config.EnableIPv4Masquerade && ipv4.IsValid() {
214+
opts["IPV4_MASQUERADE"] = uint64(byteorder.NetIPv4ToHost32(ipv4.AsSlice()))
215+
}
216+
if option.Config.EnableIPv6Masquerade && ipv6.IsValid() {
217+
ipv6Bytes := ipv6.AsSlice()
218+
opts["IPV6_MASQUERADE_1"] = sliceToBe64(ipv6Bytes[0:8])
219+
opts["IPV6_MASQUERADE_2"] = sliceToBe64(ipv6Bytes[8:16])
220+
}
221+
}
222+
223+
callsMapHostDevice := bpf.LocalMapName(callsmap.HostMapName, templateLxcID)
224+
strings[callsMapHostDevice] = bpf.LocalMapName(callsmap.NetdevMapName, uint16(ifIndex))
225+
226+
return opts, strings, nil
227+
}
228+
180229
// patchHostNetdevDatapath calculates the changes necessary
181230
// to attach the host endpoint datapath to different interfaces.
182231
func (l *loader) patchHostNetdevDatapath(ep datapath.Endpoint, ifName string) (map[string]uint64, map[string]string, error) {
@@ -428,6 +477,51 @@ func (l *loader) reloadHostDatapath(ep datapath.Endpoint, spec *ebpf.CollectionS
428477
}
429478
}
430479

480+
if option.Config.EnableIPIPTermination {
481+
ipipDevices := []string{}
482+
if option.Config.IPv4Enabled() {
483+
ipipDevices = append(ipipDevices, defaults.IPIPv4Device)
484+
}
485+
if option.Config.IPv6Enabled() {
486+
ipipDevices = append(ipipDevices, defaults.IPIPv6Device)
487+
}
488+
for _, device := range ipipDevices {
489+
iface, err := safenetlink.LinkByName(device)
490+
if err != nil {
491+
log.WithError(err).WithField("device", device).Warn("Link does not exist")
492+
continue
493+
}
494+
495+
linkDir := bpffsDeviceLinksDir(bpf.CiliumPath(), iface)
496+
netdevConsts, netdevRenames, err := l.patchIPIPdevDatapath(ep, device)
497+
if err != nil {
498+
return err
499+
}
500+
501+
coll, commit, err := loadDatapath(spec, netdevRenames, netdevConsts)
502+
if err != nil {
503+
return err
504+
}
505+
defer coll.Close()
506+
507+
// Attach cil_from_netdev to ingress.
508+
if err := attachSKBProgram(iface, coll.Programs[symbolFromHostNetdevEp], symbolFromHostNetdevEp,
509+
linkDir, netlink.HANDLE_MIN_INGRESS, option.Config.EnableTCX); err != nil {
510+
return fmt.Errorf("interface %s ingress: %w", device, err)
511+
}
512+
513+
// Attach cil_to_netdev to egress.
514+
if err := attachSKBProgram(iface, coll.Programs[symbolToHostNetdevEp], symbolToHostNetdevEp,
515+
linkDir, netlink.HANDLE_MIN_EGRESS, option.Config.EnableTCX); err != nil {
516+
return fmt.Errorf("interface %s egress: %w", device, err)
517+
}
518+
519+
if err := commit(); err != nil {
520+
return fmt.Errorf("committing bpf pins: %w", err)
521+
}
522+
}
523+
}
524+
431525
// call at the end of the function so that we can easily detect if this removes necessary
432526
// programs that have just been attached.
433527
if err := removeObsoleteNetdevPrograms(devices); err != nil {

0 commit comments

Comments
 (0)