Skip to content

Commit

Permalink
net: context: Set priority based on DSCP
Browse files Browse the repository at this point in the history
If a socket has DSCP set then the packets from the socket should also be
marked with appropriate priority in case traffic classes are used in
networking stack.

Signed-off-by: Krishna T <[email protected]>
  • Loading branch information
Krishna T authored and carlescufi committed May 16, 2023
1 parent 10f3ca2 commit 2c12960
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions subsys/net/ip/ipv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ static inline void net_ipv4_set_dscp(uint8_t *tos, uint8_t dscp)
*tos |= (dscp << NET_IPV4_DSCP_OFFSET) & NET_IPV4_DSCP_MASK;
}

/**
* @brief Convert DSCP value to priority.
*
* @param dscp DSCP value.
*/
static inline uint8_t net_ipv4_dscp_to_priority(uint8_t dscp)
{
return dscp >> 3;
}

/**
* @brief Decode ECN value from ToS field.
*
Expand Down
10 changes: 10 additions & 0 deletions subsys/net/ip/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,16 @@ static inline void net_ipv6_set_dscp(uint8_t *tc, uint8_t dscp)
*tc |= (dscp << NET_IPV6_DSCP_OFFSET) & NET_IPV6_DSCP_MASK;
}

/**
* @brief Convert DSCP value to priority.
*
* @param dscp DSCP value.
*/
static inline uint8_t net_ipv6_dscp_to_priority(uint8_t dscp)
{
return dscp >> 3;
}

/**
* @brief Decode ECN value from TC field.
*
Expand Down
10 changes: 10 additions & 0 deletions subsys/net/ip/net_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ int net_context_create_ipv4_new(struct net_context *context,
#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
net_pkt_set_ip_dscp(pkt, net_ipv4_get_dscp(context->options.dscp_ecn));
net_pkt_set_ip_ecn(pkt, net_ipv4_get_ecn(context->options.dscp_ecn));
/* Direct priority takes precedence over DSCP */
if (!IS_ENABLED(CONFIG_NET_CONTEXT_PRIORITY)) {
net_pkt_set_priority(pkt, net_ipv4_dscp_to_priority(
net_ipv4_get_dscp(context->options.dscp_ecn)));
}
#endif

return net_ipv4_create(pkt, src, dst);
Expand Down Expand Up @@ -928,6 +933,11 @@ int net_context_create_ipv6_new(struct net_context *context,
#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
net_pkt_set_ip_dscp(pkt, net_ipv6_get_dscp(context->options.dscp_ecn));
net_pkt_set_ip_ecn(pkt, net_ipv6_get_ecn(context->options.dscp_ecn));
/* Direct priority takes precedence over DSCP */
if (!IS_ENABLED(CONFIG_NET_CONTEXT_PRIORITY)) {
net_pkt_set_priority(pkt, net_ipv6_dscp_to_priority(
net_ipv6_get_dscp(context->options.dscp_ecn)));
}
#endif

return net_ipv6_create(pkt, src, dst);
Expand Down

0 comments on commit 2c12960

Please sign in to comment.