Skip to content

Commit

Permalink
Add a port index mapper service for sFlow (#4794)
Browse files Browse the repository at this point in the history
* Add a port index mapper service for sFlow
  • Loading branch information
padmanarayana authored Jun 24, 2020
1 parent 563a0fd commit 5cacc20
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dockers/docker-sflow/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ RUN apt-get update && \
dmidecode \
libmnl0=1.0.4-2

RUN pip install \
pyroute2==0.5.3

{% if docker_sflow_debs.strip() -%}
# Copy locally-built Debian package dependencies
{{ copy_files("debs/", docker_sflow_debs.split(' '), "/debs/") }}
Expand All @@ -30,5 +33,6 @@ RUN sed -ri '/^DAEMON_ARGS=""/c DAEMON_ARGS="-c /var/log/hsflowd.crash"' /etc/in
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["critical_processes", "/etc/supervisor"]
COPY ["port_index_mapper.py", "/usr/bin"]

ENTRYPOINT ["/usr/bin/supervisord"]
114 changes: 114 additions & 0 deletions dockers/docker-sflow/port_index_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python

""" port_index_mapper
A mapper service that watches for NetLink NEWLINK and DELLINKs
to construct a PORT_INDEX_TABLE in state DB which includes the
interface name, the interface index and the ifindex.
Note : Currently supports only interfaces supported by port_util.
"""

import os
import sys
import syslog
import signal
import traceback
from pyroute2 import IPDB
from pyroute2.iproute import RTM_NEWLINK, RTM_DELLINK
from swsssdk import SonicV2Connector, port_util

PORT_INDEX_TABLE_NAME = 'PORT_INDEX_TABLE'
SYSLOG_IDENTIFIER = 'port_index_mapper'

ipdb = None
state_db = None

def set_port_index_table_entry(key, index, ifindex):
state_db.set(state_db.STATE_DB, key, 'index', index)
state_db.set(state_db.STATE_DB, key, 'ifindex', ifindex)

def interface_callback(ipdb, nlmsg, action):
global state_db

try:
msgtype = nlmsg['header']['type']
if (msgtype != RTM_NEWLINK and msgtype != RTM_DELLINK):
return

# filter out unwanted messages
change = nlmsg['change']
if (change != 0xFFFFFFFF):
return

attrs = nlmsg['attrs']
for list in attrs:
if list[0] == 'IFLA_IFNAME':
ifname = list[1]
break
else:
return

# Extract the port index from the interface name
index = port_util.get_index_from_str(ifname)
if index is None:
return

_hash = '{}|{}'.format(PORT_INDEX_TABLE_NAME, ifname)

if msgtype == RTM_NEWLINK:
set_port_index_table_entry(_hash, str(index), nlmsg['index'])
elif msgtype == RTM_DELLINK:
state_db.delete(state_db.STATE_DB, _hash)

except Exception, e:
t = sys.exc_info()[2]
traceback.print_tb(t)
syslog.syslog(syslog.LOG_CRIT, "%s" % str(e))
os.kill(os.getpid(), signal.SIGTERM)

def main():
global state_db, ipdb
state_db = SonicV2Connector(host='127.0.0.1')
state_db.connect(state_db.STATE_DB, False)

ipdb = IPDB()

# Initialize the table at startup.
ifnames = ipdb.by_name.keys()
for ifname in ifnames:
index = port_util.get_index_from_str(ifname)
if index is None:
continue
ifindex = ipdb.interfaces[ifname]['index']
_hash = '{}|{}'.format(PORT_INDEX_TABLE_NAME, ifname)
set_port_index_table_entry(_hash, str(index), str(ifindex))

ipdb.register_callback(interface_callback)

signal.pause()

def signal_handler(signum, frame):
syslog.syslog(syslog.LOG_NOTICE, "got signal %d" % signum)
sys.exit(0)

if __name__ == '__main__':
rc = 0
try:
syslog.openlog(SYSLOG_IDENTIFIER)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
main()
except Exception, e:
t = sys.exc_info()[2]
traceback.print_tb(t)
syslog.syslog(syslog.LOG_CRIT, "%s" % str(e))
rc = -1
finally:
if ipdb is not None:
ipdb.release()
else:
syslog.syslog(syslog.LOG_ERR, "ipdb undefined in signal_handler")

syslog.closelog()
sys.exit(rc)

10 changes: 10 additions & 0 deletions dockers/docker-sflow/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running

[program:port_index_mapper]
command=/usr/bin/port_index_mapper.py
priority=3
autostart=false
autorestart=false
stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running

0 comments on commit 5cacc20

Please sign in to comment.