Skip to content

Commit

Permalink
fix: Add MTU to NAD when using DPDK (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainbourgeois committed Aug 23, 2024
1 parent a8d2ed3 commit 31615a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,14 @@ def _create_nad_from_config(self, interface_name: str) -> NetworkAttachmentDefin
Returns:
NetworkAttachmentDefinition: NetworkAttachmentDefinition object
"""
nad_config = self._get_nad_base_config()
cni_type = self._charm_config.cni_type
# MTU is optional for bridge, macvlan, dpdk
# MTU is ignored by host-device
if cni_type != CNIType.host_device:
if interface_mtu := self._get_interface_mtu_config(interface_name):
nad_config.update({"mtu": interface_mtu})
nad_config = self._get_nad_base_config(interface_name)

nad_config["ipam"].update(
{"addresses": [{"address": self._get_network_ip_config(interface_name)}]}
)

cni_type = self._charm_config.cni_type

# host interface name is used only by macvlan and host-device
if host_interface := self._get_interface_config(interface_name):
if cni_type == CNIType.macvlan:
Expand Down Expand Up @@ -352,7 +350,7 @@ def _create_dpdk_access_nad_from_config(self) -> NetworkAttachmentDefinition:
Returns:
NetworkAttachmentDefinition: NetworkAttachmentDefinition object
"""
access_nad_config = self._get_nad_base_config()
access_nad_config = self._get_nad_base_config(ACCESS_INTERFACE_NAME)
access_nad_config.update({"type": "vfioveth"})

return NetworkAttachmentDefinition(
Expand All @@ -371,7 +369,7 @@ def _create_dpdk_core_nad_from_config(self) -> NetworkAttachmentDefinition:
Returns:
NetworkAttachmentDefinition: NetworkAttachmentDefinition object
"""
core_nad_config = self._get_nad_base_config()
core_nad_config = self._get_nad_base_config(CORE_INTERFACE_NAME)
core_nad_config.update({"type": "vfioveth"})

return NetworkAttachmentDefinition(
Expand All @@ -384,22 +382,28 @@ def _create_dpdk_core_nad_from_config(self) -> NetworkAttachmentDefinition:
spec={"config": json.dumps(core_nad_config)},
)

@staticmethod
def _get_nad_base_config() -> Dict[Any, Any]:
def _get_nad_base_config(self, interface_name: str) -> Dict[Any, Any]:
"""Get the base NetworkAttachmentDefinition.
This config is extended according to charm config.
Returns:
config (dict): Base NAD config
"""
return {
base_nad = {
"cniVersion": "0.3.1",
"ipam": {
"type": "static",
},
"capabilities": {"mac": True},
}
cni_type = self._charm_config.cni_type
# MTU is optional for bridge, macvlan, dpdk
# MTU is ignored by host-device
if cni_type != CNIType.host_device:
if interface_mtu := self._get_interface_mtu_config(interface_name):
base_nad.update({"mtu": interface_mtu})
return base_nad

def _write_upf_config_file_to_bessd_container(self, content: str) -> None:
push_file(
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,35 @@ def _get_create_core_nad_call(mock_call):
DPDK_CORE_INTERFACE_RESOURCE_NAME in nad_annotations["k8s.v1.cni.cncf.io/resourceName"]
)

def test_given_upf_configured_to_run_in_dpdk_mode_and_mtu_is_set_when_create_network_attachment_definitions_then_nads_have_mtu_specified( # noqa: E501
self, set_can_connect_containers, enable_huge_pages_multus_and_dpdk
):
self.mock_running_service()
self.mock_client_list.side_effect = [
[Node(status=NodeStatus(allocatable={"hugepages-1Gi": "3Gi"}))],
[Node(status=NodeStatus(allocatable={"hugepages-1Gi": "3Gi"}))],
[],
[],
[],
]
self.harness.update_config(
key_values={
"cni-type": "vfioveth",
"upf-mode": "dpdk",
"access-interface-mac-address": VALID_ACCESS_MAC,
"core-interface-mac-address": VALID_CORE_MAC,
"access-interface-mtu-size": 9000,
"core-interface-mtu-size": 9000,
}
)

nads = self.harness.charm._network_attachment_definitions_from_config()
for nad in nads:
assert nad.spec
config = json.loads(nad.spec["config"])
assert "mtu" in config
assert config["mtu"] == 9000

def test_given_upf_charm_configured_to_run_in_default_mode_when_patch_statefulset_then_2_network_annotations_are_created( # noqa: E501
self, set_can_connect_containers, enable_huge_pages_multus_and_dpdk
):
Expand Down

0 comments on commit 31615a8

Please sign in to comment.