Skip to content

Commit

Permalink
[image_config]: Enable Receive Packet Steering (RPS)
Browse files Browse the repository at this point in the history
 * This patch enables Receive Packet Steering (RPS) which helps to distribute
   softirq processing for CPU bound packets to all available cores. This will
   help prevent kernel packet drops when there are bouts of intense CPU bound
   packets.

   Ref: https://lwn.net/Articles/362339/
        https://docs.kernel.org/networking/scaling.html

Signed-off-by: Prabhat Aravind <paravind@microsoft.com>
  • Loading branch information
prabhataravind committed Sep 10, 2024
1 parent c883f16 commit 982a451
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,11 @@ sudo cp $IMAGE_CONFIGS/backend_acl/backend-acl.service $FILESYSTEM_ROOT_USR_LIB_
sudo cp $IMAGE_CONFIGS/backend_acl/backend_acl.py $FILESYSTEM_ROOT/usr/bin/backend_acl.py
echo "backend-acl.service" | sudo tee -a $GENERATED_SERVICE_FILE

# Copy RPS script and service file
sudo cp $IMAGE_CONFIGS/rps/rps.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM/rps.service
sudo cp $IMAGE_CONFIGS/rps/rps.py $FILESYSTEM_ROOT/usr/bin/rps.py
echo "rps.service" | sudo tee -a $GENERATED_SERVICE_FILE

# Copy SNMP configuration files
sudo cp $IMAGE_CONFIGS/snmp/snmp.yml $FILESYSTEM_ROOT/etc/sonic/

Expand Down
37 changes: 37 additions & 0 deletions files/image_config/rps/rps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3

import os
import ast
import subprocess

'''
Script to enable Receive Packet Steering (RPS)
'''

def exec_cmd(cmd):
p = subprocess.Popen(cmd, shell=False, executable='/bin/bash',
stdout = subprocess.PIPE)
return (p.communicate()[0].strip())


def configure_rps():
num_cpus = ast.literal_eval(exec_cmd("nproc"))
cpumask = hex(pow(2, num_cpus) - 1)[2:]
net_dir_path = "/sys/class/net"

for intf in os.listdir(net_dir_path):
if "Ethernet" in intf:
queues_path = os.path.join(net_dir_path, intf, "queues")
queues = os.listdir(queues_path)
num_rx_queues = len([q for q in queues if q.startswith("rx")])
for q in range(num_rx_queues):
path = os.path.join(queues_path, "rx-{}", "rps_cpus").format(q)
command = "echo {} | sudo tee {}".format(cpumask, path)
print ("Setting {} to cpu mask {}".format(path, cpumask))
print (command)
exec_cmd(command)


if __name__ == "__main__":
configure_rps()

14 changes: 14 additions & 0 deletions files/image_config/rps/rps.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Enable Receive Packet Steering (RPS) in kernel
After=swss.service
BindsTo=sonic.target
After=sonic.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/bin/rps.py
RemainAfterExit=yes

[Install]
WantedBy=sonic.target

0 comments on commit 982a451

Please sign in to comment.