Arthur 1 dzień temu
rodzic
commit
dd08c38254

+ 10 - 10
docs/models/dcim/interface.md

@@ -113,19 +113,19 @@ Physical interfaces may be arranged into link aggregation groups (LAGs, also kno
 
 The link aggregation mode of a LAG interface. (This field may be set only on interfaces of type LAG.) Choices include:
 
-| Mode           | Linux bonding mode |
-|----------------|--------------------|
-| LACP (802.3ad) | `802.3ad`          |
-| Static         | `balance-xor`      |
-| Active-Backup  | `active-backup`    |
-| Balance-RR     | `balance-rr`       |
-| Balance-TLB    | `balance-tlb`      |
-| Balance-ALB    | `balance-alb`      |
-| Broadcast      | `broadcast`        |
+| Mode           | Group   | Linux bonding mode |
+|----------------|---------|--------------------|
+| LACP (802.3ad) | Generic | `802.3ad`          |
+| Static         | Generic | `balance-xor`      |
+| Active-Backup  | Generic | `active-backup`    |
+| Balance-RR     | Linux   | `balance-rr`       |
+| Balance-TLB    | Linux   | `balance-tlb`      |
+| Balance-ALB    | Linux   | `balance-alb`      |
+| Broadcast      | Linux   | `broadcast`        |
 
 ### LAG Hash Policy
 
-The transmit hash policy used to distribute traffic across the members of a LAG interface: layer 2, layer 2+3, layer 3+4, encapsulated layer 2+3, or encapsulated layer 3+4. (This field may be set only on LAG interfaces whose mode is LACP, static, or balance-TLB, as other modes do not select members by hash.)
+The transmit hash policy used to distribute traffic across the members of a LAG interface: layer 2, layer 2+3, layer 3+4, encapsulated layer 2+3, encapsulated layer 3+4, or VLAN + source MAC. (This field may be set only on LAG interfaces whose mode is LACP or static.)
 
 ### PoE Mode
 

+ 24 - 7
netbox/dcim/choices.py

@@ -1672,13 +1672,28 @@ class InterfaceLAGModeChoices(ChoiceSet):
     MODE_BROADCAST = 'broadcast'
 
     CHOICES = (
-        Choice(MODE_LACP, _('LACP (802.3ad)'), description=_('Dynamic link aggregation negotiated via LACP')),
-        Choice(MODE_STATIC, _('Static'), description=_('Static aggregation with hash-based balancing (balance-xor)')),
-        Choice(MODE_ACTIVE_BACKUP, _('Active-Backup'), description=_('One active member; others on standby')),
-        Choice(MODE_BALANCE_RR, _('Balance-RR'), description=_('Round-robin transmission across members')),
-        Choice(MODE_BALANCE_TLB, _('Balance-TLB'), description=_('Adaptive transmit load balancing')),
-        Choice(MODE_BALANCE_ALB, _('Balance-ALB'), description=_('Adaptive transmit and receive load balancing')),
-        Choice(MODE_BROADCAST, _('Broadcast'), description=_('Transmit on all members')),
+        (
+            _('Generic'),
+            (
+                Choice(MODE_LACP, _('LACP (802.3ad)'), description=_('Dynamic link aggregation negotiated via LACP')),
+                Choice(
+                    MODE_STATIC, _('Static'),
+                    description=_('Static aggregation with hash-based balancing (balance-xor)'),
+                ),
+                Choice(MODE_ACTIVE_BACKUP, _('Active-Backup'), description=_('One active member; others on standby')),
+            ),
+        ),
+        (
+            _('Linux'),
+            (
+                Choice(MODE_BALANCE_RR, _('Balance-RR'), description=_('Round-robin transmission across members')),
+                Choice(MODE_BALANCE_TLB, _('Balance-TLB'), description=_('Adaptive transmit load balancing')),
+                Choice(
+                    MODE_BALANCE_ALB, _('Balance-ALB'), description=_('Adaptive transmit and receive load balancing')
+                ),
+                Choice(MODE_BROADCAST, _('Broadcast'), description=_('Transmit on all members')),
+            ),
+        ),
     )
 
 
@@ -1689,6 +1704,7 @@ class InterfaceLAGHashPolicyChoices(ChoiceSet):
     POLICY_LAYER3_4 = 'layer3+4'
     POLICY_ENCAP2_3 = 'encap2+3'
     POLICY_ENCAP3_4 = 'encap3+4'
+    POLICY_VLAN_SRCMAC = 'vlan+srcmac'
 
     CHOICES = (
         Choice(POLICY_LAYER2, _('Layer 2')),
@@ -1696,6 +1712,7 @@ class InterfaceLAGHashPolicyChoices(ChoiceSet):
         Choice(POLICY_LAYER3_4, _('Layer 3+4')),
         Choice(POLICY_ENCAP2_3, _('Encapsulated Layer 2+3')),
         Choice(POLICY_ENCAP3_4, _('Encapsulated Layer 3+4')),
+        Choice(POLICY_VLAN_SRCMAC, _('VLAN + Source MAC')),
     )
 
 

+ 0 - 1
netbox/dcim/constants.py

@@ -88,7 +88,6 @@ NONCONNECTABLE_IFACE_TYPES = VIRTUAL_IFACE_TYPES + WIRELESS_IFACE_TYPES + [
 LAG_HASH_MODES = (
     InterfaceLAGModeChoices.MODE_LACP,
     InterfaceLAGModeChoices.MODE_STATIC,
-    InterfaceLAGModeChoices.MODE_BALANCE_TLB,
 )
 
 

+ 9 - 10
netbox/dcim/forms/model_forms.py

@@ -2037,6 +2037,7 @@ class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
     type = ChoiceField(
         label=_('Type'),
         choices=InterfaceTypeChoices,
+        widget=HTMXSelect(hx_target_id='lag-attributes'),
     )
     duplex = TypedChoiceField(
         label=_('Duplex'),
@@ -2219,24 +2220,22 @@ class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
 
-        # A hash policy applies only to LAG modes which hash
-        if get_field_value(self, 'lag_mode') not in LAG_HASH_MODES:
+        # LAG attributes apply only to LAG interfaces, and a hash policy only to LAG modes which hash
+        if get_field_value(self, 'type') != InterfaceTypeChoices.TYPE_LAG:
+            del self.fields['lag_mode']
+            del self.fields['lag_hash_policy']
+        elif get_field_value(self, 'lag_mode') not in LAG_HASH_MODES:
             del self.fields['lag_hash_policy']
 
     def clean(self):
         super().clean()
 
-        # Clear a hash policy left over from a previous LAG mode
+        # Clear any LAG attributes which no longer apply
+        if 'lag_mode' not in self.fields:
+            self.instance.lag_mode = None
         if 'lag_hash_policy' not in self.fields:
             self.instance.lag_hash_policy = None
 
-        # Clear LAG attributes left over when changing a LAG interface to another type
-        if self.instance.is_lag and self.cleaned_data.get('type') != InterfaceTypeChoices.TYPE_LAG:
-            self.cleaned_data['lag_mode'] = None
-            self.instance.lag_hash_policy = None
-            if 'lag_hash_policy' in self.fields:
-                self.cleaned_data['lag_hash_policy'] = None
-
 
 class FrontPortForm(FrontPortFormMixin, ModularDeviceComponentForm):
     type = ChoiceField(

+ 1 - 1
netbox/dcim/models/device_components.py

@@ -1340,7 +1340,7 @@ class Interface(
             raise ValidationError({'lag_hash_policy': _("LAG hash policy may be set only on LAG interfaces.")})
         if self.lag_hash_policy and self.lag_mode not in LAG_HASH_MODES:
             raise ValidationError({
-                'lag_hash_policy': _("LAG hash policy may be set only for LACP, static, or balance-TLB LAG modes."),
+                'lag_hash_policy': _("LAG hash policy may be set only for LACP or static LAG modes."),
             })
 
         # Wireless validation

+ 24 - 0
netbox/dcim/tests/test_api.py

@@ -4790,6 +4790,30 @@ class InterfaceTestCase(Mixins.FieldPrefetchMixin, Mixins.ComponentTraceMixin, A
         self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
         self.assertIn('lag_hash_policy', response.json())
 
+    def test_change_lag_mode_to_non_hash_mode(self):
+        """Changing to a non-hashing LAG mode via the API requires explicitly clearing the hash policy."""
+        self.add_permissions('dcim.change_interface')
+        lag = Interface.objects.create(
+            device=Device.objects.first(),
+            name='bond0',
+            type=InterfaceTypeChoices.TYPE_LAG,
+            lag_mode=InterfaceLAGModeChoices.MODE_LACP,
+            lag_hash_policy=InterfaceLAGHashPolicyChoices.POLICY_LAYER3_4,
+        )
+        url = self._get_detail_url(lag)
+
+        data = {'lag_mode': InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP}
+        response = self.client.patch(url, data, format='json', **self.header)
+        self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
+        self.assertIn('lag_hash_policy', response.json())
+
+        data['lag_hash_policy'] = None
+        response = self.client.patch(url, data, format='json', **self.header)
+        self.assertHttpStatus(response, status.HTTP_200_OK)
+        lag.refresh_from_db()
+        self.assertEqual(lag.lag_mode, InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP)
+        self.assertFalse(lag.lag_hash_policy)
+
     def test_graphql_filter_lag_attributes(self):
         self.add_permissions('dcim.view_interface')
         lag = Interface.objects.create(

+ 19 - 14
netbox/dcim/tests/test_filtersets.py

@@ -5859,20 +5859,6 @@ class InterfaceTestCase(TestCase, DeviceComponentFilterSetTestMixin, ChangeLogge
         # Wireless links
         WirelessLink(interface_a=interfaces[7], interface_b=interfaces[8]).save()
 
-        # LAG attributes (set directly; validation is covered in test_models)
-        Interface.objects.filter(pk=interfaces[0].pk).update(
-            lag_mode=InterfaceLAGModeChoices.MODE_LACP,
-            lag_hash_policy=InterfaceLAGHashPolicyChoices.POLICY_LAYER2,
-        )
-        Interface.objects.filter(pk=interfaces[1].pk).update(
-            lag_mode=InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP,
-            lag_hash_policy=InterfaceLAGHashPolicyChoices.POLICY_LAYER2_3,
-        )
-        Interface.objects.filter(pk=interfaces[2].pk).update(
-            lag_mode=InterfaceLAGModeChoices.MODE_BALANCE_RR,
-            lag_hash_policy=InterfaceLAGHashPolicyChoices.POLICY_LAYER3_4,
-        )
-
     def test_name(self):
         params = {'name': ['Interface 1', 'Interface 2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
@@ -5913,11 +5899,30 @@ class InterfaceTestCase(TestCase, DeviceComponentFilterSetTestMixin, ChangeLogge
         params = {'poe_type': [InterfacePoETypeChoices.TYPE_1_8023AF, InterfacePoETypeChoices.TYPE_2_8023AT]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
 
+    def _create_lag_interfaces(self):
+        device = Device.objects.first()
+        for name, lag_mode, lag_hash_policy in (
+            ('LAG 1', InterfaceLAGModeChoices.MODE_LACP, InterfaceLAGHashPolicyChoices.POLICY_LAYER2),
+            ('LAG 2', InterfaceLAGModeChoices.MODE_STATIC, InterfaceLAGHashPolicyChoices.POLICY_LAYER2_3),
+            ('LAG 3', InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP, None),
+        ):
+            interface = Interface(
+                device=device,
+                name=name,
+                type=InterfaceTypeChoices.TYPE_LAG,
+                lag_mode=lag_mode,
+                lag_hash_policy=lag_hash_policy,
+            )
+            interface.full_clean()
+            interface.save()
+
     def test_lag_mode(self):
+        self._create_lag_interfaces()
         params = {'lag_mode': [InterfaceLAGModeChoices.MODE_LACP, InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
     def test_lag_hash_policy(self):
+        self._create_lag_interfaces()
         params = {'lag_hash_policy': [
             InterfaceLAGHashPolicyChoices.POLICY_LAYER2, InterfaceLAGHashPolicyChoices.POLICY_LAYER2_3,
         ]}

+ 8 - 4
netbox/dcim/tests/test_forms.py

@@ -1353,8 +1353,10 @@ class InterfaceTestCase(TestCase):
         self.assertIsNone(lag.lag_mode)
         self.assertIsNone(lag.lag_hash_policy)
 
-    def test_lag_mode_on_non_lag_interface_rejected(self):
-        """A LAG mode chosen for a non-LAG interface is rejected rather than silently dropped."""
+    def test_lag_attributes_only_for_lag_interfaces(self):
+        """LAG attributes are offered only for LAG interfaces, and are not saved for any other type."""
+        self.assertIn('lag_mode', InterfaceForm(initial={'type': InterfaceTypeChoices.TYPE_LAG}).fields)
+
         data = {
             'device': self.device.pk,
             'name': 'eth9',
@@ -1362,8 +1364,10 @@ class InterfaceTestCase(TestCase):
             'lag_mode': InterfaceLAGModeChoices.MODE_LACP,
         }
         form = InterfaceForm(data)
-        self.assertFalse(form.is_valid())
-        self.assertIn('lag_mode', form.errors)
+        self.assertNotIn('lag_mode', form.fields)
+        self.assertNotIn('lag_hash_policy', form.fields)
+        self.assertTrue(form.is_valid(), form.errors)
+        self.assertIsNone(form.save().lag_mode)
 
     def test_child_interface_preselects_every_parent_vdc(self):
         """A child interface's form pre-selects every virtual device context of its parent."""

+ 1 - 1
netbox/dcim/tests/test_models.py

@@ -3770,7 +3770,7 @@ class InterfaceLAGAttributesTestCase(TestCase):
             (None, False),
             (InterfaceLAGModeChoices.MODE_LACP, True),
             (InterfaceLAGModeChoices.MODE_STATIC, True),
-            (InterfaceLAGModeChoices.MODE_BALANCE_TLB, True),
+            (InterfaceLAGModeChoices.MODE_BALANCE_TLB, False),
             (InterfaceLAGModeChoices.MODE_ACTIVE_BACKUP, False),
             (InterfaceLAGModeChoices.MODE_BALANCE_RR, False),
             (InterfaceLAGModeChoices.MODE_BALANCE_ALB, False),

+ 2 - 2
netbox/utilities/templates/form_helpers/render_fieldset.html

@@ -1,7 +1,7 @@
 {% load i18n %}
 {% load form_helpers %}
-<div class="field-group mb-5"{% if html_id %} id="{{ html_id }}"{% endif %}{% if heading %} role="group" aria-label="{{ heading }}"{% endif %}>
-  {% if heading %}
+<div class="field-group{% if rows %} mb-5{% endif %}"{% if html_id %} id="{{ html_id }}"{% endif %}{% if heading %} role="group" aria-label="{{ heading }}"{% endif %}>
+  {% if heading and rows %}
     <div class="row">
       <h2 class="col-9 offset-3">{{ heading }}</h2>
     </div>