Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mux]: Fix mark_dhcp_packet #9373

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions files/build_templates/mux.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ StartLimitBurst=3
[Service]
User={{ sonicadmin_user }}
ExecStartPre=/usr/local/bin/write_standby.py
ExecStartPre=/usr/bin/{{docker_container_name}}.sh start
ExecStart=/usr/bin/{{docker_container_name}}.sh wait
ExecStop=/usr/bin/{{docker_container_name}}.sh stop
ExecStopPost=/usr/local/bin/write_standby.py
Restart=always
RestartSec=30

[Service]
User={{ sonicadmin_user }}
ExecStartPre=/usr/local/bin/mark_dhcp_packet.py
ExecStartPre=/usr/bin/{{docker_container_name}}.sh start
ExecStart=/usr/bin/{{docker_container_name}}.sh wait
ExecStop=/usr/bin/{{docker_container_name}}.sh stop
ExecStopPost=/usr/local/bin/mark_dhcp_packet.py
ExecStopPost=/usr/local/bin/write_standby.py
Restart=always
RestartSec=30

Expand Down
22 changes: 15 additions & 7 deletions files/scripts/mark_dhcp_packet.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env python3

import os
import subprocess
import sys
import time

from sonic_py_common import logger
from swsscommon import swsscommon

log = logger.Logger('mark_dhcp_packet')


class MarkDhcpPacket(object):
"""
Class used to configure dhcp packet mark in ebtables
Expand Down Expand Up @@ -38,7 +36,9 @@ def state_db(self):
Initializes the connector during the first call
"""
if self.state_db_connector is None:
self.state_db_connector = swsscommon.SonicV2Connector(host='127.0.0.1')
self.state_db_connector = swsscommon.SonicV2Connector(
host='127.0.0.1'
)
self.state_db_connector.connect(self.state_db_connector.STATE_DB)

return self.state_db_connector
Expand All @@ -51,7 +51,8 @@ def is_dualtor(self):
localhost_key = self.config_db.get_keys('DEVICE_METADATA')[0]
metadata = self.config_db.get_entry('DEVICE_METADATA', localhost_key)

return 'subtype' in metadata and 'dualtor' in metadata['subtype'].lower()
return 'subtype' in metadata and \
'dualtor' in metadata['subtype'].lower()

def get_mux_intfs(self):
"""
Expand Down Expand Up @@ -82,10 +83,16 @@ def clear_dhcp_packet_marks(self):
self.run_command("sudo ebtables -F INPUT")

def apply_mark_in_ebtables(self, intf, mark):
self.run_command("sudo ebtables -A INPUT -i {} -j mark --mark-set {}".format(intf, mark))
self.run_command("sudo ebtables -A INPUT -i {} -j mark --mark-set {}"
.format(intf, mark))

def update_mark_in_state_db(self, intf, mark):
self.state_db.set(self.state_db.STATE_DB, 'DHCP_PACKET_MARK|' + intf, 'mark', mark)
self.state_db.set(
self.state_db.STATE_DB,
'DHCP_PACKET_MARK|' + intf,
'mark',
mark
)

def apply_marks(self):
"""
Expand All @@ -103,6 +110,7 @@ def apply_marks(self):

log.log_info("Finish marking dhcp packets in ebtables.")


if __name__ == '__main__':
mark_dhcp_packet = MarkDhcpPacket()
mark_dhcp_packet.apply_marks()