@@ -12,8 +12,9 @@ Overview
1212
1313The Network Packet Filtering facility provides the infrastructure to
1414construct custom rules for accepting and/or denying packet transmission
15- and reception. This can be used to create a basic firewall, control
16- network traffic, etc.
15+ and reception. It also allows to modify the priority of incoming
16+ network packets. This can be used to create a basic firewall, control network
17+ traffic, etc.
1718
1819The :kconfig:option: `CONFIG_NET_PKT_FILTER ` must be set in order to enable the
1920relevant APIs.
@@ -25,8 +26,13 @@ for a given rule are true then the packet outcome is immediately determined
2526as specified by the current rule and no more rules are considered. If one
2627condition is false then the next rule in the list is considered.
2728
28- Packet outcome is either ``NET_OK `` to accept the packet or ``NET_DROP `` to
29- drop it.
29+ Packet outcome is either ``NET_OK `` to accept the packet, ``NET_DROP `` to
30+ drop it or ``NET_CONTINUE `` to modify its priority on the fly.
31+
32+ When the outcome is ``NET_CONTINUE `` the priority is updated but the final
33+ outcome is not yet determined and processing continues. If all conditions of
34+ multiple rules are true, then the packet gets the priority of the rule last
35+ considered.
3036
3137A rule is represented by a :c:struct: `npf_rule ` object. It can be inserted to,
3238appended to or removed from a rule list contained in a
@@ -47,7 +53,8 @@ retrieve the outer structure from the provided ``npf_test`` structure pointer.
4753
4854Convenience macros are provided in :zephyr_file: `include/zephyr/net/net_pkt_filter.h `
4955to statically define condition instances for various conditions, and
50- :c:macro: `NPF_RULE() ` to create a rule instance to tie them.
56+ :c:macro: `NPF_RULE() ` and :c:macro: `NPF_PRIORITY() ` to create a rule instance
57+ with an immediate outcome or a priority change.
5158
5259Examples
5360********
@@ -86,6 +93,42 @@ Another (less efficient) way to achieve the same result could be:
8693 npf_append_recv_rule(&npf_default_ok);
8794 }
8895
96+ This example assigns priorities to different network traffic. It gives network
97+ control priority (``NET_PRIORITY_NC ``) to the ``ptp `` packets, critical
98+ applications priority (``NET_PRIORITY_CA ``) to the internet traffic of version
99+ 6, excellent effort (``NET_PRIORITY_EE ``) for internet protocol version 4
100+ traffic, and the lowest background priority (``NET_PRIORITY_BK ``) to ``lldp ``
101+ and ``arp ``.
102+
103+ Priority rules are only really uselfull if multiple traffic class queues are
104+ enabled in the project configuration :kconfig:option: `CONFIG_NET_TC_RX_COUNT `.
105+ The mapping from the priority of the packet to the traffic class queue is in
106+ accordance with the standard 802.1Q and depends on the
107+ :kconfig:option: `CONFIG_NET_TC_RX_COUNT `.
108+
109+ .. code-block :: c
110+
111+ static NPF_ETH_TYPE_MATCH(is_arp_packet, NET_ETH_PTYPE_ARP);
112+ static NPF_ETH_TYPE_MATCH(is_lldp_packet, NET_ETH_PTYPE_LLDP);
113+ static NPF_ETH_TYPE_MATCH(is_ptp_packet, NET_ETH_PTYPE_PTP);
114+ static NPF_ETH_TYPE_MATCH(is_ipv4_packet, NET_ETH_PTYPE_IP);
115+ static NPF_ETH_TYPE_MATCH(is_ipv6_packet, NET_ETH_PTYPE_IPV6);
116+
117+ static NPF_PRIORITY(rule_arp, NET_PRIORITY_BK, is_arp_packet);
118+ static NPF_PRIORITY(rule_lldp, NET_PRIORITY_BK, is_lldp_packet);
119+ static NPF_PRIORITY(rule_ipv4, NET_PRIORITY_EE, is_ipv4_packet);
120+ static NPF_PRIORITY(rule_ipv6, NET_PRIORITY_CA, is_ipv6_packet);
121+ static NPF_PRIORITY(rule_ptp, NET_PRIORITY_NC, is_ptp_packet);
122+
123+ void install_my_filter(void) {
124+ npf_append_recv_rule(&rule_arp);
125+ npf_append_recv_rule(&rule_lldp);
126+ npf_append_recv_rule(&rule_ipv4);
127+ npf_append_recv_rule(&rule_ipv6);
128+ npf_append_recv_rule(&rule_ptp);
129+ npf_append_recv_rule(&npf_default_ok);
130+ }
131+
89132 API Reference
90133*************
91134
0 commit comments