Kernel NET policy
Subsystems (19)
v3 cover Kernel NET policy
From: Kan Liang <[email protected]>
It is a big challenge to get good network performance. First, the network performance is not good with default system settings. Second, it is too difficult to do automatic tuning for all possible workloads, since workloads have different requirements. Some workloads may want high throughput. Some may need low latency. Last but not least, there are lots of manual configurations. Fine grained configuration is too difficult for users.
NET policy intends to simplify the network configuration and get a good network performance according to the hints(policy) which is applied by user. It provides some typical "policies" for user which can be set per-socket, per-task or per-device. The kernel will automatically figures out how to merge different requests to get good network performance.
NET policy is designed for multiqueue network devices. This implementation is only for Intel NICs using i40e driver. But the concepts and generic code should apply to other multiqueue NICs too.
NET policy is also a combination of generic policy manager code and some ethtool callbacks (per queue coalesce setting, flow classification rules) to configure the driver.
This series also supports CPU hotplug and device hotplug.
Here are some common questions about NET policy.
1. Why userspace tool cannot do the same thing?
A: Kernel is more suitable for NET policy.
- User space code would be far more complicated to get right and perform well . It always need to work with out of date state compared to the latest, because it cannot do any locking with the kernel state.
- User space code is less efficient than kernel code, because of the additional context switches needed.
- Kernel is in the right position to coordinate requests from multiple users.
2. Is NET policy looking for optimal settings?
A: No. The NET policy intends to get a good network performance according
to user's specific request. Our target for good performance is ~90% of the optimal settings.
3. How's the configuration impact the connection rates?
A: There are two places to acquire rtnl mutex to configure the device.
- One is to do device policy setting. It happens on initalization stage, hotplug or queue number changes. The device policy will be set to NET_POLICY_NONE. If so, it "falls back" to the system default way to direct the packets. It doesn't block the connection.
- The other is to set Rx network flow classification options or rules. It uses work queue to do asynchronized setting. It avoid destroying the connection rates.
4. About disabling IRQ balance?
A: Disabling IRQ balance is a common way (recommend way for some devices) to
tune network performance. NET policy provides an option for driver to choose to disable IRQ balance and set IRQ affinity.
Here are some key Interfaces/APIs for NET policy.
Interfaces which export to user space
/proc/net/netpolicy/$DEV/policy
User can set/get per device policy from /proc
/proc/$PID/net_policy
User can set/get per task policy from /proc prctl(PR_SET_NETPOLICY, POLICY_NAME, NULL, NULL, NULL) An alternative way to set/get per task policy is from prctl.
setsockopt(sockfd,SOL_SOCKET,SO_NETPOLICY,&policy,sizeof(int)) User can set/get per socket policy by setsockopt
New ndo opt
int (*ndo_netpolicy_init)(struct net_device *dev,
struct netpolicy_info *info);
Initialize device driver for NET policy
int (*ndo_get_irq_info)(struct net_device *dev,
struct netpolicy_dev_info *info);
Collect device information. Currently, only collecting IRQ informance should be enough.
int (*ndo_set_net_policy)(struct net_device *dev,
enum netpolicy_name name);
This interface is used to set device NET policy by name. It is device driver's responsibility to set driver specific configuration for the given policy.
NET policy subsystem APIs
netpolicy_register(struct netpolicy_instance *instance,
enum netpolicy_name policy)
netpolicy_unregister(struct netpolicy_instance *instance) Register/unregister per task/socket NET policy.
The socket/task can only be benefited when it register itself with specific policy. After registeration, an record will be created and inserted into a RCU hash table, which include all the NET policy related information for the socket/task.
netpolicy_pick_queue(struct netpolicy_instance *instance, bool is_rx); Find the proper queue according to policy for packet receiving and transmitting
netpolicy_set_rules(struct netpolicy_instance *instance); Configure Rx network flow classification rules
For using NET policy, the per-device policy must be set in advance. It will automatically configure the system and re-organize the resource of the system accordingly. For system configuration, in this series, it will disable irq balance, set device queue irq affinity, and modify interrupt moderation. For re-organizing the resource, current implementation forces that CPU and queue irq are 1:1 mapping. An 1:1 mapping group is also called NET policy object. For each device policy, it maintains a policy list. Once the device policy is applied, the objects will be insert and tracked in that device policy list. The policy list only be updated when CPU/device hotplug, queue number changes or device policy changes.
The user can use /proc, prctl and setsockopt to set per-task and per-socket NET policy. Once the policy is set, an related record will be inserted into RCU hash table. The record includes ptr, policy and NET policy object. The ptr is the pointer address of task/socket. The object will not be assigned until the first package receive/transmit. The object is picked by round-robin from object list. Once the object is determined, the following packets will be set to redirect to the queue(object).
The object can be shared. The per-task or per-socket policy can be inherited.
Now NET policy supports four per device policies and three per task/socket policies.
- BULK policy: This policy is designed for high throughput. It can be applied to either per device policy or per task/socket policy.
- CPU policy: This policy is designed for high throughput but lower CPU utilization (power saving). It can be applied to either per device policy or per task/socket policy.
- LATENCY policy: This policy is designed for low latency. It can be applied to either per device policy or per task/socket policy.
- MIX policy: This policy can only be applied to per device policy. This is designed for the case which miscellaneous types of workload running on the device.
Lots of tests are done for NET policy on platforms with Intel Xeon E5 V2 and XL710 40G NIC. The baseline test is with Linux 4.6.0 kernel. Netperf is used to evaluate the throughput and latency performance.
- "netperf -f m -t TCP_RR -H server_IP -c -C -l 60 -- -r buffersize -b burst -D" is used to evaluate throughput performance, which is called throughput-first workload.
- "netperf -t TCP_RR -H server_IP -c -C -l 60 -- -r buffersize" is used to evaluate latency performance, which is called latency-first workload.
- Different loads are also evaluated by running 1, 12, 24, 48 or 96 throughput-first workloads/latency-first workload simultaneously.
For "BULK" policy, the throughput performance is on average ~1.22X than baseline.
For "CPU" policy, the throughput performance is on average ~1.19X than baseline, and has lower CPU% (on average ~5% lower than "BULK" policy). For "LATENCY" policy, the latency is on average 49.8% less than the baseline. For "MIX" policy, mixed workloads performance is evaluated.
The mixed workloads are combination of throughput-first workload and latency-first workload. Five different types of combinations are evaluated (pure throughput-first workload, pure latency-first workloads,
2/3 throughput-first workload + 1/3 latency-first workloads,
1/3 throughput-first workload + 2/3 latency-first workloads and 1/2 throughput-first workload + 1/2 latency-first workloads).
For caculating the performance of mixed workloads, a weighted sum system is introduced.
Score = normalized_latency * Weight + normalized_throughput * (1 - Weight). If we assume that the user has an equal interest in latency and throughput performance, the Score for "MIX" policy is on average ~1.63X than baseline.
Changes since V2:
- Set default to n for NET policy subsystem
- Modify the queue selection algorism. The new algorism will consider CPU loads and ref number
- Extends the netpolicy to support tc bpf when selecting Tx queue
- Provides an option irq_affinity for driver to choose to disable IRQ balance and set IRQ affinity
- Make the netpolicy_sys_map_version per device not global
- Modify the changelog accordingly
Changes since V1:
- Using work queue to set Rx network flow classification rules and search available NET policy object asynchronously.
- Using RCU lock to replace read-write lock
- Redo performance test and update performance results.
- Some minor modification for codes and documents.
- Remove i40e related patches which will be submitted in separate thread.
Kan Liang (26):
net: introduce NET policy
net/netpolicy: init NET policy net/netpolicy: get device queue irq information net/netpolicy: get CPU information net/netpolicy: create CPU and queue mapping net/netpolicy: set and remove IRQ affinity net/netpolicy: enable and disable NET policy net/netpolicy: introduce NET policy object net/netpolicy: set NET policy by policy name net/netpolicy: add three new NET policies net/netpolicy: add MIX policy net/netpolicy: NET device hotplug net/netpolicy: support CPU hotplug net/netpolicy: handle channel changes net/netpolicy: implement netpolicy register net/netpolicy: introduce per socket netpolicy net/netpolicy: introduce netpolicy_pick_queue net/netpolicy: set tx queues according to policy net/netpolicy: tc bpf extension to pick Tx queue net/netpolicy: set Rx queues according to policy net/netpolicy: introduce per task net policy net/netpolicy: set per task policy by proc net/netpolicy: fast path for finding the queues net/netpolicy: optimize for queue pair net/netpolicy: limit the total record number Documentation/networking: Document NET policy
Documentation/networking/netpolicy.txt | 157 ++++ arch/alpha/include/uapi/asm/socket.h | 2 + arch/avr32/include/uapi/asm/socket.h | 2 + arch/frv/include/uapi/asm/socket.h | 2 + arch/ia64/include/uapi/asm/socket.h | 2 + arch/m32r/include/uapi/asm/socket.h | 2 + arch/mips/include/uapi/asm/socket.h | 2 + arch/mn10300/include/uapi/asm/socket.h | 2 + arch/parisc/include/uapi/asm/socket.h | 2 + arch/powerpc/include/uapi/asm/socket.h | 2 + arch/s390/include/uapi/asm/socket.h | 2 + arch/sparc/include/uapi/asm/socket.h | 2 + arch/xtensa/include/uapi/asm/socket.h | 2 + fs/proc/base.c | 64 ++ include/linux/init_task.h | 9 + include/linux/netdevice.h | 31 + include/linux/netpolicy.h | 177 ++++ include/linux/sched.h | 8 + include/net/net_namespace.h | 3 + include/net/request_sock.h | 4 +- include/net/sock.h | 28 + include/uapi/asm-generic/socket.h | 2 + include/uapi/linux/bpf.h | 8 + include/uapi/linux/prctl.h | 4 + kernel/exit.c | 4 + kernel/fork.c | 6 + kernel/sched/fair.c | 8 +- kernel/sys.c | 31 + net/Kconfig | 7 + net/core/Makefile | 1 + net/core/dev.c | 20 +- net/core/ethtool.c | 8 +- net/core/filter.c | 36 + net/core/netpolicy.c | 1571 ++++++++++++++++++++++++++++++++ net/core/sock.c | 36 + net/ipv4/af_inet.c | 71 ++ net/ipv4/udp.c | 4 + samples/bpf/Makefile | 1 + samples/bpf/bpf_helpers.h | 2 + 39 files changed, 2317 insertions(+), 8 deletions(-) create mode 100644 Documentation/networking/netpolicy.txt create mode 100644 include/linux/netpolicy.h create mode 100644 net/core/netpolicy.c
Signature · 1 line
2.5.5
RFC PATCH v3 1/26 net: introduce NET policy
From: Kan Liang <[email protected]>
This patch introduce NET policy subsystem. If proc is supported in the system, it creates netpolicy node in proc system.
--- include/linux/netdevice.h | 7 +++ include/net/net_namespace.h | 3 ++ net/Kconfig | 7 +++ net/core/Makefile | 1 + net/core/netpolicy.c | 128 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 net/core/netpolicy.c
include/linux/netdevice.h +7 -0
@@ -1618,6 +1618,8 @@ enum netdev_priv_flags {* switch driver and used to set the phys state of the * switch port. * +* @proc_dev: device node in proc to configure device net policy +* * FIXME: cleanup struct net_device such that network protocol info * moves out. */
@@ -1885,6 +1887,11 @@ struct net_device {struct lock_class_key *qdisc_tx_busylock;struct lock_class_key *qdisc_running_key;bool proto_down; +#ifdef CONFIG_NETPOLICY +#ifdef CONFIG_PROC_FS +struct proc_dir_entry *proc_dev; +#endif /* CONFIG_PROC_FS */ +#endif /* CONFIG_NETPOLICY */ };#define to_net_dev (d) container_of(d, struct net_device, dev)
include/net/net_namespace.h +3 -0
@@ -142,6 +142,9 @@ struct net {#endif struct sock *diag_nlsk;atomic_t fnhe_genid; +#ifdef CONFIG_NETPOLICY +struct proc_dir_entry *proc_netpolicy; +#endif /* CONFIG_NETPOLICY */ };#include <linux/seq_file_net.h>
net/Kconfig +7 -0
@@ -205,6 +205,13 @@ source "net/bridge/netfilter/Kconfig" endif +config NETPOLICY + depends on NET + bool "Net policy support" + default n + ---help--- + Net policy support + source "net/dccp/Kconfig" source "net/sctp/Kconfig" source "net/rds/Kconfig"
net/core/Makefile +1 -0
@@ -27,3 +27,4 @@ obj-$(CONFIG_LWTUNNEL) += lwtunnel.o obj-$(CONFIG_DST_CACHE) += dst_cache.o obj-$(CONFIG_HWBM) += hwbm.o obj-$(CONFIG_NET_DEVLINK) += devlink.o +obj-$(CONFIG_NETPOLICY) += netpolicy.o
net/core/netpolicy.c new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 2/26 net/netpolicy: init NET policy
In reply to [email protected]From: Kan Liang <[email protected]>
This patch tries to initialize NET policy for all the devices in the system. However, not all device drivers have NET policy support. For those drivers who does not have NET policy support, the node will not be showed in /proc/net/netpolicy/.
The device driver who has NET policy support must implement the interface ndo_netpolicy_init, which is used to do necessory initialization and collect information (E.g. supported policies) from driver.
The user can check /proc/net/netpolicy/ and /proc/net/netpolicy/$DEV/policy to know the available device and its supported policy.
np_lock is also introduced to protect the state of NET policy.
Device hotplug will be handled later in this series.
--- include/linux/netdevice.h | 12 +++++++ include/linux/netpolicy.h | 31 ++++++++++++++++ net/core/netpolicy.c | 91 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 include/linux/netpolicy.h
include/linux/netdevice.h +12 -0
@@ -53,6 +53,7 @@#include <uapi/linux/if_bonding.h> #include <uapi/linux/pkt_cls.h> #include <linux/hashtable.h> +#include <linux/netpolicy.h> struct netpoll_info ;struct device ;
@@ -1121,6 +1122,9 @@ struct netdev_xdp {* int (*ndo_xdp)(struct net_device *dev, struct netdev_xdp *xdp); * This function is used to set or query state related to XDP on the * netdevice. See definition of enum xdp_netdev_command for details. +* int(*ndo_netpolicy_init)(struct net_device *dev, +* struct netpolicy_info *info); +* This function is used to init and get supported policy. * */ struct net_device_ops {
@@ -1307,6 +1311,10 @@ struct net_device_ops { int needed_headroom);int (*ndo_xdp)(struct net_device *dev,struct netdev_xdp *xdp); +#ifdef CONFIG_NETPOLICY +int (*ndo_netpolicy_init)(struct net_device *dev, +struct netpolicy_info *info); +#endif /* CONFIG_NETPOLICY */ };/**
@@ -1619,6 +1627,8 @@ enum netdev_priv_flags {* switch port. * * @proc_dev: device node in proc to configure device net policy +* @netpolicy: NET policy related information of net device +* @np_lock: protect the state of NET policy * * FIXME: cleanup struct net_device such that network protocol info * moves out.
@@ -1891,6 +1901,8 @@ struct net_device {#ifdef CONFIG_PROC_FS struct proc_dir_entry *proc_dev;#endif /* CONFIG_PROC_FS */ +struct netpolicy_info *netpolicy; +spinlock_t np_lock;#endif /* CONFIG_NETPOLICY */ };#define to_net_dev (d) container_of(d, struct net_device, dev)
include/linux/netpolicy.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* netpolicy.h: Net policy support +* Copyright (c) 2016, Intel Corporation. +* Author: Kan Liang ([email protected]) +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +*/ +#ifndef __LINUX_NETPOLICY_H +#define __LINUX_NETPOLICY_H + +enum netpolicy_name { +NET_POLICY_NONE =0 , +NET_POLICY_MAX , +}; + +extern const char *policy_name[]; + +struct netpolicy_info { +enum netpolicy_name cur_policy; +unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )]; +}; + +#endif /*__LINUX_NETPOLICY_H*/
net/core/netpolicy.c +80 -11
127 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 3/26 net/netpolicy: get device queue irq information
In reply to [email protected]From: Kan Liang <[email protected]>
Net policy needs to know device information. Currently, it's enough to only get irq information of rx and tx queues.
This patch introduces ndo ops to do so, not ethtool ops. Because there are already several ways to get irq information in userspace. It's not necessory to extend the ethtool.
--- include/linux/netdevice.h | 5 +++++ include/linux/netpolicy.h | 7 +++++++ net/core/netpolicy.c | 14 ++++++++++++++ 3 files changed, 26 insertions(+)
include/linux/netdevice.h +5 -0
@@ -1125,6 +1125,9 @@ struct netdev_xdp {* int(*ndo_netpolicy_init)(struct net_device *dev, * struct netpolicy_info *info); * This function is used to init and get supported policy. +* int (*ndo_get_irq_info)(struct net_device *dev, +* struct netpolicy_dev_info *info); +* This function is used to get irq information of rx and tx queues * */ struct net_device_ops {
@@ -1314,6 +1317,8 @@ struct net_device_ops {#ifdef CONFIG_NETPOLICY int (*ndo_netpolicy_init)(struct net_device *dev,struct netpolicy_info *info); +int (*ndo_get_irq_info)(struct net_device *dev, +struct netpolicy_dev_info *info);#endif /* CONFIG_NETPOLICY */ };
include/linux/netpolicy.h +7 -0
@@ -23,6 +23,13 @@ enum netpolicy_name {extern const char *policy_name[]; +struct netpolicy_dev_info { +u32 rx_num; +u32 tx_num; +u32 *rx_irq; +u32 *tx_irq; +}; +struct netpolicy_info {enum netpolicy_name cur_policy;unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )];
net/core/netpolicy.c +14 -0
@@ -36,6 +36,20 @@#include <net/net_namespace.h> #include <net/rtnetlink.h> +static int netpolicy_get_dev_info (struct net_device *dev, +struct netpolicy_dev_info *d_info) +{ +if (!dev->netdev_ops->ndo_get_irq_info) +return -ENOTSUPP ; +return dev->netdev_ops->ndo_get_irq_info (dev, d_info); +} + +static void netpolicy_free_dev_info (struct netpolicy_dev_info *d_info) +{ +kfree (d_info->rx_irq); +kfree (d_info->tx_irq); +} +const char *policy_name[NET_POLICY_MAX ] = {"NONE" };
Signature · 1 line
2.5.5
Re: [RFC V3 PATCH 03/26] net/netpolicy: get device queue irq information
Hello.
Quoted 8 lines · On 09/12/2016 05:55 PM, [email protected] wrote:
From: Kan Liang <[email protected]>
Net policy needs to know device information. Currently, it's enough to only get irq information of rx and tx queues.
This patch introduces ndo ops to do so, not ethtool ops. Because there are already several ways to get irq information in userspace. It's not necessory to extend the ethtool.
Necessary.
Quoted 1 line
Signed-off-by Kan Liang <[email protected]>
[...]
MBR, Sergei
RE: [RFC V3 PATCH 03/26] net/netpolicy: get device queue irq information
Quoted 7 lines
Hello.
Quoted 2 lines · On 09/12/2016 05:55 PM, [email protected] wrote:
From: Kan Liang <[email protected]>
Quoted 8 lines
Quoted 6 lines
Net policy needs to know device information. Currently, it's enough to only get irq information of rx and tx queues.
This patch introduces ndo ops to do so, not ethtool ops. Because there are already several ways to get irq information in userspace. It's not necessory to extend the ethtool.
Necessary.
OK. I will extend the ethtool in next version.
Thanks,
Kan
Quoted 6 lines
Quoted 2 lines
Signed-off-by Kan Liang <[email protected]>[...]
MBR, Sergei
Re: [RFC V3 PATCH 03/26] net/netpolicy: get device queue irq information
Quoted 14 lines · Kan
Quoted 14 lines
Hello.
Quoted 8 lines · On 09/12/2016 05:55 PM, [email protected] wrote:
From: Kan Liang <[email protected]>
Net policy needs to know device information. Currently, it's enough to only get irq information of rx and tx queues.
This patch introduces ndo ops to do so, not ethtool ops. Because there are already several ways to get irq information in userspace. It's not necessory to extend the ethtool.
Quoted 6 lines
Quoted 1 line
Necessary.
OK. I will extend the ethtool in next version.
Thanks,
Kan
Kan, I don't think Sergei was saying you have to extend the ethtool. Your spelling of necessary was incorrect in your patch description.
Sergei, please feel free to tell me I am wrong if my assumption on that is incorrect.
- Alex
RE: [RFC V3 PATCH 03/26] net/netpolicy: get device queue irq information
Quoted 24 lines · Kan
Quoted 14 lines
Quoted 14 lines
Hello.
Quoted 8 lines · On 09/12/2016 05:55 PM, [email protected] wrote:
From: Kan Liang <[email protected]>
Net policy needs to know device information. Currently, it's enough to only get irq information of rx and tx queues.
This patch introduces ndo ops to do so, not ethtool ops. Because there are already several ways to get irq information in userspace. It's not necessory to extend the ethtool.
Quoted 6 lines
Quoted 1 line
Necessary.
OK. I will extend the ethtool in next version.
Thanks,
Kan
Quoted 7 lines
Kan, I don't think Sergei was saying you have to extend the ethtool. Your spelling of necessary was incorrect in your patch description.
Sergei, please feel free to tell me I am wrong if my assumption on that is incorrect.
- Alex
Oh, I see. Thanks Alex. :)
Kan
RFC PATCH v3 4/26 net/netpolicy: get CPU information
In reply to [email protected]From: Kan Liang <[email protected]>
Net policy also needs to know CPU information. Currently, online CPU count is enough.
--- net/core/netpolicy.c | 5 +++++ 1 file changed, 5 insertions(+)
net/core/netpolicy.c +5 -0
@@ -50,6 +50,11 @@ static void netpolicy_free_dev_info(struct netpolicy_dev_info *d_info)kfree (d_info->tx_irq); } +static u32 netpolicy_get_cpu_information (void ) +{ +return num_online_cpus (); +} +const char *policy_name[NET_POLICY_MAX ] = {"NONE" };
Signature · 1 line
2.5.5
RFC PATCH v3 5/26 net/netpolicy: create CPU and queue mapping
In reply to [email protected]From: Kan Liang <[email protected]>
Current implementation forces CPU and queue 1:1 mapping. This patch introduces the function netpolicy_update_sys_map to create this mapping. The result is stored in netpolicy_sys_info.
If the CPU count and queue count are different, the remaining CPUs/queues are not used for now.
CPU hotplug, device hotplug or ethtool may change the CPU count or queue count. For these cases, this function can also be called to reconstruct the mapping. These cases will be handled later in this series.
--- include/linux/netpolicy.h | 18 ++++++++++++ net/core/netpolicy.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)
include/linux/netpolicy.h +18 -0
@@ -30,9 +30,27 @@ struct netpolicy_dev_info { u32 *tx_irq; }; +struct netpolicy_sys_map { +u32 cpu; +u32 queue; +u32 irq; +}; + +struct netpolicy_sys_info { +/* +* Record the cpu and queue 1:1 mapping +*/ +u32 avail_rx_num; +struct netpolicy_sys_map *rx; +u32 avail_tx_num; +struct netpolicy_sys_map *tx; +}; +struct netpolicy_info {enum netpolicy_name cur_policy;unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )]; +/* cpu and queue mapping information */ +struct netpolicy_sys_info sys_info; };#endif /*__LINUX_NETPOLICY_H*/
net/core/netpolicy.c +74 -0
80 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 6/26 net/netpolicy: set and remove IRQ affinity
In reply to [email protected]From: Kan Liang <[email protected]>
This patches introduces functions to set and remove IRQ affinity according to cpu and queue mapping.
The functions will not record the previous affinity status. After a set/remove cycles, it will set the affinity on all online CPU with IRQ balance enabling.
--- net/core/netpolicy.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
net/core/netpolicy.c +33 -0
@@ -29,6 +29,7 @@#include <linux/kernel.h> #include <linux/errno.h> #include <linux/init.h> +#include <linux/irq.h> #include <linux/seq_file.h> #include <linux/proc_fs.h> #include <linux/uaccess.h>
@@ -129,6 +130,38 @@ err:return -ENOMEM ; } +static void netpolicy_clear_affinity (struct net_device *dev) +{ +struct netpolicy_sys_info *s_info = &dev->netpolicy->sys_info; +u32 i; + +for (i =0 ; i < s_info->avail_rx_num; i++) { +irq_clear_status_flags (s_info->rx[i].irq,IRQ_NO_BALANCING ); +irq_set_affinity_hint (s_info->rx[i].irq, cpu_online_mask); + } + +for (i =0 ; i < s_info->avail_tx_num; i++) { +irq_clear_status_flags (s_info->tx[i].irq,IRQ_NO_BALANCING ); +irq_set_affinity_hint (s_info->tx[i].irq, cpu_online_mask); + } +} + +static void netpolicy_set_affinity (struct net_device *dev) +{ +struct netpolicy_sys_info *s_info = &dev->netpolicy->sys_info; +u32 i; + +for (i =0 ; i < s_info->avail_rx_num; i++) { +irq_set_status_flags (s_info->rx[i].irq,IRQ_NO_BALANCING ); +irq_set_affinity_hint (s_info->rx[i].irq,cpumask_of (s_info->rx[i].cpu)); + } + +for (i =0 ; i < s_info->avail_tx_num; i++) { +irq_set_status_flags (s_info->tx[i].irq,IRQ_NO_BALANCING ); +irq_set_affinity_hint (s_info->tx[i].irq,cpumask_of (s_info->tx[i].cpu)); + } +} +const char *policy_name[NET_POLICY_MAX ] = {"NONE" };
Signature · 1 line
2.5.5
RFC PATCH v3 7/26 net/netpolicy: enable and disable NET policy
In reply to [email protected]From: Kan Liang <[email protected]>
This patch introduces functions to enable and disable NET policy.
For enabling, it collects device and CPU information and setup CPU/queue mapping. Also, for some drivers like i40e driver, it will get better performance if setting IRQ affinity. This patch provides an option irq_affinity for driver to set IRQ affinity in NET policy enabling.
For disabling, it removes the IRQ affinity (if set) and mapping information.
np_lock should protect the enable and disable state. It will be done later in this series.
--- include/linux/netpolicy.h | 1 + net/core/netpolicy.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
include/linux/netpolicy.h +1 -0
@@ -49,6 +49,7 @@ struct netpolicy_sys_info {struct netpolicy_info {enum netpolicy_name cur_policy;unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )]; +bool irq_affinity;/* cpu and queue mapping information */ struct netpolicy_sys_info sys_info; };
net/core/netpolicy.c +41 -0
47 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 8/26 net/netpolicy: introduce NET policy object
In reply to [email protected]From: Kan Liang <[email protected]>
This patch introduces the concept of NET policy object and policy object list.
The NET policy object is the instance of CPU/queue mapping. The object can be shared between different tasks/sockets. So besides CPU and queue information, the object also maintains a reference counter.
Each policy will have a dedicated object list. If the policy is set as device policy, all objects will be inserted into the related policy object list. The user will search and pickup the available objects from the list later.
The network performance for objects could be different because of the queue and CPU topology. To generate a proper object list, dev location, HT and CPU topology have to be considered. The high performance objects are in the front of the list.
The object lists will be regenerated if sys mapping changes or device net policy changes.
Lock np_ob_list_lock is used to protect the object list.
--- include/linux/netdevice.h | 2 + include/linux/netpolicy.h | 15 +++ net/core/netpolicy.c | 238 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 254 insertions(+), 1 deletion(-)
include/linux/netdevice.h +2 -0
@@ -1634,6 +1634,7 @@ enum netdev_priv_flags {* @proc_dev: device node in proc to configure device net policy * @netpolicy: NET policy related information of net device * @np_lock: protect the state of NET policy +* @np_ob_list_lock: protect the net policy object list * * FIXME: cleanup struct net_device such that network protocol info * moves out.
@@ -1908,6 +1909,7 @@ struct net_device {#endif /* CONFIG_PROC_FS */ struct netpolicy_info *netpolicy;spinlock_t np_lock; +spinlock_t np_ob_list_lock;#endif /* CONFIG_NETPOLICY */ };#define to_net_dev (d) container_of(d, struct net_device, dev)
include/linux/netpolicy.h +15 -0
@@ -21,6 +21,12 @@ enum netpolicy_name { NET_POLICY_MAX, }; +enum netpolicy_traffic { +NETPOLICY_RX =0 , +NETPOLICY_TX , +NETPOLICY_RXTX , +}; +extern const char *policy_name[];struct netpolicy_dev_info {
@@ -46,12 +52,21 @@ struct netpolicy_sys_info { struct netpolicy_sys_map *tx; }; +struct netpolicy_object { +struct list_head list; +u32 cpu; +u32 queue; +atomic_t refcnt; +}; +struct netpolicy_info {enum netpolicy_name cur_policy;unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )];bool irq_affinity;/* cpu and queue mapping information */ struct netpolicy_sys_info sys_info; +/* List of policy objects 0 rx 1 tx */ +struct list_head obj_list[NETPOLICY_RXTX ][NET_POLICY_MAX ]; };#endif /*__LINUX_NETPOLICY_H*/
net/core/netpolicy.c +237 -1
274 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 9/26 net/netpolicy: set NET policy by policy name
In reply to [email protected]From: Kan Liang <[email protected]>
User can write policy name to /proc/net/netpolicy/$DEV/policy to enable net policy for specific device.
When the policy is enabled, the subsystem automatically disables IRQ balance and set IRQ affinity. The object list is also generated accordingly.
It is device driver's responsibility to set driver specific configuration for the given policy.
np_lock will be used to protect the state.
--- include/linux/netdevice.h | 5 +++ include/linux/netpolicy.h | 1 + net/core/netpolicy.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+)
include/linux/netdevice.h +5 -0
@@ -1128,6 +1128,9 @@ struct netdev_xdp {* int (*ndo_get_irq_info)(struct net_device *dev, * struct netpolicy_dev_info *info); * This function is used to get irq information of rx and tx queues +* int (*ndo_set_net_policy)(struct net_device *dev, +* enum netpolicy_name name); +* This function is used to set per device net policy by name * */ struct net_device_ops {
@@ -1319,6 +1322,8 @@ struct net_device_ops { struct netpolicy_info *info);int (*ndo_get_irq_info)(struct net_device *dev,struct netpolicy_dev_info *info); +int (*ndo_set_net_policy)(struct net_device *dev, +enum netpolicy_name name);#endif /* CONFIG_NETPOLICY */ };
include/linux/netpolicy.h +1 -0
@@ -27,6 +27,7 @@ enum netpolicy_traffic { NETPOLICY_RXTX, }; +#define POLICY_NAME_LEN_MAX 64extern const char *policy_name[];struct netpolicy_dev_info {
net/core/netpolicy.c +95 -0
124 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 10/26 net/netpolicy: add three new NET policies
In reply to [email protected]From: Kan Liang <[email protected]>
Introduce three NET policies
CPU policy: configure for higher throughput and lower CPU% (power saving).
BULK policy: configure for highest throughput.
LATENCY policy: configure for lowest latency.
--- include/linux/netpolicy.h | 3 +++ net/core/netpolicy.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-)
include/linux/netpolicy.h +3 -0
@@ -18,6 +18,9 @@enum netpolicy_name {NET_POLICY_NONE =0 , +NET_POLICY_CPU , +NET_POLICY_BULK , +NET_POLICY_LATENCY ,NET_POLICY_MAX , };
net/core/netpolicy.c +4 -1
@@ -226,7 +226,10 @@ static int netpolicy_enable(struct net_device *dev) }const char *policy_name[NET_POLICY_MAX ] = { -"NONE" +"NONE" , +"CPU" , +"BULK" , +"LATENCY" }; static u32 cpu_to_queue(struct net_device *dev,
Signature · 1 line
2.5.5
RFC PATCH v3 11/26 net/netpolicy: add MIX policy
In reply to [email protected]From: Kan Liang <[email protected]>
MIX policy is combine of other policies. It allows different queue has different policy. If MIX policy is applied, /proc/net/netpolicy/$DEV/policy shows per queue policy.
Usually, the workloads requires either high throughput or low latency. So for current implementation, MIX policy is combine of LATENCY policy and BULK policy.
The workloads which requires high throughput are usually utilize more CPU resources compared to the workloads which requires low latency. This means that if there is an equal interest in latency and throughput performance, it is better to reserve more BULK queues than LATENCY queues. In this patch, MIX policy is forced to include 1/3 LATENCY policy queues and 2/3 BULK policy queues.
--- include/linux/netpolicy.h | 7 +++ net/core/netpolicy.c | 139 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 136 insertions(+), 10 deletions(-)
include/linux/netpolicy.h +7 -0
@@ -22,6 +22,12 @@ enum netpolicy_name {NET_POLICY_BULK ,NET_POLICY_LATENCY ,NET_POLICY_MAX , + +/* +* Mixture of the above policy +* Can only be set as global policy. +*/ + NET_POLICY_MIX, }; enum netpolicy_traffic {
@@ -67,6 +73,7 @@ struct netpolicy_info {enum netpolicy_name cur_policy;unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )];bool irq_affinity; +bool has_mix_policy;/* cpu and queue mapping information */ struct netpolicy_sys_info sys_info;/* List of policy objects 0 rx 1 tx */
net/core/netpolicy.c +129 -10
211 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 12/26 net/netpolicy: NET device hotplug
In reply to [email protected]From: Kan Liang <[email protected]>
Support NET device up/down/namechange in the NET policy code.
--- net/core/netpolicy.c | 66 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 8 deletions(-)
net/core/netpolicy.c +58 -8
@@ -688,6 +688,9 @@ static const struct file_operations proc_net_policy_operations = {static int netpolicy_proc_dev_init (struct net *net,struct net_device *dev) { +if (dev->proc_dev) +proc_remove (dev->proc_dev); + dev->proc_dev =proc_net_mkdir (net, dev->name, net->proc_netpolicy);if (!dev->proc_dev)return -ENOMEM ;
@@ -754,6 +757,19 @@ void uninit_netpolicy(struct net_device *dev)spin_unlock (&dev->np_lock); } +static void netpolicy_dev_init (struct net *net, +struct net_device *dev) +{ +if (!init_netpolicy (dev)) { +#ifdef CONFIG_PROC_FS +if (netpolicy_proc_dev_init (net, dev)) +uninit_netpolicy (dev); + else +#endif /* CONFIG_PROC_FS */ +pr_info ("NETPOLICY: Init net policy for %s\n" , dev->name); + } +} +static int __net_init netpolicy_net_init (struct net *net) {struct net_device *dev, *aux;
@@ -767,14 +783,7 @@ static int __net_init netpolicy_net_init(struct net *net)rtnl_lock ();for_each_netdev_safe (net, dev, aux) { -if (!init_netpolicy (dev)) { -#ifdef CONFIG_PROC_FS -if (netpolicy_proc_dev_init (net, dev)) -uninit_netpolicy (dev); - else -#endif /* CONFIG_PROC_FS */ -pr_info ("NETPOLICY: Init net policy for %s\n" , dev->name); - } +netpolicy_dev_init (net, dev); }rtnl_unlock ();
@@ -799,17 +808,58 @@ static struct pernet_operations netpolicy_net_ops = { .exit = netpolicy_net_exit, }; +static int netpolicy_notify (struct notifier_block *this, +unsigned long event, +void *ptr) +{ +struct net_device *dev =netdev_notifier_info_to_dev (ptr); + +switch (event) { +case NETDEV_CHANGENAME : +#ifdef CONFIG_PROC_FS +if (dev->proc_dev) { +proc_remove (dev->proc_dev); +if ((netpolicy_proc_dev_init (dev_net (dev), dev) <0 ) && + dev->proc_dev) { +proc_remove (dev->proc_dev); + dev->proc_dev =NULL ; + } + } +#endif +break ; +case NETDEV_UP : +netpolicy_dev_init (dev_net (dev), dev); +break ; +case NETDEV_GOING_DOWN : +uninit_netpolicy (dev); +#ifdef CONFIG_PROC_FS +proc_remove (dev->proc_dev); + dev->proc_dev =NULL ; +#endif +break ; + } + +return NOTIFY_OK ; +} + +static struct notifier_block netpolicy_dev_notf = { + .notifier_call = netpolicy_notify, +}; +static int __init netpolicy_init (void ) {int ret; ret =register_pernet_subsys (&netpolicy_net_ops); +if (!ret) +register_netdevice_notifier (&netpolicy_dev_notf);return ret; } static void __exit netpolicy_exit(void) { +unregister_netdevice_notifier (&netpolicy_dev_notf);unregister_pernet_subsys (&netpolicy_net_ops); }
Signature · 1 line
2.5.5
RFC PATCH v3 13/26 net/netpolicy: support CPU hotplug
In reply to [email protected]From: Kan Liang <[email protected]>
For CPU hotplug, the NET policy subsystem will rebuild the sys map and object list.
--- net/core/netpolicy.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
net/core/netpolicy.c +80 -0
@@ -38,6 +38,7 @@#include <net/rtnetlink.h> #include <linux/sort.h> #include <linux/ctype.h> +#include <linux/cpu.h> static int netpolicy_get_dev_info (struct net_device *dev,struct netpolicy_dev_info *d_info)
@@ -846,6 +847,77 @@ static struct notifier_block netpolicy_dev_notf = { .notifier_call = netpolicy_notify, }; +/** +* update_netpolicy_sys_map() - rebuild the sys map and object list +* +* This function go through all the available net policy supported device, +* and rebuild sys map and object list. +* +*/ +void update_netpolicy_sys_map (void ) +{ +struct net *net; +struct net_device *dev, *aux; +enum netpolicy_name cur_policy; + +for_each_net (net) { +for_each_netdev_safe (net, dev, aux) { +spin_lock (&dev->np_lock); +if (!dev->netpolicy) + goto unlock; + cur_policy = dev->netpolicy->cur_policy; +if (cur_policy ==NET_POLICY_NONE ) + goto unlock; + + dev->netpolicy->cur_policy =NET_POLICY_NONE ; + +/* rebuild everything */ +netpolicy_disable (dev); +netpolicy_enable (dev); +if (netpolicy_gen_obj_list (dev, cur_policy)) { +pr_warn ("NETPOLICY: Failed to generate netpolicy object list for dev %s\n" , + dev->name); +netpolicy_disable (dev); + goto unlock; + } +if (dev->netdev_ops->ndo_set_net_policy (dev, cur_policy)) { +pr_warn ("NETPOLICY: Failed to set netpolicy for dev %s\n" , + dev->name); +netpolicy_disable (dev); + goto unlock; + } + + dev->netpolicy->cur_policy = cur_policy; +unlock: +spin_unlock (&dev->np_lock); + } + } +} + +static int netpolicy_cpu_callback (struct notifier_block *nfb, +unsigned long action,void *hcpu) +{ +switch (action & ~CPU_TASKS_FROZEN ) { +case CPU_ONLINE : +rtnl_lock (); +update_netpolicy_sys_map (); +rtnl_unlock (); +break ; +case CPU_DYING : +rtnl_lock (); +update_netpolicy_sys_map (); +rtnl_unlock (); +break ; + } +return NOTIFY_OK ; +} + +static struct notifier_block netpolicy_cpu_notifier = { + &netpolicy_cpu_callback, +NULL , +0 +}; +static int __init netpolicy_init (void ) {int ret;
@@ -854,6 +926,10 @@ static int __init netpolicy_init(void)if (!ret)register_netdevice_notifier (&netpolicy_dev_notf); +cpu_notifier_register_begin (); +__register_cpu_notifier (&netpolicy_cpu_notifier); +cpu_notifier_register_done (); +return ret; }
@@ -861,6 +937,10 @@ static void __exit netpolicy_exit(void) {unregister_netdevice_notifier (&netpolicy_dev_notf);unregister_pernet_subsys (&netpolicy_net_ops); + +cpu_notifier_register_begin (); +__unregister_cpu_notifier (&netpolicy_cpu_notifier); +cpu_notifier_register_done (); }subsys_initcall (netpolicy_init);
Signature · 1 line
2.5.5
RFC PATCH v3 14/26 net/netpolicy: handle channel changes
In reply to [email protected]From: Kan Liang <[email protected]>
User can uses ethtool to set the channel number. This patch handles the channel changes by rebuilding the object list.
--- include/linux/netpolicy.h | 8 ++++++++ net/core/ethtool.c | 8 +++++++- net/core/netpolicy.c | 1 + 3 files changed, 16 insertions(+), 1 deletion(-)
include/linux/netpolicy.h +8 -0
@@ -80,4 +80,12 @@ struct netpolicy_info { struct list_head obj_list[NETPOLICY_RXTX][NET_POLICY_MAX]; }; +#ifdef CONFIG_NETPOLICY +extern void update_netpolicy_sys_map (void ); +#else +static inline void update_netpolicy_sys_map (void ) +{ +} +#endif +#endif /*__LINUX_NETPOLICY_H*/
net/core/ethtool.c +7 -1
@@ -1703,6 +1703,7 @@ static noinline_for_stack int ethtool_set_channels(struct net_device *dev, {struct ethtool_channels channels, max;u32 max_rx_in_use =0 ; +int ret;if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)return -EOPNOTSUPP ;
@@ -1726,7 +1727,12 @@ static noinline_for_stack int ethtool_set_channels(struct net_device *dev, (channels.combined_count + channels.rx_count) <= max_rx_in_use) return -EINVAL; -return dev->ethtool_ops->set_channels (dev, &channels); + ret = dev->ethtool_ops->set_channels (dev, &channels); +#ifdef CONFIG_NETPOLICY +if (!ret) +update_netpolicy_sys_map (); +#endif +return ret; }static int ethtool_get_pauseparam (struct net_device *dev,void __user *useraddr)
net/core/netpolicy.c +1 -0
@@ -893,6 +893,7 @@ unlock: } } } +EXPORT_SYMBOL (update_netpolicy_sys_map);static int netpolicy_cpu_callback (struct notifier_block *nfb,unsigned long action,void *hcpu)
Signature · 1 line
2.5.5
RFC PATCH v3 15/26 net/netpolicy: implement netpolicy register
In reply to [email protected]From: Kan Liang <[email protected]>
The socket/task can only be benefited when it register itself with specific policy. If it's the first time to register, a record will be created and inserted into RCU hash table. The record includes ptr, policy and object information. ptr is the socket/task's pointer which is used as key to search the record in hash table. Object will be assigned later.
This patch also introduces a new type NET_POLICY_INVALID, which indicates that the task/socket are not registered.
np_hashtable_lock is introduced to protect the hash table.
--- include/linux/netpolicy.h | 26 ++++++++ net/core/netpolicy.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+)
include/linux/netpolicy.h +26 -0
@@ -17,6 +17,7 @@#define __LINUX_NETPOLICY_Henum netpolicy_name { +NET_POLICY_INVALID =-1 ,NET_POLICY_NONE =0 ,NET_POLICY_CPU ,NET_POLICY_BULK ,
@@ -80,12 +81,37 @@ struct netpolicy_info {struct list_head obj_list[NETPOLICY_RXTX ][NET_POLICY_MAX ]; }; +struct netpolicy_instance { +struct net_device *dev; +enum netpolicy_name policy;/* required policy */ +void *ptr;/* pointers */ +}; + +/* check if policy is valid */ +static inline int is_net_policy_valid (enum netpolicy_name policy) +{ +return ((policy <NET_POLICY_MAX ) && (policy >NET_POLICY_INVALID )); +} +#ifdef CONFIG_NETPOLICY extern void update_netpolicy_sys_map (void ); +extern int netpolicy_register (struct netpolicy_instance *instance, +enum netpolicy_name policy); +extern void netpolicy_unregister (struct netpolicy_instance *instance);#else static inline void update_netpolicy_sys_map (void ) { } + +static inline int netpolicy_register (struct netpolicy_instance *instance, +enum netpolicy_name policy) +{return 0 ; +} + +static inline void netpolicy_unregister (struct netpolicy_instance *instance) +{ +} +#endif #endif /*__LINUX_NETPOLICY_H*/
net/core/netpolicy.c +153 -0
177 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 16/26 net/netpolicy: introduce per socket netpolicy
In reply to [email protected]From: Kan Liang <[email protected]>
The network socket is the most basic unit which control the network traffic. A socket option is needed for user to set their own policy on socket to improve the network performance.
There is no existing SOCKET options which can be reused. For socket options, SO_MARK or may be SO_PRIORITY is close to NET policy's requirement. But they can not be reused for NET policy. SO_MARK can be used for routing and packet filtering. But the NET policy doesn't intend to change the routing. It only redirects the packet to the specific device queue. Also, the target queue is assigned by NET policy subsystem at run time. It should not be set in advance. SO_PRIORITY can set protocol-defined priority for all packets on the socket. But the NET policies don't have priority yet.
This patch introduces a new socket option SO_NETPOLICY to set/get net policy for socket. so that the application can set its own policy on socket to improve the network performance.
Per socket net policy can also be inherited by new socket.
The usage of SO_NETPOLICY socket option is as below. setsockopt(sockfd,SOL_SOCKET,SO_NETPOLICY,&policy,sizeof(int)) getsockopt(sockfd,SOL_SOCKET,SO_NETPOLICY,&policy,sizeof(int)) The policy set by SO_NETPOLICY socket option must be valid and compatible with current device policy. Othrewise, it will error out. The socket policy will be set to NET_POLICY_INVALID.
--- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/avr32/include/uapi/asm/socket.h | 2 ++ arch/frv/include/uapi/asm/socket.h | 2 ++ arch/ia64/include/uapi/asm/socket.h | 2 ++ arch/m32r/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/mn10300/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/powerpc/include/uapi/asm/socket.h | 2 ++ arch/s390/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ arch/xtensa/include/uapi/asm/socket.h | 2 ++ include/net/request_sock.h | 4 +++- include/net/sock.h | 9 +++++++++ include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 28 ++++++++++++++++++++++++++++ 16 files changed, 66 insertions(+), 1 deletion(-)
arch/alpha/include/uapi/asm/socket.h +2 -0
@@ -97,4 +97,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _UAPI_ASM_SOCKET_H */
arch/avr32/include/uapi/asm/socket.h +2 -0
@@ -90,4 +90,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _UAPI__ASM_AVR32_SOCKET_H */
arch/frv/include/uapi/asm/socket.h +2 -0
@@ -90,5 +90,7 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_SOCKET_H */
arch/ia64/include/uapi/asm/socket.h +2 -0
@@ -99,4 +99,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_IA64_SOCKET_H */
arch/m32r/include/uapi/asm/socket.h +2 -0
@@ -90,4 +90,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_M32R_SOCKET_H */
arch/mips/include/uapi/asm/socket.h +2 -0
@@ -108,4 +108,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _UAPI_ASM_SOCKET_H */
arch/mn10300/include/uapi/asm/socket.h +2 -0
@@ -90,4 +90,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_SOCKET_H */
arch/parisc/include/uapi/asm/socket.h +2 -0
@@ -89,4 +89,6 @@#define SO_CNX_ADVICE 0x402E +#define SO_NETPOLICY 0x402F +#endif /* _UAPI_ASM_SOCKET_H */
arch/powerpc/include/uapi/asm/socket.h +2 -0
@@ -97,4 +97,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_POWERPC_SOCKET_H */
arch/s390/include/uapi/asm/socket.h +2 -0
@@ -96,4 +96,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _ASM_SOCKET_H */
arch/sparc/include/uapi/asm/socket.h +2 -0
@@ -86,6 +86,8 @@#define SO_CNX_ADVICE 0x0037 +#define SO_NETPOLICY 0x0038 +/* Security levels - as per NRL IPv6 - don't actually do anything */ #define SO_SECURITY_AUTHENTICATION 0x5001#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002
arch/xtensa/include/uapi/asm/socket.h +2 -0
@@ -101,4 +101,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* _XTENSA_SOCKET_H */
include/net/request_sock.h +3 -1
@@ -101,7 +101,9 @@ reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener,sk_tx_queue_clear (req_to_sk (req)); req->saved_syn =NULL ;atomic_set (&req->rsk_refcnt,0 ); - +#ifdef CONFIG_NETPOLICY +memcpy (&req_to_sk (req)->sk_netpolicy, &sk_listener->sk_netpolicy,sizeof (sk_listener->sk_netpolicy)); +#endif return req; }
include/net/sock.h +9 -0
@@ -70,6 +70,7 @@#include <net/checksum.h> #include <net/tcp_states.h> #include <linux/net_tstamp.h> +#include <linux/netpolicy.h> /* * This structure really needs to be cleaned up.
@@ -141,6 +142,7 @@ typedef __u64 __bitwise __addrpair;* %SO_OOBINLINE settings, %SO_TIMESTAMPING settings * @skc_incoming_cpu: record/match cpu processing incoming packets * @skc_refcnt: reference count +* @skc_netpolicy: per socket net policy * * This is the minimal network layer representation of sockets, the header * for struct sock and struct inet_timewait_sock.
@@ -200,6 +202,10 @@ struct sock_common {struct sock *skc_listener;/* request_sock */ struct inet_timewait_death_row *skc_tw_dr;/* inet_timewait_sock */ }; + +#ifdef CONFIG_NETPOLICY +struct netpolicy_instance skc_netpolicy; +#endif /* * fields between dontcopy_begin/dontcopy_end * are not copied in sock_copy()
@@ -339,6 +345,9 @@ struct sock {#define sk_incoming_cpu __sk_common.skc_incoming_cpu#define sk_flags __sk_common.skc_flags#define sk_rxhash __sk_common.skc_rxhash +#ifdef CONFIG_NETPOLICY +#define sk_netpolicy __sk_common.skc_netpolicy +#endif socket_lock_t sk_lock;struct sk_buff_head sk_receive_queue;
include/uapi/asm-generic/socket.h +2 -0
@@ -92,4 +92,6 @@#define SO_CNX_ADVICE 53 +#define SO_NETPOLICY 54 +#endif /* __ASM_GENERIC_SOCKET_H */
net/core/sock.c +28 -0
@@ -1003,6 +1003,12 @@ set_rcvbuf:if (val ==1 )dst_negative_advice (sk);break ; + +#ifdef CONFIG_NETPOLICY +case SO_NETPOLICY : + ret =netpolicy_register (&sk->sk_netpolicy, val); +break ; +#endif default : ret = -ENOPROTOOPT ;break ;
@@ -1263,6 +1269,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname, v.val = sk->sk_incoming_cpu;break ; +#ifdef CONFIG_NETPOLICY +case SO_NETPOLICY : + v.val = sk->sk_netpolicy.policy; +break ; +#endif default :/* We implement the SO_SNDLOWAT etc to not be settable * (1003.1g 7).
@@ -1402,6 +1413,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,sock_update_classid (&sk->sk_cgrp_data);sock_update_netprioidx (&sk->sk_cgrp_data); + +#ifdef CONFIG_NETPOLICY + sk->sk_netpolicy.dev =NULL ; + sk->sk_netpolicy.ptr = (void *)sk; + sk->sk_netpolicy.policy =NET_POLICY_INVALID ; +#endif }return sk;
@@ -1439,6 +1456,10 @@ static void __sk_destruct(struct rcu_head *head)put_pid (sk->sk_peer_pid);if (likely (sk->sk_net_refcnt))put_net (sock_net (sk)); +#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (sk->sk_netpolicy.policy)) +netpolicy_unregister (&sk->sk_netpolicy); +#endif sk_prot_free (sk->sk_prot_creator, sk); }
@@ -1575,6 +1596,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)if (sock_needs_netstamp (sk) && newsk->sk_flags &SK_FLAGS_TIMESTAMP )net_enable_timestamp (); + +#ifdef CONFIG_NETPOLICY + newsk->sk_netpolicy.ptr = (void *)newsk; +if (is_net_policy_valid (newsk->sk_netpolicy.policy)) +netpolicy_register (&newsk->sk_netpolicy, newsk->sk_netpolicy.policy); + +#endif } out:return newsk;
Signature · 1 line
2.5.5
RFC PATCH v3 17/26 net/netpolicy: introduce netpolicy_pick_queue
In reply to [email protected]From: Kan Liang <[email protected]>
To achieve better network performance, the key step is to distribute the packets to dedicated queues according to policy and system run time status.
This patch provides an interface which can return the proper dedicated queue for socket/task. Then the packets of the socket/task will be redirect to the dedicated queue for better network performance.
For selecting the proper queue, currently it checks the CPU loads and ref number. The object which has lowest CPU loads and ref number will be chosen.
The selected object will be stored in hashtable. So it does not need to go through the whole object list every time.
--- include/linux/netpolicy.h | 12 ++++ include/linux/sched.h | 3 + kernel/sched/fair.c | 8 +-- net/core/netpolicy.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 4 deletions(-)
include/linux/netpolicy.h +12 -0
@@ -85,8 +85,15 @@ struct netpolicy_instance {struct net_device *dev;enum netpolicy_name policy;/* required policy */ void *ptr;/* pointers */ +struct task_struct *task; }; +struct netpolicy_cpu_load { +unsigned long load; +struct netpolicy_object *obj; +}; +#define LOAD_TOLERANCE 5 +/* check if policy is valid */ static inline int is_net_policy_valid (enum netpolicy_name policy) {
@@ -98,6 +105,7 @@ extern void update_netpolicy_sys_map(void);extern int netpolicy_register (struct netpolicy_instance *instance,enum netpolicy_name policy);extern void netpolicy_unregister (struct netpolicy_instance *instance); +extern int netpolicy_pick_queue (struct netpolicy_instance *instance,bool is_rx);#else static inline void update_netpolicy_sys_map (void ) {
@@ -112,6 +120,10 @@ static inline void netpolicy_unregister(struct netpolicy_instance *instance) { } +static inline int netpolicy_pick_queue (struct netpolicy_instance *instance,bool is_rx) +{ +return 0 ; +}#endif #endif /*__LINUX_NETPOLICY_H*/
include/linux/sched.h +3 -0
@@ -3481,4 +3481,7 @@ void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,void cpufreq_remove_update_util_hook (int cpu);#endif /* CONFIG_CPU_FREQ */ +extern unsigned long weighted_cpuload (const int cpu); +extern unsigned long capacity_of (int cpu); +#endif
kernel/sched/fair.c +4 -4
@@ -1257,10 +1257,10 @@ bool should_numa_migrate_memory(struct task_struct *p, struct page * page,group_faults_cpu (ng, src_nid) *group_faults (p, dst_nid) *4 ; } -static unsigned long weighted_cpuload (const int cpu); +unsigned long weighted_cpuload (const int cpu);static unsigned long source_load (int cpu,int type);static unsigned long target_load (int cpu,int type); -static unsigned long capacity_of (int cpu); +unsigned long capacity_of (int cpu);static long effective_load (struct task_group *tg,int cpu,long wl,long wg);/* Cached statistics for all CPUs within a node */
@@ -4752,7 +4752,7 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load, }/* Used instead of source_load when we know the type == 0 */ -static unsigned long weighted_cpuload (const int cpu) +unsigned long weighted_cpuload (const int cpu) {return cfs_rq_runnable_load_avg (&cpu_rq (cpu)->cfs); }
@@ -4902,7 +4902,7 @@ static unsigned long target_load(int cpu, int type)return max (rq->cpu_load[type-1 ], total); } -static unsigned long capacity_of (int cpu) +unsigned long capacity_of (int cpu) {return cpu_rq (cpu)->cpu_capacity; }
net/core/netpolicy.c +179 -0
191 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC PATCH v3 18/26 net/netpolicy: set tx queues according to policy
In reply to [email protected]From: Kan Liang <[email protected]>
When the device tries to transmit a packet, netdev_pick_tx is called to find the available tx queues. If the net policy is applied, it picks up the assigned tx queue from net policy subsystem, and redirect the traffic to the assigned queue.
--- include/net/sock.h | 9 +++++++++ net/core/dev.c | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)
include/net/sock.h +9 -0
@@ -2280,4 +2280,13 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +/* Return netpolicy instance information from socket. */ +static inline struct netpolicy_instance *netpolicy_find_instance (struct sock *sk) +{ +#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (sk->sk_netpolicy.policy)) +return &sk->sk_netpolicy; +#endif +return NULL ; +}#endif /* _SOCK_H */
net/core/dev.c +18 -2
@@ -3266,6 +3266,7 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,struct sk_buff *skb, void *accel_priv) { + struct sock *sk = skb->sk;int queue_index =0 ;#ifdef CONFIG_XPS
@@ -3280,8 +3281,23 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,if (ops->ndo_select_queue) queue_index = ops->ndo_select_queue (dev, skb, accel_priv, __netdev_pick_tx); -else - queue_index =__netdev_pick_tx (dev, skb); +else { +#ifdef CONFIG_NETPOLICY +struct netpolicy_instance *instance; + + queue_index =-1 ; +if (dev->netpolicy && sk) { + instance =netpolicy_find_instance (sk); +if (instance) { +if (!instance->dev) + instance->dev = dev; + queue_index =netpolicy_pick_queue (instance, false); + } + } + if (queue_index < 0) +#endif + queue_index =__netdev_pick_tx (dev, skb); + }if (!accel_priv) queue_index =netdev_cap_txqueue (dev, queue_index);
Signature · 1 line
2.5.5
Re: [RFC V3 PATCH 18/26] net/netpolicy: set tx queues according to policy
Quoted 54 lines · On Mon, Sep 12, 2016 at 7:55 AM, <[email protected]> wrote:
From: Kan Liang <[email protected]>
When the device tries to transmit a packet,
netdev_pick_txis called to find the available tx queues. If the net policy is applied, it picks up the assigned tx queue from net policy subsystem, and redirect the traffic to the assigned queue.Signed-off-by Kan Liang <[email protected]>--- include/net/sock.h | 9 +++++++++ net/core/dev.c | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)include/net/sock.h +9
-0@@ -2280,4 +2280,13 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +/* Return netpolicy instance information from socket. */ +static inline struct netpolicy_instance *netpolicy_find_instance (struct sock *sk) +{ +#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (sk->sk_netpolicy.policy)) +return &sk->sk_netpolicy; +#endif +return NULL ; +}#endif /* _SOCK_H */ net/core/dev.c +6
-2@@ -3266,6 +3266,7 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,struct sk_buff *skb, void *accel_priv) { + struct sock *sk = skb->sk;int queue_index =0 ;#ifdef CONFIG_XPS @@ -3280,8 +3281,23 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,if (ops->ndo_select_queue) queue_index = ops->ndo_select_queue (dev, skb, accel_priv, __netdev_pick_tx); -else - queue_index =__netdev_pick_tx (dev, skb); +else { +#ifdef CONFIG_NETPOLICY +struct netpolicy_instance *instance; + + queue_index =-1 ;
net/core/dev.c struct netdev_queue *netdev_pick_tx(struct net_device *dev, · line 3298
Quoted 10 lines
net/core/dev.c +10
-0+if (dev->netpolicy && sk) { + instance =netpolicy_find_instance (sk); +if (instance) { +if (!instance->dev) + instance->dev = dev; + queue_index =netpolicy_pick_queue (instance, false); + } + } + if (queue_index < 0) +#endif
I doubt this produces the intended effect. Several drivers use ndo_select_queue (such as mlx4) where there might do something special for a few packets but end up called the default handler which __netdev_pick_tx for most packets. So in such cases the netpolicy path would be routinely bypassed. Maybe this code should be in __netdev_pick_tx.
Tom
Quoted 8 lines
net/core/dev.c +2
-1+ queue_index =__netdev_pick_tx (dev, skb); + }if (!accel_priv) queue_index =netdev_cap_txqueue (dev, queue_index); --2.5.5
RE: [RFC V3 PATCH 18/26] net/netpolicy: set tx queues according to policy
Quoted 86 lines
-----Original Message-----
From: Tom Herbert [mailto:[email protected]]
Sent: Monday, September 12, 2016 4:23 PM
To: Liang, Kan <[email protected]>
Cc: David S. Miller <[email protected]>; LKML <linux- [email protected]>; Linux Kernel Network Developers <[email protected]>; Kirsher, Jeffrey T <[email protected]>; Ingo Molnar <[email protected]>; [email protected]; Alexey Kuznetsov <[email protected]>; James Morris <[email protected]>; Hideaki YOSHIFUJI <[email protected]>; Patrick McHardy <[email protected]>; [email protected]; Kees Cook <[email protected]>; [email protected]; [email protected]; John Stultz <[email protected]>; Alexander Duyck <[email protected]>; Ben Hutchings <[email protected]>; David Decotigny <[email protected]>; Florian Westphal <[email protected]>; Alexander Duyck <[email protected]>; Daniel Borkmann <[email protected]>; [email protected]; Cong Wang <[email protected]>; Hannes Frederic Sowa <[email protected]>; Stephen Hemminger <[email protected]>; Alexei Starovoitov <[email protected]>; Brandeburg, Jesse <[email protected]>; Andi Kleen <[email protected]>
Subject: Re: [RFC V3 PATCH 18/26] net/netpolicy: set tx queues according to policynet/core/dev.c struct netdev_queue *netdev_pick_tx(struct
Quoted 33 lines · On Mon, Sep 12, 2016 at 7:55 AM, <[email protected]> wrote:
From: Kan Liang <[email protected]>
When the device tries to transmit a packet,
netdev_pick_txis called to find the available tx queues. If the net policy is applied, it picks up the assigned tx queue from net policy subsystem, and redirect the traffic to the assigned queue.Signed-off-by Kan Liang <[email protected]>--- include/net/sock.h | 9 +++++++++ net/core/dev.c | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)include/net/sock.h index renamed from include/net/sock.h +0
-0e1e9e3d..ca97f35 100644
include/net/sock.h +7
-0@@ -2280,4 +2280,13 @@ extern int sysctl_optmem_max; extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +/* Return netpolicy instance information from socket. */ static +inline struct netpolicy_instance *netpolicy_find_instance (struct sock +*sk) {#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (sk->sk_netpolicy.policy)) +return &sk->sk_netpolicy;#endif +return NULL ; +}#endif /* _SOCK_H */ net/core/dev.c index 34b5322..b9a8044 renamed from net/core/dev.c +0
-0100644
net/core/dev.c +0
-0@@ -3266,6 +3266,7 @@ struct netdev_queue *netdev_pick_tx(struct
net_device*dev,net/core/dev.c struct netdev_queue *netdev_pick_tx(struct
Quoted 7 lines
net/core/dev.c +1
-0struct sk_buff *skb, void *accel_priv) { + struct sock *sk = skb->sk;int queue_index =0 ;#ifdef CONFIG_XPS @@ -3280,8 +3281,23 @@ struct netdev_queue *netdev_pick_tx(struct
net_device*dev,Quoted 17 lines
net/core/dev.c +12
-2if (ops->ndo_select_queue) queue_index = ops->ndo_select_queue (dev, skb, accel_priv, __netdev_pick_tx); -else - queue_index =__netdev_pick_tx (dev, skb); +else { +#ifdef CONFIG_NETPOLICY +struct netpolicy_instance *instance; + + queue_index =-1 ; +if (dev->netpolicy && sk) { + instance =netpolicy_find_instance (sk); +if (instance) { +if (!instance->dev) + instance->dev = dev; + queue_index =netpolicy_pick_queue (instance, false); + }
Quoted 8 lines
net/core/dev.c struct netdev_queue *netdev_pick_tx(struct · line 3297
Quoted 2 lines
net/core/dev.c +2
-0+ } + if (queue_index < 0) #endifI doubt this produces the intended effect. Several drivers use
ndo_select_queue(such as mlx4) where there might do something special for a few packets but end up called the default handler which__netdev_pick_txfor most packets. So in such cases the netpolicy path would be routinely bypassed. Maybe this code should be in__netdev_pick_tx.
I will move the code to __netdev_pick_tx in next version.
Thanks,
Kan
Quoted 12 lines
Tom
Quoted 9 lines
net/core/dev.c +2
-0+ queue_index =__netdev_pick_tx (dev, skb); + }if (!accel_priv) queue_index = netdev_cap_txqueue(dev,
queue_index);
--
2.5.5
RFC PATCH v3 19/26 net/netpolicy: tc bpf extension to pick Tx queue
In reply to [email protected]From: Kan Liang <[email protected]>
This patch extends the netpolicy to support tc bpf when selecting Tx queue. It implements a bpf classifier for clsact qdisc. The classifier will pick up the proper queue from net policy subsystem. This queue selection from tc is not compatible with XPS. So XPS will be invalid.
Currently, tc bpf extension only supports the queue selection on egress. To enable the extension, the following command must be applied.
# ./tc qdisc add dev $DEVNAME clsact # ./tc filter add dev $DEVNAME egress bpf obj netpolicy_kern.o
--- include/uapi/linux/bpf.h | 8 ++++++++ net/core/dev.c | 4 ++-- net/core/filter.c | 36 ++++++++++++++++++++++++++++++++++++ samples/bpf/Makefile | 1 + samples/bpf/bpf_helpers.h | 2 ++ 5 files changed, 49 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +8 -0
@@ -398,6 +398,14 @@ enum bpf_func_id {*/ BPF_FUNC_skb_change_tail, +/** +* bpf_netpolicy(skb) +* Netpolicy tc extension. Search for proper Tx queue +* @skb: pointer to skb +* Return: 0 on success or negative error +*/ + BPF_FUNC_netpolicy, + __BPF_FUNC_MAX_ID, };
net/core/dev.c +2 -2
@@ -3285,8 +3285,8 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,#ifdef CONFIG_NETPOLICY struct netpolicy_instance *instance; - queue_index =-1 ; -if (dev->netpolicy && sk) { + queue_index =sk_tx_queue_get (sk); +if ((queue_index <0 ) && dev->netpolicy && sk) { instance =netpolicy_find_instance (sk);if (instance) {if (!instance->dev)
net/core/filter.c +36 -0
@@ -2351,6 +2351,38 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { .arg3_type = ARG_CONST_STACK_SIZE, }; +#ifdef CONFIG_NETPOLICY +static u64 bpf_netpolicy (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) +{ +struct sk_buff *skb = (struct sk_buff *) (unsigned long ) r1; +struct netpolicy_instance *instance; +struct net_device *dev = skb->dev; +struct sock *sk = skb->sk; +int queue_index; + +if (dev->netpolicy && sk) { + instance =netpolicy_find_instance (sk); +if (instance) { +if (!instance->dev) + instance->dev = dev; + queue_index =netpolicy_pick_queue (instance, false); +if ((queue_index >=0 ) &&sk_fullsock (sk) && +rcu_access_pointer (sk->sk_dst_cache)) +sk_tx_queue_set (sk, queue_index); + } + } + +return 0 ; +} + +static const struct bpf_func_proto bpf_netpolicy_proto = { + .func = bpf_netpolicy, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; +#endif +static const struct bpf_func_proto *bpf_get_skb_set_tunnel_proto (enum bpf_func_id which) {
@@ -2515,6 +2547,10 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)return &bpf_get_smp_processor_id_proto;case BPF_FUNC_skb_under_cgroup:return &bpf_skb_under_cgroup_proto; +#ifdef CONFIG_NETPOLICY +case BPF_FUNC_netpolicy: +return &bpf_netpolicy_proto; +#endif default :return sk_filter_func_proto (func_id); }
samples/bpf/Makefile +1 -0
@@ -85,6 +85,7 @@ always += xdp2_kern.o always += test_current_task_under_cgroup_kern.o always += trace_event_kern.o always += sampleip_kern.o +always += netpolicy_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
samples/bpf/bpf_helpers.h +2 -0
@@ -88,6 +88,8 @@ static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flag (void *) BPF_FUNC_l4_csum_replace;static int (*bpf_skb_under_cgroup)(void *ctx,void *map,int index) = (void *) BPF_FUNC_skb_under_cgroup; +static int (*bpf_netpolicy)(void *ctx) = + (void *) BPF_FUNC_netpolicy;#if defined(__x86_64__)
Signature · 1 line
2.5.5
RFC PATCH v3 20/26 net/netpolicy: set Rx queues according to policy
In reply to [email protected]From: Kan Liang <[email protected]>
For setting Rx queues, this patch configure Rx network flow classification rules to redirect the packets to the assigned queue.
Since we may not get all the information required for rule until the first packet arrived, it will add the rule after recvmsg. Also, to avoid destroying the connection rates, the configuration will be done asynchronized by work queue. So the first several packets may not use the assigned queue.
The dev information will be discarded in udp_queue_rcv_skb, so we record it in netpolicy struct in advance.
This patch only support INET tcp4 and udp4. It can be extend to other socket type and V6 later shortly.
For each sk, it only supports one rule. If the port/address changed, the previos rule will be replaced.
--- include/linux/netpolicy.h | 30 +++++++++++ net/core/netpolicy.c | 132 +++++++++++++++++++++++++++++++++++++++++++++- net/ipv4/af_inet.c | 71 +++++++++++++++++++++++++ net/ipv4/udp.c | 4 ++ 4 files changed, 236 insertions(+), 1 deletion(-)
include/linux/netpolicy.h +30 -0
@@ -37,6 +37,8 @@ enum netpolicy_traffic { NETPOLICY_RXTX, }; +#define NETPOLICY_INVALID_QUEUE -1 +#define NETPOLICY_INVALID_LOC NETPOLICY_INVALID_QUEUE#define POLICY_NAME_LEN_MAX 64extern const char *policy_name[];
@@ -81,11 +83,34 @@ struct netpolicy_info {struct list_head obj_list[NETPOLICY_RXTX ][NET_POLICY_MAX ]; }; +struct netpolicy_tcpudpip4_spec { +/* source and Destination host and port */ +__be32 ip4src; +__be32 ip4dst; +__be16 psrc; +__be16 pdst; +}; + +union netpolicy_flow_union { +struct netpolicy_tcpudpip4_spec tcp_udp_ip4_spec; +}; + +struct netpolicy_flow_spec { +__u32 flow_type; +union netpolicy_flow_union spec; +}; +struct netpolicy_instance {struct net_device *dev;enum netpolicy_name policy;/* required policy */ void *ptr;/* pointers */ struct task_struct *task; +int location;/* rule location */ +atomic_t rule_queue;/* queue set by rule */ +struct work_struct fc_wk;/* flow classification work */ +atomic_t fc_wk_cnt;/* flow classification work number */ +struct netpolicy_flow_spec flow;/* flow information */ + };struct netpolicy_cpu_load {
@@ -106,6 +131,7 @@ extern int netpolicy_register(struct netpolicy_instance *instance, enum netpolicy_name policy);extern void netpolicy_unregister (struct netpolicy_instance *instance);extern int netpolicy_pick_queue (struct netpolicy_instance *instance,bool is_rx); +extern void netpolicy_set_rules (struct netpolicy_instance *instance);#else static inline void update_netpolicy_sys_map (void ) {
@@ -124,6 +150,10 @@ static inline int netpolicy_pick_queue(struct netpolicy_instance *instance, bool {return 0 ; } + +static inline void netpolicy_set_rules (struct netpolicy_instance *instance) +{ +}#endif #endif /*__LINUX_NETPOLICY_H*/
net/core/netpolicy.c +131 -1
175 lines not on this page · raw message
net/ipv4/af_inet.c +71 -0
83 lines not on this page · raw message
net/ipv4/udp.c +4 -0
@@ -1786,6 +1786,10 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,if (sk) {int ret; +#ifdef CONFIG_NETPOLICY +/* Record dev info before it's discarded in udp_queue_rcv_skb */ + sk->sk_netpolicy.dev = skb->dev; +#endif if (inet_get_convert_csum (sk) && uh->check && !IS_UDPLITE (sk))skb_checksum_try_convert (skb,IPPROTO_UDP , uh->check, inet_compute_pseudo);
Signature · 1 line
2.5.5
RFC PATCH v3 21/26 net/netpolicy: introduce per task net policy
In reply to [email protected]From: Kan Liang <[email protected]>
Usually, application as a whole has specific requirement. Applying the net policy to all sockets one by one in the application is too complex. This patch introduces per task net policy to address this case. Once the per task net policy is applied, all the sockets in the application will apply the same net policy. Also, per task net policy can be inherited by all children.
The usage of PR_SET_NETPOLICY option is as below. prctl(PR_SET_NETPOLICY, POLICY_NAME, NULL, NULL, NULL).
It applies per task policy. The policy name must be valid and compatible with current device policy. Othrewise, it will error out. The task policy will be set to NET_POLICY_INVALID.
--- include/linux/init_task.h | 9 +++++++++ include/linux/sched.h | 5 +++++ include/net/sock.h | 12 +++++++++++- include/uapi/linux/prctl.h | 4 ++++ kernel/exit.c | 4 ++++ kernel/fork.c | 6 ++++++ kernel/sys.c | 31 +++++++++++++++++++++++++++++++ net/core/netpolicy.c | 35 +++++++++++++++++++++++++++++++++++ net/core/sock.c | 10 +++++++++- net/ipv4/af_inet.c | 7 +++++-- 10 files changed, 119 insertions(+), 4 deletions(-)
include/linux/init_task.h +9 -0
@@ -183,6 +183,14 @@ extern struct task_group root_task_group;# define INIT_KASAN (tsk)#endif +#ifdef CONFIG_NETPOLICY +#define INIT_NETPOLICY (tsk) \ + .task_netpolicy.policy = NET_POLICY_INVALID, \ + .task_netpolicy.dev = NULL, \ + .task_netpolicy.ptr = (void *)&tsk, +#else +#define INIT_NETPOLICY (tsk) +#endif /* * INIT_TASK is used to set up the first task table, touch at * your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -260,6 +268,7 @@ extern struct task_group root_task_group; INIT_VTIME(tsk) \ INIT_NUMA_BALANCING(tsk) \ INIT_KASAN(tsk) \ + INIT_NETPOLICY(tsk) \ }
include/linux/sched.h +5 -0
@@ -62,6 +62,8 @@ struct sched_param {#include <asm/processor.h> +#include <linux/netpolicy.h> +#define SCHED_ATTR_SIZE_VER0 48/* sizeof first published struct */ /*
@@ -1923,6 +1925,9 @@ struct task_struct {#ifdef CONFIG_MMU struct task_struct *oom_reaper_list;#endif +#ifdef CONFIG_NETPOLICY +struct netpolicy_instance task_netpolicy; +#endif /* CPU-specific state of this task */ struct thread_struct thread;/*
include/net/sock.h +11 -1
@@ -1484,6 +1484,7 @@ void sock_edemux(struct sk_buff *skb);#define sock_edemux (skb) sock_efree(skb)#endif +void sock_setnetpolicy (struct socket *sock);int sock_setsockopt (struct socket *sock,int level,int op,char __user *optval,unsigned int optlen);
@@ -2280,10 +2281,19 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; -/* Return netpolicy instance information from socket. */ +/* Return netpolicy instance information from either task or socket. +* If both task and socket have netpolicy instance information, +* using task's and unregistering socket's. Because task policy is +* dominant policy +*/ static inline struct netpolicy_instance *netpolicy_find_instance (struct sock *sk) {#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (current->task_netpolicy.policy)) { +if (is_net_policy_valid (sk->sk_netpolicy.policy)) +netpolicy_unregister (&sk->sk_netpolicy); +return ¤t->task_netpolicy; + }if (is_net_policy_valid (sk->sk_netpolicy.policy))return &sk->sk_netpolicy;#endif
include/uapi/linux/prctl.h +4 -0
@@ -197,4 +197,8 @@ struct prctl_mm_map {# define PR_CAP_AMBIENT_LOWER 3# define PR_CAP_AMBIENT_CLEAR_ALL 4 +/* Control net policy */ +#define PR_SET_NETPOLICY 48 +#define PR_GET_NETPOLICY 49 +#endif /* _LINUX_PRCTL_H */
kernel/exit.c +4 -0
@@ -858,6 +858,10 @@ void do_exit(long code)if (unlikely (current->pi_state_cache))kfree (current->pi_state_cache);#endif +#ifdef CONFIG_NETPOLICY +if (is_net_policy_valid (current->task_netpolicy.policy)) +netpolicy_unregister (¤t->task_netpolicy); +#endif /* * Make sure we are holding no locks: */
kernel/fork.c +6 -0
@@ -1451,6 +1451,12 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->sequential_io_avg =0 ;#endif +#ifdef CONFIG_NETPOLICY + p->task_netpolicy.ptr = (void *)p; +if (is_net_policy_valid (p->task_netpolicy.policy)) +netpolicy_register (&p->task_netpolicy, p->task_netpolicy.policy); +#endif +/* Perform scheduler related setup. Assign this task to a CPU. */ retval =sched_fork (clone_flags, p);if (retval)
kernel/sys.c +31 -0
@@ -2072,6 +2072,31 @@ static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr) }#endif +#ifdef CONFIG_NETPOLICY +static int prctl_set_netpolicy (struct task_struct *me,int policy) +{ +return netpolicy_register (&me->task_netpolicy, policy); +} + +static int prctl_get_netpolicy (struct task_struct *me,unsigned long adr) +{ +return put_user (me->task_netpolicy.policy, (int __user *)adr); +} + +#else /* CONFIG_NETPOLICY */ + +static int prctl_set_netpolicy (struct task_struct *me,int policy) +{ +return -EINVAL ; +} + +static int prctl_get_netpolicy (struct task_struct *me,unsigned long adr) +{ +return -EINVAL ; +} + +#endif /* CONFIG_NETPOLICY */ + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) {
@@ -2270,6 +2295,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,case PR_GET_FP_MODE : error =GET_FP_MODE (me);break ; +case PR_SET_NETPOLICY : + error =prctl_set_netpolicy (me, arg2); +break ; +case PR_GET_NETPOLICY : + error =prctl_get_netpolicy (me, arg2); +break ;default : error = -EINVAL ;break ;
net/core/netpolicy.c +35 -0
@@ -24,6 +24,35 @@* is too difficult for users. * So, it is a big challenge to get good network performance. * +* NET policy supports four policies per device, and three policies per task +* and per socket. For using NET policy, the device policy must be set in +* advance. The task policy or socket policy must be compatible with device +* policy. +* +* BULK policy This policy is designed for high throughput. It can be +* applied to either device policy or task/socket policy. +* If it is applied to device policy, the only compatible +* task/socket policy is BULK policy itself. +* CPU policy This policy is designed for high throughput and lower +* CPU utilization. It can be applied to either device +* policy or task/socket policy. If it is applied to +* device policy, the only compatible task/socket policy +* is CPU policy itself. +* LATENCY policy This policy is designed for low latency. It can be +* applied to either device policy or task/socket policy. +* If it is applied to device policy, the only compatible +* task/socket policy is LATENCY policy itself. +* MIX policy This policy can only be applied to device policy. It +* is compatible with BULK and LATENCY policy. This +* policy is designed for the case which miscellaneous +* types of workload running on the device. +* +* The device policy changes the system configuration and reorganize the +* resource on the device, but it does not change the packets behavior. +* The task policy and socket policy redirect the packets to get good +* performance. If both task policy and socket policy are set in the same +* task, task policy will be applied. The task policy can also be inherited by +* children. */ #include <linux/module.h> #include <linux/kernel.h>
@@ -444,6 +473,12 @@ static inline bool policy_validate(struct netpolicy_instance *instance) policy_name[instance->policy]);return false; } + +/* task policy is dominant policy */ +if (is_net_policy_valid (current->task_netpolicy.policy) && + (current->task_netpolicy.policy != instance->policy)) +return false; +return true; }
net/core/sock.c +9 -1
@@ -1006,7 +1006,13 @@ set_rcvbuf:#ifdef CONFIG_NETPOLICY case SO_NETPOLICY : - ret =netpolicy_register (&sk->sk_netpolicy, val); +if (is_net_policy_valid (current->task_netpolicy.policy) && + (current->task_netpolicy.policy != val)) { +printk_ratelimited (KERN_WARNING "NETPOLICY: new policy is not compatible with task netpolicy\n" ); + ret = -EINVAL ; + }else { + ret =netpolicy_register (&sk->sk_netpolicy, val); + }break ;#endif default :
@@ -1599,6 +1605,8 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)#ifdef CONFIG_NETPOLICY newsk->sk_netpolicy.ptr = (void *)newsk; +if (is_net_policy_valid (current->task_netpolicy.policy)) + newsk->sk_netpolicy.policy =NET_POLICY_INVALID ;if (is_net_policy_valid (newsk->sk_netpolicy.policy))netpolicy_register (&newsk->sk_netpolicy, newsk->sk_netpolicy.policy);
net/ipv4/af_inet.c +5 -2
@@ -766,8 +766,11 @@ static void sock_netpolicy_manage_flow(struct sock *sk, struct msghdr *msg)if (!instance)return ; -if (!instance->dev) -return ; +if (!instance->dev) { +if (!sk->sk_netpolicy.dev) +return ; + instance->dev = sk->sk_netpolicy.dev; + } flow = &instance->flow;/* TODO: need to change here and add more protocol support */
Signature · 1 line
2.5.5
RFC PATCH v3 22/26 net/netpolicy: set per task policy by proc
In reply to [email protected]From: Kan Liang <[email protected]>
Users may not want to change the source code to add per task net polic support. Or they may want to change a running task's net policy. prctl does not work for both cases.
This patch adds an interface in /proc, which can be used to set and retrieve policy of already running tasks. User can write the policy name into /proc/$PID/net_policy to set per task net policy.
--- fs/proc/base.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
fs/proc/base.c +64 -0
@@ -91,6 +91,8 @@#include <asm/hardwall.h> #endif #include <trace/events/oom.h> +#include <linux/netpolicy.h> +#include <linux/ctype.h> #include "internal.h" #include "fd.h"
@@ -2811,6 +2813,65 @@ static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,return err; } +#ifdef CONFIG_NETPOLICY +static int proc_net_policy_show (struct seq_file *m,void *v) +{ +struct inode *inode = m->private; +struct task_struct *task =get_proc_task (inode); + +if (is_net_policy_valid (task->task_netpolicy.policy)) +seq_printf (m,"%s\n" , policy_name[task->task_netpolicy.policy]); + +return 0 ; +} + +static int proc_net_policy_open (struct inode *inode,struct file *file) +{ +return single_open (file, proc_net_policy_show, inode); +} + +static ssize_t proc_net_policy_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct inode *inode =file_inode (file); +struct task_struct *task =get_proc_task (inode); +char name[POLICY_NAME_LEN_MAX ]; +int i, ret; + +if (count >=POLICY_NAME_LEN_MAX ) +return -EINVAL ; + +if (copy_from_user (name, buf, count)) +return -EINVAL ; + +for (i =0 ; i < count -1 ; i++) + name[i] =toupper (name[i]); + name[POLICY_NAME_LEN_MAX -1 ] =0 ; + +for (i =0 ; i <NET_POLICY_MAX ; i++) { +if (!strncmp (name, policy_name[i],strlen (policy_name[i]))) { + ret =netpolicy_register (&task->task_netpolicy, i); +if (ret) +return ret; +break ; + } + } + +if (i ==NET_POLICY_MAX ) +return -EINVAL ; + +return count; +} + +static const struct file_operations proc_net_policy_operations = { + .open = proc_net_policy_open, + .write = proc_net_policy_write, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; +#endif /* CONFIG_NETPOLICY */ +/* * Thread groups */
@@ -2910,6 +2971,9 @@ static const struct pid_entry tgid_base_stuff[] = {REG ("timers" ,S_IRUGO , proc_timers_operations), #endifREG ("timerslack_ns" ,S_IRUGO |S_IWUGO , proc_pid_set_timerslack_ns_operations), +#if IS_ENABLED(CONFIG_NETPOLICY) + REG("net_policy", S_IRUSR|S_IWUSR, proc_net_policy_operations), +#endif };static int proc_tgid_base_readdir (struct file *file,struct dir_context *ctx)
Signature · 1 line
2.5.5
Re: [RFC V3 PATCH 22/26] net/netpolicy: set per task policy by proc
Quoted 3 lines · On 09/12/2016 05:55 PM, [email protected] wrote:
From: Kan Liang <[email protected]>
Users may not want to change the source code to add per task net polic
Policy?
Quoted 8 lines
support. Or they may want to change a running task's net policy. prctl does not work for both cases.
This patch adds an interface in /proc, which can be used to set and retrieve policy of already running tasks. User can write the policy name into
/proc/$PID/net_policyto set per task net policy.Signed-off-by Kan Liang <[email protected]>
[...]
MBR, Sergei
RFC PATCH v3 23/26 net/netpolicy: fast path for finding the queues
In reply to [email protected]From: Kan Liang <[email protected]>
Current implementation searches the hash table to get assigned object for each transmit/receive packet. It's not necessory, because the assigned object usually remain unchanged. This patch store the assigned queue to speed up the searching process.
But under certain situations, the assigned objects has to be changed, especially when system cpu and queue mapping changed, such as CPU hotplug, device hotplug, queue number changes and so on. In this patch, the netpolicy_sys_map_version is used to track the system cpu and queue mapping changes. If the netpolicy_sys_map_version doesn't match with the instance's version, the stored queue will be dropped. The netpolicy_sys_map_version is protected by RCU lock.
Also, to reduce the overhead, this patch asynchronously find the available object by work queue. So the first several packets may not be benefited.
--- include/linux/netpolicy.h | 23 +++++++--- net/core/netpolicy.c | 106 +++++++++++++++++++++++++++++++++++++++++++++- net/ipv4/af_inet.c | 7 +-- 3 files changed, 125 insertions(+), 11 deletions(-)
include/linux/netpolicy.h +18 -5
@@ -55,14 +55,20 @@ struct netpolicy_sys_map {u32 irq; }; +struct netpolicy_sys_map_version { +struct rcu_head rcu; +int major; +}; +struct netpolicy_sys_info {/* * Record the cpu and queue 1:1 mapping */ -u32 avail_rx_num; -struct netpolicy_sys_map *rx; -u32 avail_tx_num; -struct netpolicy_sys_map *tx; +u32 avail_rx_num; +struct netpolicy_sys_map *rx; +u32 avail_tx_num; +struct netpolicy_sys_map *tx; +struct netpolicy_sys_map_version __rcu *version; };struct netpolicy_object {
@@ -110,7 +116,14 @@ struct netpolicy_instance {struct work_struct fc_wk;/* flow classification work */ atomic_t fc_wk_cnt;/* flow classification work number */ struct netpolicy_flow_spec flow;/* flow information */ - +/* For fast path */ +atomic_t rx_queue; +atomic_t tx_queue; +struct work_struct get_rx_wk; +atomic_t get_rx_wk_cnt; +struct work_struct get_tx_wk; +atomic_t get_tx_wk_cnt; +int sys_map_version; };struct netpolicy_cpu_load {
net/core/netpolicy.c +105 -1
190 lines not on this page · raw message
net/ipv4/af_inet.c +2 -5
@@ -760,7 +760,6 @@ static void sock_netpolicy_manage_flow(struct sock *sk, struct msghdr *msg)struct netpolicy_instance *instance;struct netpolicy_flow_spec *flow;bool change = false; -int queue; instance =netpolicy_find_instance (sk);if (!instance)
@@ -814,10 +813,8 @@ static void sock_netpolicy_manage_flow(struct sock *sk, struct msghdr *msg) return; } - queue =netpolicy_pick_queue (instance, true); -if (queue <0 ) -return ; -if ((queue !=atomic_read (&instance->rule_queue)) || change) +if ((atomic_read (&instance->rx_queue) !=atomic_read (&instance->rule_queue)) || + change)netpolicy_set_rules (instance);#endif }
Signature · 1 line
2.5.5
RFC PATCH v3 24/26 net/netpolicy: optimize for queue pair
In reply to [email protected]From: Kan Liang <[email protected]>
Some drivers like i40e driver does not support separate Tx and Rx queues as channels. Using Rx queue to stand for the channels, if queue_pair is set by driver.
--- include/linux/netpolicy.h | 1 + net/core/netpolicy.c | 3 +++ 2 files changed, 4 insertions(+)
include/linux/netpolicy.h +1 -0
@@ -83,6 +83,7 @@ struct netpolicy_info {unsigned long avail_policy[BITS_TO_LONGS (NET_POLICY_MAX )];bool irq_affinity;bool has_mix_policy; +bool queue_pair;/* cpu and queue mapping information */ struct netpolicy_sys_info sys_info;/* List of policy objects 0 rx 1 tx */
net/core/netpolicy.c +3 -0
@@ -538,6 +538,9 @@ int netpolicy_pick_queue(struct netpolicy_instance *instance, bool is_rx)if (!policy_validate (instance))return -EINVAL ; +if (dev->netpolicy->queue_pair) + is_rx = true; +/* fast path */ rcu_read_lock (); version =rcu_dereference (dev->netpolicy->sys_info.version)->major;
Signature · 1 line
2.5.5
RFC PATCH v3 25/26 net/netpolicy: limit the total record number
In reply to [email protected]From: Kan Liang <[email protected]>
NET policy can not fulfill users request without limit, because of the security consideration and device limitation. For security consideration, the attacker may fake millions of per task/socket request to crash the system. For device limitation, the flow director rules number is limited on i40e driver. NET policy should not run out the rules, otherwise it cannot guarantee the good performance.
This patch limits the total record number in RCU hash table to fix the cases as above. The max total record number could vary for different device. For i40e driver, it limits the record number according to flow director rules number. If it exceeds the limitation, the registeration and new object request will be denied.
Since the dev may not be aware in registeration, the cur_rec_num may not be updated on time. So the actual registered record may exceeds the max_rec_num. But it will not bring any problems. Because the patch also check the limitation on object request. It guarantees that the device resource will not run out.
--- include/linux/netpolicy.h | 4 ++++ net/core/netpolicy.c | 23 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-)
include/linux/netpolicy.h +4 -0
@@ -40,6 +40,7 @@ enum netpolicy_traffic {#define NETPOLICY_INVALID_QUEUE -1#define NETPOLICY_INVALID_LOC NETPOLICY_INVALID_QUEUE#define POLICY_NAME_LEN_MAX 64 +#define NETPOLICY_MAX_RECORD_NUM 7000extern const char *policy_name[];struct netpolicy_dev_info {
@@ -88,6 +89,9 @@ struct netpolicy_info {struct netpolicy_sys_info sys_info;/* List of policy objects 0 rx 1 tx */ struct list_head obj_list[NETPOLICY_RXTX ][NET_POLICY_MAX ]; +/* for record number limitation */ +int max_rec_num; +atomic_t cur_rec_num; };struct netpolicy_tcpudpip4_spec {
net/core/netpolicy.c +21 -2
@@ -409,6 +409,9 @@ static int get_avail_queue(struct netpolicy_instance *instance, bool is_rx)unsigned long ptr_id = (uintptr_t )instance->ptr;int queue =-1 ; +if (atomic_read (&dev->netpolicy->cur_rec_num) > dev->netpolicy->max_rec_num) +return queue; +spin_lock_bh (&np_hashtable_lock); old_record =netpolicy_record_search (ptr_id);if (!old_record) {
@@ -430,8 +433,10 @@ static int get_avail_queue(struct netpolicy_instance *instance, bool is_rx)if (is_rx) { new_record->rx_obj =get_avail_object (dev, new_record->policy, instance, is_rx); -if (!new_record->dev) +if (!new_record->dev) { new_record->dev = dev; +atomic_inc (&dev->netpolicy->cur_rec_num); + }if (!new_record->rx_obj) {kfree (new_record); goto err;
@@ -440,8 +445,10 @@ static int get_avail_queue(struct netpolicy_instance *instance, bool is_rx) }else { new_record->tx_obj =get_avail_object (dev, new_record->policy, instance, is_rx); -if (!new_record->dev) +if (!new_record->dev) { new_record->dev = dev; +atomic_inc (&dev->netpolicy->cur_rec_num); + }if (!new_record->tx_obj) {kfree (new_record); goto err;
@@ -685,6 +692,7 @@ int netpolicy_register(struct netpolicy_instance *instance, enum netpolicy_name policy) {unsigned long ptr_id = (uintptr_t )instance->ptr; +struct net_device *dev = instance->dev;struct netpolicy_record *new, *old;if (!is_net_policy_valid (policy)) {
@@ -692,6 +700,10 @@ int netpolicy_register(struct netpolicy_instance *instance,return -EINVAL ; } +if (dev && dev->netpolicy && + (atomic_read (&dev->netpolicy->cur_rec_num) > dev->netpolicy->max_rec_num)) +return -ENOSPC ; + new =kzalloc (sizeof (*new),GFP_KERNEL );if (!new) { instance->policy =NET_POLICY_INVALID ;
@@ -715,6 +727,8 @@ int netpolicy_register(struct netpolicy_instance *instance, new->dev = instance->dev; new->policy = policy;hash_add_rcu (np_record_hash, &new->hash_node, ptr_id); +if (dev && dev->netpolicy) +atomic_inc (&dev->netpolicy->cur_rec_num); } instance->policy = policy;spin_unlock_bh (&np_hashtable_lock);
@@ -761,6 +775,8 @@ void netpolicy_unregister(struct netpolicy_instance *instance)/* The record cannot be share. It can be safely free. */ put_queue (record->dev, record->rx_obj, record->tx_obj);kfree (record); +if (dev && dev->netpolicy) +atomic_dec (&dev->netpolicy->cur_rec_num); } instance->policy =NET_POLICY_INVALID ;spin_unlock_bh (&np_hashtable_lock);
@@ -1298,6 +1314,9 @@ int init_netpolicy(struct net_device *dev) goto unlock; } +if (!dev->netpolicy->max_rec_num) + dev->netpolicy->max_rec_num =NETPOLICY_MAX_RECORD_NUM ; +spin_lock (&dev->np_ob_list_lock);for (i =0 ; i <NETPOLICY_RXTX ; i++) {for (j =NET_POLICY_NONE ; j <NET_POLICY_MAX ; j++)
Signature · 1 line
2.5.5
RFC PATCH v3 26/26 Documentation/networking: Document NET policy
In reply to [email protected]From: Kan Liang <[email protected]>
--- Documentation/networking/netpolicy.txt | 157 +++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 Documentation/networking/netpolicy.txt
Documentation/networking/netpolicy.txt new file +157 -0
@@ -0,0 +1,157 @@ +What is Linux Net Policy? + +It is a big challenge to get good network performance. First, the network +performance is not good with default system settings. Second, it is too +difficult to do automatic tuning for all possible workloads, since workloads +have different requirements. Some workloads may want high throughput. Some may +need low latency. Last but not least, there are lots of manual configurations. +Fine grained configuration is too difficult for users. + +"NET policy" intends to simplify the network configuration and get a +good network performance according to the hints(policy) which is applied by +user. It provides some typical "policies" for user which can be set +per-socket, per-task or per-device. The kernel automatically figures out +how to merge different requests to get good network performance. + +"Net policy" is designed for multiqueue network devices. This document +describes the concepts and APIs of "net policy" support. + +NET POLICY CONCEPTS + +Scope of Net Policies + + Device net policy: this policy applies to the whole device. Once the + device net policy is set, it automatically configures the system + according to the applied policy. The configuration usually includes IRQ + affinity, IRQ balance disable, interrupt moderation, and so on. But the + device net policy does not change the packet direction. + + Task net policy: this is a per-task policy. When it is applied to specific + task, all packet transmissions of the task will be redirected to the + assigned queues accordingly. If a task does not define a task policy, + it "falls back" to the system default way to direct the packets. The + per-task policy must be compatible with device net policy. + + Socket net policy: this is a per-socket policy. When it is applied to + specific socket, all packet transmissions of the socket will be redirected + to the assigned queues accordingly. If a socket does not define a socket + policy, it "falls back" to the system default way to direct the packets. + The per-socket policy must be compatible with both device net policy and + per-task policy. + +Components of Net Policies + + Net policy object: it is a combination of CPU and queue. The queue IRQ has + to set affinity with the CPU. It can be shared between sockets and tasks. + A reference counter is used to track the sharing number. + + Net policy object list: each device policy has an object list. Once the + device policy is determined, the net policy object will be inserted into + the net policy object list. The net policy object list does not change + unless the CPU/queue number is changed, the netpolicy is disabled or + the device policy is changed. + The network performance for objects could be different because of the + CPU/queue topology and dev location. The objects which can bring high + performance are in the front of the list. + + RCU hash table: an RCU hash table to maintain the relationship between + the task/socket and the assigned object. The task/socket can get the + assigned object by searching the table. + If it is the first time, there is no assigned object in the table. It will + go through the object list to find the available object based on position + and reference number. + If the net policy object list changes, all the assigned objects will become + invalid. + +NET POLICY APIs + +Interfaces between net policy and device driver + + int (*ndo_netpolicy_init)(struct net_device *dev, + struct netpolicy_info *info); + + The device driver who has NET policy support must implement this interface. + In this interface, the device driver does necessory initialization, and fill + the info for net policy module. The information could include supported + policy, MIX policy support, queue pair support and so on. + + int (*ndo_get_irq_info)(struct net_device *dev, + struct netpolicy_dev_info *info); + + This interface is used to get more accurate device IRQ information. + + int (*ndo_set_net_policy)(struct net_device *dev, + enum netpolicy_name name); + + This interface is used to set device net policy by name. It is device driver's + responsibility to set driver specific configuration for the given policy. + +Interfaces between net policy and kernel + + int netpolicy_register(struct netpolicy_instance *instance); + void netpolicy_unregister(struct netpolicy_instance *instance); + + This interface is used to register per task/socket net policy. + The socket/task can only be benefited when it register itself with specific + policy. After registeration, a record will be created and inserted into RCU + hash table, which include all the NET policy related information for the + socket/task, such as pointor, policy, object and so on. + + int netpolicy_pick_queue(struct netpolicy_instance *instance, bool is_rx); + + This interface is used to find the proper queue(object) for packet + receiving and transmitting. The proper queue is picked from object list + according to policy, reference, location and so on. + + + int netpolicy_set_rules(struct netpolicy_instance *instance); + + This interface is used to add device specific rules. Once the rule is + applied, the packet from specific IP and port will be redirected to the + given queue. This interface is usually used on receive side. + +NET POLICY INTERFACE + +Device net policy setting + + /proc/net/netpolicy/$DEV/policy + + Concatenating(cat) the "policy" file can show the available device + policies, if there is no device policy applied. Otherwise, the device + policy name will be printed out. If it is MIX policy, the policy for each + queue will also be printed out. + User can set device net policy by writing policy name. + +Task policy setting + + /proc/$PID/net_policy + + Concatenating(cat) the "net_policy" file can show the applied per task + policy. + User can set per task net policy by writing policy name. + + OR + + prctl(PR_SET_NETPOLICY, POLICY_NAME, NULL, NULL, NULL) + + "prctl" is an alternative way to set/get per task policy. + +Socket policy setting + + setsockopt(sockfd,SOL_SOCKET,SO_NETPOLICY,&policy,sizeof(int)) + + The socket net policy can be set by option SO_NETPOLICY of setsockopt. + +AVAILABLE NET POLICIES + + The available net policies are defined as below: + - CPU: intends to get higher throughput and lower CPU% (power saving). + This policy can be applied as either device net policy or + task/socket net policy. + - BULK: intends to get highest throughput. This policy can be applied as + either device net policy or task/socket net policy. + - LATENCY: intends to get lowest latency. This policy can be applied as + either device net policy or task/socket net policy. + - MIX: combination of other policies, which allows each queue to have a + different policy. This policy can only be set as device net policy. +
Signature · 1 line
2.5.5
Quoted 4 lines · [email protected] <[email protected]> wrote:
From: Kan Liang <[email protected]>
It is a big challenge to get good network performance. First, the network performance is not good with default system settings. Second, it is too
[..]
I ask to be dropped from CC list of further submissions of this series, I've said all I have say about this ('do it in userspace') and its very unlikely I will change my opinion.
Thanks.
Quoted 11 lines · Florian Westphal
Quoted 4 lines · [email protected] <[email protected]> wrote:
From: Kan Liang <[email protected]>
It is a big challenge to get good network performance. First, the network performance is not good with default system settings. Second, it is too
[..]
I ask to be dropped from CC list of further submissions of this series, I've said all I have say about this ('do it in userspace') and its very unlikely I will change my opinion.
+1
Same for me.
Quoted 5 lines · On Mon, 2016-09-12 at 07:55 -0700, [email protected] wrote:
From: Kan Liang <[email protected]>
Documentation/networking/netpolicy.txt | 157 ++++
I find this patch series very suspect, as Documentation/networking/scaling.txt is untouched.
I highly recommend you present your ideas at next netdev conference.
I really doubt the mailing lists are the best place to present your work, given the huge amount of code/layers you want to add in linux kernel.
Quoted 8 lines · On Mon, 12 Sep 2016 08:52:14 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 5 lines · On Mon, 2016-09-12 at 07:55 -0700, [email protected] wrote:
From: Kan Liang <[email protected]>
Documentation/networking/netpolicy.txt | 157 ++++
Quoted 8 lines
I find this patch series very suspect, as
Documentation/networking/scaling.txtis untouched.I highly recommend you present your ideas at next netdev conference.
I really doubt the mailing lists are the best place to present your work, given the huge amount of code/layers you want to add in linux kernel.
Agreed.