-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add service mark_dhcp_packet to mux container #9015
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #!/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 | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.config_db_connector = None | ||
| self.state_db_connector = None | ||
|
|
||
| @property | ||
| def config_db(self): | ||
| """ | ||
| Returns config DB connector. | ||
| Initializes the connector during the first call | ||
| """ | ||
| if self.config_db_connector is None: | ||
| self.config_db_connector = swsscommon.ConfigDBConnector() | ||
| self.config_db_connector.connect() | ||
|
|
||
| return self.config_db_connector | ||
|
|
||
| @property | ||
| def state_db(self): | ||
| """ | ||
| Returns the state DB connector. | ||
| 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.connect(self.state_db_connector.STATE_DB) | ||
|
|
||
| return self.state_db_connector | ||
|
|
||
| @property | ||
| def is_dualtor(self): | ||
| """ | ||
| Checks if script is running on a dual ToR system | ||
| """ | ||
| 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() | ||
|
|
||
| def get_mux_intfs(self): | ||
| """ | ||
| Returns a list of mux cable interfaces | ||
| """ | ||
| mux_cables = self.config_db.get_table('MUX_CABLE') | ||
| mux_intfs = [intf for intf in mux_cables if intf.startswith('Ethernet')] | ||
|
|
||
| return mux_intfs | ||
|
|
||
| def generate_mark_for_intf(self, intf): | ||
| ''' | ||
| type: string, format: hexadecimal | ||
| ''' | ||
| intf_index_str = intf[8:] | ||
| intf_mark = "0x67" + intf_index_str.zfill(3) | ||
|
|
||
| return intf_mark | ||
|
|
||
| def run_command(self, cmd): | ||
| subprocess.call(cmd, shell=True) | ||
| log.log_info("run command: {}".format(cmd)) | ||
|
|
||
| def clear_dhcp_packet_marks(self): | ||
| self.run_command("sudo ebtables -F INPUT") | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def apply_mark_in_ebtables(self, intf): | ||
| mark = self.generate_mark_for_intf(intf) | ||
| 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.generate_mark_for_intf(intf) | ||
| self.state_db.set(self.state_db.STATE_DB, 'DHCP_PACKET_MARK', intf, mark) | ||
|
|
||
| def apply_marks(self): | ||
| """ | ||
| Writes dhcp packet marks in ebtables | ||
| """ | ||
| if not self.is_dualtor: | ||
| return | ||
|
|
||
| self.clear_dhcp_packet_marks() | ||
|
|
||
| mux_intfs = self.get_mux_intfs() | ||
|
|
||
| for intf in mux_intfs: | ||
| self.apply_mark_in_ebtables(intf) | ||
| self.update_mark_in_state_db(intf) | ||
|
|
||
| log.log_info("Finish marking dhcp packets in ebtables.") | ||
|
|
||
| if __name__ == '__main__': | ||
| mark_dhcp_packet = MarkDhcpPacket() | ||
| mark_dhcp_packet.apply_marks() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.