Skip to content

Commit

Permalink
bpf/init.sh: move node config generation to Go
Browse files Browse the repository at this point in the history
This refactors writes to node_config.h in init.sh to a Go implementation
and centralizes most writes to node_config.h in writeNodeConfigHeader().

Signed-off-by: Robin Gögge <r.goegge@isovalent.com>
  • Loading branch information
rgo3 committed May 24, 2023
1 parent 2dcf1b0 commit 4efde2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
41 changes: 0 additions & 41 deletions bpf/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ function setup_proxy_rules()
fi
}

function mac2array()
{
echo "{0x${1//:/,0x}}"
}

function rnd_mac_addr()
{
local lower=$(od /dev/urandom -N5 -t x1 -An | sed 's/ /:/g')
Expand All @@ -186,37 +181,6 @@ function encap_fail()
exit 1
}

# node_config.h header generation
case "${MODE}" in
*)
sed -i '/^#.*CILIUM_NET_MAC.*$/d' $RUNDIR/globals/node_config.h
CILIUM_NET_MAC=$(ip link show $HOST_DEV2 | grep ether | awk '{print $2}')
CILIUM_NET_MAC=$(mac2array $CILIUM_NET_MAC)

# Remove the entire '#ifndef ... #endif block
# Each line must contain the string '#.*CILIUM_NET_MAC.*'
sed -i '/^#.*CILIUM_NET_MAC.*$/d' $RUNDIR/globals/node_config.h
echo "#ifndef CILIUM_NET_MAC" >> $RUNDIR/globals/node_config.h
echo "#define CILIUM_NET_MAC { .addr = ${CILIUM_NET_MAC}}" >> $RUNDIR/globals/node_config.h
echo "#endif /* CILIUM_NET_MAC */" >> $RUNDIR/globals/node_config.h

sed -i '/^#.*HOST_IFINDEX.*$/d' $RUNDIR/globals/node_config.h
HOST_IDX=$(cat "${SYSCLASSNETDIR}/${HOST_DEV2}/ifindex")
echo "#define HOST_IFINDEX $HOST_IDX" >> $RUNDIR/globals/node_config.h

sed -i '/^#.*HOST_IFINDEX_MAC.*$/d' $RUNDIR/globals/node_config.h
HOST_MAC=$(ip link show $HOST_DEV1 | grep ether | awk '{print $2}')
HOST_MAC=$(mac2array $HOST_MAC)
echo "#define HOST_IFINDEX_MAC { .addr = ${HOST_MAC}}" >> $RUNDIR/globals/node_config.h

sed -i '/^#.*CILIUM_IFINDEX.*$/d' $RUNDIR/globals/node_config.h
CILIUM_IDX=$(cat "${SYSCLASSNETDIR}/${HOST_DEV1}/ifindex")
echo "#define CILIUM_IFINDEX $CILIUM_IDX" >> $RUNDIR/globals/node_config.h

CILIUM_EPHEMERAL_MIN=$(cat "${PROCSYSNETDIR}/ipv4/ip_local_port_range" | awk '{print $1}')
echo "#define EPHEMERAL_MIN $CILIUM_EPHEMERAL_MIN" >> $RUNDIR/globals/node_config.h
esac

# If the host does not have an IPv6 address assigned, assign our generated host
# IP to make the host accessible to endpoints
if [ "$IP6_HOST" != "<nil>" ]; then
Expand Down Expand Up @@ -283,11 +247,6 @@ else
ip link del cilium_sit 2> /dev/null || true
fi

if [ "$MODE" = "tunnel" ]; then
sed -i '/^#.*TUNNEL_MODE.*$/d' $RUNDIR/globals/node_config.h
echo "#define TUNNEL_MODE 1" >> $RUNDIR/globals/node_config.h
fi

# Remove eventual existing encapsulation device from previous run
case "${TUNNEL_PROTOCOL}" in
"<nil>")
Expand Down
44 changes: 44 additions & 0 deletions pkg/datapath/linux/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"net"
"sort"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -57,6 +58,7 @@ import (
"github.com/cilium/cilium/pkg/netns"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/sysctl"
wgtypes "github.com/cilium/cilium/pkg/wireguard/types"
)

Expand Down Expand Up @@ -637,6 +639,30 @@ func (h *HeaderfileWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeC
cDefinesMap["CIDR_IDENTITY_RANGE_START"] = fmt.Sprintf("%d", identity.MinLocalIdentity)
cDefinesMap["CIDR_IDENTITY_RANGE_END"] = fmt.Sprintf("%d", identity.MaxLocalIdentity)

if option.Config.TunnelingEnabled() {
cDefinesMap["TUNNEL_MODE"] = "1"
}

ciliumNetLink, err := netlink.LinkByName(defaults.SecondHostDevice)
if err != nil {
return err
}
cDefinesMap["CILIUM_NET_MAC"] = fmt.Sprintf("{.addr=%s}", mac.CArrayString(ciliumNetLink.Attrs().HardwareAddr))
cDefinesMap["HOST_IFINDEX"] = fmt.Sprintf("%d", ciliumNetLink.Attrs().Index)

ciliumHostLink, err := netlink.LinkByName(defaults.HostDevice)
if err != nil {
return err
}
cDefinesMap["HOST_IFINDEX_MAC"] = fmt.Sprintf("{.addr=%s}", mac.CArrayString(ciliumHostLink.Attrs().HardwareAddr))
cDefinesMap["CILIUM_IFINDEX"] = fmt.Sprintf("%d", ciliumHostLink.Attrs().Index)

ephemeralMin, err := getEphemeralPortRangeMin()
if err != nil {
return err
}
cDefinesMap["EPHEMERAL_MIN"] = fmt.Sprintf("%d", ephemeralMin)

// Since golang maps are unordered, we sort the keys in the map
// to get a consistent written format to the writer. This maintains
// the consistency when we try to calculate hash for a datapath after
Expand Down Expand Up @@ -671,6 +697,24 @@ func (h *HeaderfileWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeC
return fw.Flush()
}

func getEphemeralPortRangeMin() (int, error) {
ephemeralPortRangeStr, err := sysctl.Read("net.ipv4.ip_local_port_range")
if err != nil {
return 0, fmt.Errorf("unable to read net.ipv4.ip_local_port_range: %w", err)
}
ephemeralPortRange := strings.Split(ephemeralPortRangeStr, "\t")
if len(ephemeralPortRange) != 2 {
return 0, fmt.Errorf("invalid ephemeral port range: %s", ephemeralPortRangeStr)
}
ephemeralPortMin, err := strconv.Atoi(ephemeralPortRange[0])
if err != nil {
return 0, fmt.Errorf("unable to parse min port value %s for ephemeral range: %w",
ephemeralPortRange[0], err)
}

return ephemeralPortMin, nil
}

// vlanFilterMacros generates VLAN_FILTER macros which
// are written to node_config.h
func vlanFilterMacros() (string, error) {
Expand Down

0 comments on commit 4efde2e

Please sign in to comment.