netifd: add flow steering mode to the packet steering script
[openwrt/staging/nbd.git] / package / network / config / netifd / files / usr / libexec / network / packet-steering.uc
index 72f96024d8101b10213a1f0626030b0f01019e1d..a578e288791d7d4a3d5bb44a222614af227b419f 100755 (executable)
@@ -9,8 +9,11 @@ let eth_bias = 2.0;
 let debug = 0, do_nothing = 0;
 let disable;
 let cpus;
+let all_cpus;
+let local_flows = 0;
 
-for (let arg in ARGV) {
+while (length(ARGV) > 0) {
+       let arg = shift(ARGV);
        switch (arg) {
        case "-d":
                debug++;
@@ -21,6 +24,12 @@ for (let arg in ARGV) {
        case '0':
                disable = true;
                break;
+       case '2':
+               all_cpus = true;
+               break;
+       case '-l':
+               local_flows = +shift(ARGV);
+               break;
        }
 }
 
@@ -46,9 +55,19 @@ function set_task_cpu(pid, cpu) {
                system(`taskset -p -c ${cpu} ${pid}`);
 }
 
+function cpu_mask(cpu)
+{
+       let mask;
+       if (cpu < 0)
+               mask = (1 << length(cpus)) - 1;
+       else
+               mask = (1 << int(cpu));
+       return sprintf("%x", mask);
+}
+
 function set_netdev_cpu(dev, cpu) {
        let queues = glob(`/sys/class/net/${dev}/queues/rx-*/rps_cpus`);
-       let val = sprintf("%x", (1 << int(cpu)));
+       let val = cpu_mask(cpu);
        if (disable)
                val = 0;
        for (let queue in queues) {
@@ -57,6 +76,13 @@ function set_netdev_cpu(dev, cpu) {
                if (!do_nothing)
                        writefile(queue, `${val}`);
        }
+       queues = glob(`/sys/class/net/${dev}/queues/rx-*/rps_flow_cnt`);
+       for (let queue in queues) {
+               if (debug || do_nothing)
+                       warn(`echo ${local_flows} > ${queue}\n`);
+               if (!do_nothing)
+                       writefile(queue, `${local_flows}`);
+       }
 }
 
 function task_device_match(name, device)
@@ -173,7 +199,12 @@ function assign_dev_cpu(dev) {
        }
 
        if (length(dev.netdev) > 0) {
-               let cpu = dev.rx_cpu = get_next_cpu(rx_weight, dev.napi_cpu);
+               let cpu;
+               if (all_cpus)
+                       cpu = -1;
+               else
+                       cpu = get_next_cpu(rx_weight, dev.napi_cpu);
+               dev.rx_cpu = cpu;
                for (let netdev in dev.netdev)
                        set_netdev_cpu(netdev, cpu);
        }