Skip to content

Commit

Permalink
Merge branch 'master' into improve-azp
Browse files Browse the repository at this point in the history
  • Loading branch information
liushilongbuaa authored Nov 8, 2024
2 parents 195387b + d5fa793 commit 3a19ef0
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 391 deletions.
4 changes: 4 additions & 0 deletions .github/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: "CodeQL config"
queries:
- uses: security-and-quality
- uses: security-extended
43 changes: 43 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# For more infomation, please visit: https://github.com/github/codeql-action

name: "CodeQL"

on:
push:
branches:
- 'master'
- '202[0-9][0-9][0-9]'
pull_request_target:
branches:
- 'master'
- '202[0-9][0-9][0-9]'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'python' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: ./.github/codeql/codeql-config.yml
languages: ${{ matrix.language }}

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
22 changes: 22 additions & 0 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Semgrep

on:
pull_request: {}
push:
branches:
- master
- '201[7-9][0-1][0-9]'
- '202[0-9][0-1][0-9]'

jobs:
semgrep:
if: github.repository_owner == 'sonic-net'
name: Semgrep
runs-on: ubuntu-latest
container:
image: returntocorp/semgrep
steps:
- uses: actions/checkout@v3
- run: semgrep ci
env:
SEMGREP_RULES: p/default
27 changes: 19 additions & 8 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ schedules:
- 20????
always: true

parameters:
- name: dist
default: bullseye

variables:
DIFF_COVER_CHECK_THRESHOLD: 70
DIFF_COVER_CHECK_THRESHOLD: 80
DIFF_COVER_ENABLE: 'true'
DIFF_COVER_WORKING_DIRECTORY: $(System.DefaultWorkingDirectory)

pool:
vmImage: 'ubuntu-20.04'

container:
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-buster:latest
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-bullseye:master

steps:
- script: |
Expand Down Expand Up @@ -64,22 +68,29 @@ steps:
- script: |
set -xe
sudo apt-get -y purge libhiredis-dev libnl-3-dev libnl-route-3-dev
sudo dpkg -i $(find . -name "*.deb")
workingDirectory: $(Pipeline.Workspace)/target/
sudo apt install -y libhiredis0.14
sudo dpkg -i libnl-3-200_*.deb
sudo dpkg -i libnl-genl-3-200_*.deb
sudo dpkg -i libnl-route-3-200_*.deb
sudo dpkg -i libnl-nf-3-200_*.deb
sudo dpkg -i libyang_1.*.deb
sudo dpkg -i libswsscommon_1.0.0_amd64.deb
sudo dpkg -i python3-swsscommon_1.0.0_amd64.deb
workingDirectory: $(Pipeline.Workspace)/target/debs/${{ parameters.dist }}/
displayName: 'Install Debian dependencies'

- script: |
set -xe
find . -name "swsssdk-2.0.1-py3-none-any.whl" | xargs -i sudo pip3 install {}
find . -name "sonic_py_common-1.0-py3-none-any.whl" | xargs -i sudo pip3 install {}
workingDirectory: $(Pipeline.Workspace)/target/
sudo pip3 install swsssdk-2.0.1-py3-none-any.whl
sudo pip3 install sonic_py_common-1.0-py3-none-any.whl
workingDirectory: $(Pipeline.Workspace)/target/python-wheels/${{ parameters.dist }}/
displayName: 'Install Python dependencies'

- script: |
set -ex
# Install .NET CORE
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/debian/10/prod
sudo apt-add-repository https://packages.microsoft.com/debian/11/prod
sudo apt-get update
sudo apt-get install -y dotnet-sdk-5.0
displayName: "Install .NET CORE"
Expand Down
68 changes: 54 additions & 14 deletions src/lldp_syncd/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ def parse_time(time_str):
}
:return: parsed age in time ticks (or seconds)
"""
days, hour_min_secs = re.split(LLDPD_UPTIME_RE_SPLIT_PATTERN, time_str)
struct_time = time.strptime(hour_min_secs, LLDPD_TIME_FORMAT)
time_delta = datetime.timedelta(days=int(days), hours=struct_time.tm_hour,
minutes=struct_time.tm_min,
seconds=struct_time.tm_sec)
return int(time_delta.total_seconds())
try:
days, hour_min_secs = re.split(LLDPD_UPTIME_RE_SPLIT_PATTERN, time_str)
struct_time = time.strptime(hour_min_secs, LLDPD_TIME_FORMAT)
time_delta = datetime.timedelta(days=int(days), hours=struct_time.tm_hour,
minutes=struct_time.tm_min,
seconds=struct_time.tm_sec)
return int(time_delta.total_seconds())
except ValueError:
logger.exception("Failed to parse lldp age {} -- ".format(time_str))
return 0


class LldpSyncDaemon(SonicSyncDaemon):
Expand Down Expand Up @@ -190,6 +194,8 @@ def source_update(self):
logger.debug("Invoking lldpcli with: {}".format(cmd_local))

lldp_json = self._scrap_output(cmd)
if lldp_json is None:
return None
lldp_json['lldp_loc_chassis'] = self._scrap_output(cmd_local)

return lldp_json
Expand Down Expand Up @@ -264,7 +270,7 @@ def parse_update(self, lldp_json):
parsed_interfaces[if_name].update({'lldp_rem_sys_cap_enabled':
self.parse_sys_capabilities(
capability_list, enabled=True)})
if lldp_json['lldp_loc_chassis']:
if lldp_json.get('lldp_loc_chassis'):
loc_chassis_keys = ('lldp_loc_chassis_id_subtype',
'lldp_loc_chassis_id',
'lldp_loc_sys_name',
Expand Down Expand Up @@ -345,6 +351,26 @@ def cache_diff(self, cache, update):

return new_keys, changed_keys, deleted_keys

def is_only_time_mark_modified(self, cached_interface, updated_interface):
"""
Check if only lldp_rem_time_mark is modified in the update
:param cached_interface: Local cached interface dict
:param updated_interface: Updated interface dict
:return: True if only lldp_rem_time_mark is modified, False otherwise
"""
if len(cached_interface) != len(updated_interface):
return False

changed_keys = 0

for key in cached_interface.keys():
if 'lldp_rem_time_mark' == key and cached_interface[key] != updated_interface[key]:
changed_keys += 1
elif key not in updated_interface or cached_interface[key] != updated_interface[key]:
return False

return True if changed_keys == 1 else False

def sync(self, parsed_update):
"""
Sync LLDP information to redis DB.
Expand All @@ -363,18 +389,32 @@ def sync(self, parsed_update):
logger.debug("sync'd: {}".format(json.dumps(chassis_update, indent=3)))

new, changed, deleted = self.cache_diff(self.interfaces_cache, parsed_update)

# For changed elements, if only lldp_rem_time_mark changed, update its value, otherwise delete and repopulate
for interface in changed:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
logger.warning("Ignoring interface '{}'".format(interface))
continue
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
if self.is_only_time_mark_modified(self.interfaces_cache[interface], parsed_update[interface]):
self.db_connector.set(self.db_connector.APPL_DB, table_key, 'lldp_rem_time_mark', parsed_update[interface]['lldp_rem_time_mark'], blocking=True)
logger.debug("Only sync'd interface {} lldp_rem_time_mark: {}".format(interface, parsed_update[interface]['lldp_rem_time_mark']))
else:
self.db_connector.delete(self.db_connector.APPL_DB, table_key)
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.debug("Sync'd changed interface {} : {}".format(interface, parsed_update[interface]))
self.interfaces_cache = parsed_update
# Delete LLDP_ENTRIES which were modified or are missing
for interface in changed + deleted:
# Delete LLDP_ENTRIES which are missing
for interface in deleted:
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
self.db_connector.delete(self.db_connector.APPL_DB, table_key)
# Repopulate LLDP_ENTRY_TABLE by adding all changed elements
for interface in changed + new:
logger.debug("Delete table_key: {}".format(table_key))
# Repopulate LLDP_ENTRY_TABLE by adding new elements
for interface in new:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
logger.warning("Ignoring interface '{}'".format(interface))
continue
# port_table_key = LLDP_ENTRY_TABLE:INTERFACE_NAME;
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
for k, v in parsed_update[interface].items():
self.db_connector.set(self.db_connector.APPL_DB, table_key, k, v, blocking=True)
logger.debug("sync'd: \n{}".format(json.dumps(parsed_update[interface], indent=3)))
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.debug("Add new interface {} : {}".format(interface, parsed_update[interface]))
Loading

0 comments on commit 3a19ef0

Please sign in to comment.