From patchwork Thu Dec 5 13:31:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna Nyiri X-Patchwork-Id: 13895340 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A92D8219A9B for ; Thu, 5 Dec 2024 13:31:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405515; cv=none; b=ZZA9mZiNjcYOCcdyXjg19OSvmkqkaxjG0t4xdfB11vcsBAO8+wbGLv3X3QMuOfdW4L8AgvYQumgc/C1wJSc/Q6HGtl2XVLOTXclMYEBcLm9NtrFIZ5Rw0CpxNGooXlnd/3wzUIFLkyZ9YzEU1PxIUHm0OShIi0C+hzoNu5VGQkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405515; c=relaxed/simple; bh=QPh03QEa0M8YEQs8GzmP3qJH+OhK905FKbbXXrN+B+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GJuzR6VrQWwdwDGReOROgqRgnp3UFx7r2hDBqT6mIkB8vvTQN5RDgPpbljHApDloq1uWRwoMW/2i5IkixiCw01S7vPkczMvmPzxjsRy3dD6AvaB4nLhCxW6WrhXzh+Uu0+NF/oAXSRKwWZP+zrHNKKWBRNjQSt2zNao4LzuRKp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bcF7QlVJ; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bcF7QlVJ" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-434a90fed23so6374945e9.1 for ; Thu, 05 Dec 2024 05:31:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733405512; x=1734010312; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fNATByMEO9e+OmkVFZzCHZ/xIGul3/7QVkpEK0lUick=; b=bcF7QlVJKn/n5c7aIn28/mWuEZcJEKaz5EGoxfv0t1WSmLREu7U50MuqRTBFrmgeW0 i5WAUeyWU/0ARAZNsJBzd3v+kU6/XRq323yFrZ9/F558Vrc1YnVTk/LmZg0PnRDGZvPV TgFbUMXFQc6+Gl6i4KGs8Cczw7MIbSMGz1A4ueAbAVdfiNu30QylK7Ha27ak1jRml3p+ weJD7c72mCU/aSJEqTfgzW+LySaKZZJasGGxrYyp07xCfuNoDEp7Ga2gArzaVTpW7BT9 BuAsym/1Dkb/k41pYmL2je9oBthniZg7mhtOK56T45AwSr0lRciW/kanHhDngwJRz9PL iHzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733405512; x=1734010312; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fNATByMEO9e+OmkVFZzCHZ/xIGul3/7QVkpEK0lUick=; b=oqP/plsuwPjrjxr71KnU/8/NFUGBFB/6e4v+RalK7k5qgC8QEnq8ITmrdpkObqZvgV yS3DHYKJKs4W7kS/uWrzOfTP9MNW/O8QHRoLxxQB2DZ9EGAPr7EIQky3Dg05jCKKfWm6 jn7eX3lMEvQnpxFFOTqUnihYFs2SO9yaI3LAvXh3/WT4I+/zRNIRetXrwDDIoDr0hL1O MFEH9Oz8hwesyPrqWEXAWm5Fk5YaTJF0Y9D5x8YEgpqd5JUW59mUaQaL0uEHyo6+eleh Ja2AYUTu1woVZVSq2dVbS6MmAPNSjGhzKl6yoq09aeH2bANzM4PznFcGt4UZlyO4IJ3x pC/w== X-Gm-Message-State: AOJu0Yyj1pdrU9vOPoXqIZ1I5+cREjiyzTWkWsyk8/qa7bjA+w1DctGs dQXG6VYEwReOtO03tUtnVVjUEpUWOyW7xLk3eS/dEqlh8g3I5ApLPnY8jjCP+dA= X-Gm-Gg: ASbGnctbdCaoCs5rQvs1rC7AU9Qy3F4fe7XuQIqAv71fx628IQTkh36Yde/ah11o0XX mXqhKxcjH/wLNiS/2RaHK2/cJzZmsjIe8oMrEiODXPrC5BjdZlg6K7js6romum4lIr6s35QDjyI 7dwuB2lslKhdQKghhW/LbA3qXT8dm2xAqiIk32mIXsYyO2sEHHt3c8rdBAqMMRGIo3y3asRv/yl XMtQ4KUnIZb2iQFiWd+3YRkmR1gzQen9Iz7xbe1eF5y6PIGUQTigeJY8pO26BfMiwM9gylkfTfA DblniTmlFURZXNVgX8qvAOJuXeOfKtrdWlvkzdT/TicadfXaVXH8WIxnfA== X-Google-Smtp-Source: AGHT+IG5Jk0INyqZQFDQugeufm7NCoe53R7jn43ojJGy2F4Iz17G1eLBOqFmunl/U6+CfIr8OJvIjA== X-Received: by 2002:a05:600c:1f8f:b0:434:a396:9474 with SMTP id 5b1f17b1804b1-434d09cbe39mr92641505e9.18.1733405511777; Thu, 05 Dec 2024 05:31:51 -0800 (PST) Received: from localhost.localdomain (20014C4E37C0C700ABF575982C3B3E76.dsl.pool.telekom.hu. [2001:4c4e:37c0:c700:abf5:7598:2c3b:3e76]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434d5280fc4sm60852465e9.24.2024.12.05.05.31.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 05:31:51 -0800 (PST) From: Anna Emese Nyiri To: netdev@vger.kernel.org Cc: fejes@inf.elte.hu, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, idosch@idosch.org, Anna Emese Nyiri Subject: [PATCH net-next v5 1/4] sock: Introduce sk_set_prio_allowed helper function Date: Thu, 5 Dec 2024 14:31:09 +0100 Message-ID: <20241205133112.17903-2-annaemesenyiri@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241205133112.17903-1-annaemesenyiri@gmail.com> References: <20241205133112.17903-1-annaemesenyiri@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Simplify priority setting permissions with the 'sk_set_prio_allowed' function, centralizing the validation logic. This change is made in anticipation of a second caller in a following patch. No functional changes. Reviewed-by: Willem de Bruijn Reviewed-by: Eric Dumazet Suggested-by: Willem de Bruijn Signed-off-by: Anna Emese Nyiri --- net/core/sock.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 74729d20cd00..9016f984d44e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -454,6 +454,13 @@ static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen, return 0; } +static bool sk_set_prio_allowed(const struct sock *sk, int val) +{ + return ((val >= TC_PRIO_BESTEFFORT && val <= TC_PRIO_INTERACTIVE) || + sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) || + sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)); +} + static bool sock_needs_netstamp(const struct sock *sk) { switch (sk->sk_family) { @@ -1193,9 +1200,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname, /* handle options which do not require locking the socket. */ switch (optname) { case SO_PRIORITY: - if ((val >= 0 && val <= 6) || - sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) || - sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { + if (sk_set_prio_allowed(sk, val)) { sock_set_priority(sk, val); return 0; } From patchwork Thu Dec 5 13:31:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna Nyiri X-Patchwork-Id: 13895341 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F34E219A9F for ; Thu, 5 Dec 2024 13:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405517; cv=none; b=MAaXHMxh/MNIiGuO16TYPfg+gmFw3KtcGjRBnpMjVrNbeGU9jMphh6mCT2xizk4y0glK6pz5Le9cuJYEU3yLssgRb4B96f0ShvAqSgIjzmLZzuYnMP6mCiyOvMlpCVJkjIsTvp7zNmXxpE9smkFlumWxO2mBQ0DRsZA9KBw7/nM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405517; c=relaxed/simple; bh=DbHPvlIimZiedmRYTDT8zdoDSyoO1hTrxnWsGzmSiPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HwHzklUPmdR5+k8rHQzsKRU2TqAVmnybpXDYOQEkv0z1ExSEVL8M9szMehvfkNlxqY8Cb6/2gy/tovm1xBeMHxaLG35WU5xcE7SFGmr+KI8ju8yIcbgDbMjLoXTgyjKYb+MHMveqwO/4CCnEtQ3b9TkHFroTL0zCWjlIQ73s0tk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VJwcHJ0j; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VJwcHJ0j" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-434a1639637so10028715e9.1 for ; Thu, 05 Dec 2024 05:31:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733405514; x=1734010314; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mEBuFHKRsrtBdcV60BXZPS+V3Bhyi4EnWZVBsP3ESnY=; b=VJwcHJ0jy6WCoQKINqv2wrOMLpcTvsE/HvmuOhO7I3fAQtsOsMalnq5oDKLgQ0Z7bS DmsD0Ho8xVlWthb4JtHwctLnX4369Fo9kmiDVtCRlnKiM7Xg3I+ryUR6LTh/bFSWZSUX q0Ms9X3j4SEB/M63WJd25WUfSICOchKAj/8V1M6j+gQzTDO5UlUcQYYgA12Pxyi5fT+a jnZAGUvBHYdR+Ysx/hA1fin7Nh/kiBrdFhA7pUxOMmyG021z32Q7a55BbwYWTPBnbI84 1GQLP6epKCwWjQsPJLgps0pGHmjdfGSiCr8WvNxxn99nmhEBp2iCksCUoWh312bWMjbF JjJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733405514; x=1734010314; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mEBuFHKRsrtBdcV60BXZPS+V3Bhyi4EnWZVBsP3ESnY=; b=aWHosflMHeOKxQKtClmt8Lr8EmsAq8UWH4UWO9dp6b4jbL/5mzr1BgbwwPmjqYcD+8 XZo9dlbTl/Cos0RXf5MGa1sgZnwBQmXJtcrbtSwY8tuWSF4wJVwxiJcSgTbVS1vxYYja ScuwaB1gQnnTWBzZ60BVz8nKGP5cmk1Qr8Jk0bN1xk8ckjADdn28HJl60NYVmvLtxKgi IuEazh0qzvTiTrbpFZ2VEnuqLBVCKpTkvbSj5AA4Up3XeXLBQzGEQuM35e3v1wIaa084 kPo9JuzfIlIsuB1jMRghyJRcqT6qRMCQkwSspjWdzO7GgxvUiX5y6pAeoDHMLYKvl/Rv djlA== X-Gm-Message-State: AOJu0YyKnzIflC2VmeOpzVQmFrVqCcPixsx1yAvw6Hjh2lNw+enyLfGe qFDp74U1qnrEey8MnWLatZmlQ1CSXN/U+lxZtqUfndPfqMOyVgabol5452nYfuk= X-Gm-Gg: ASbGncsVGGj5mKrsodnGBuNrQyletUZpY1zMZO7t2j/Unnfj0bYldnhISLjoOGEiWTv ijoZwdi0wKchSzmdlvebJv8M1PE/wkMz9WdiVqgDNW/5tOGZm1mJ5kQ3ttEAs5SflCDodsZM7bD g+V5Dz6jEYo+DuzVgpUKCfjrHLGnAUvWF3P+0eJt4odpnK8VAFUVitN9KpP+SLNr5SB+aFacSy2 30l+kB0bJeszNMxuKz0P3oCwl4Ku6fey2sL+9EvT4YC+Mr5RxR0R5B0S6IZksoREKe0mD7tzUR/ U4Gp3VHSdMSyCg2FJTlYnMPUydlNbMapFW3yhcJXlUKrLwtJfvFFeD96fQ== X-Google-Smtp-Source: AGHT+IFOPWJMarFVfSzL3FbSB8qDXZH+5RG/wGlL4bNwVMPAVnkAnLWVTYt1XAEivC3GTDAXLrQHYw== X-Received: by 2002:a05:600c:5105:b0:434:a6af:79f6 with SMTP id 5b1f17b1804b1-434d09c2594mr94622725e9.15.1733405513532; Thu, 05 Dec 2024 05:31:53 -0800 (PST) Received: from localhost.localdomain (20014C4E37C0C700ABF575982C3B3E76.dsl.pool.telekom.hu. [2001:4c4e:37c0:c700:abf5:7598:2c3b:3e76]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434d5280fc4sm60852465e9.24.2024.12.05.05.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 05:31:53 -0800 (PST) From: Anna Emese Nyiri To: netdev@vger.kernel.org Cc: fejes@inf.elte.hu, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, idosch@idosch.org, Anna Emese Nyiri Subject: [PATCH net-next v5 2/4] sock: support SO_PRIORITY cmsg Date: Thu, 5 Dec 2024 14:31:10 +0100 Message-ID: <20241205133112.17903-3-annaemesenyiri@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241205133112.17903-1-annaemesenyiri@gmail.com> References: <20241205133112.17903-1-annaemesenyiri@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org The Linux socket API currently allows setting SO_PRIORITY at the socket level, applying a uniform priority to all packets sent through that socket. The exception to this is IP_TOS, when the priority value is calculated during the handling of ancillary data, as implemented in commit ("ipv4: IP_TOS and IP_TTL can be specified as ancillary data"). However, this is a computed value, and there is currently no mechanism to set a custom priority via control messages prior to this patch. According to this patch, if SO_PRIORITY is specified as ancillary data, the packet is sent with the priority value set through sockc->priority, overriding the socket-level values set via the traditional setsockopt() method. This is analogous to the existing support for SO_MARK, as implemented in commit ("ip: support SO_MARK cmsg"). If both cmsg SO_PRIORITY and IP_TOS are passed, then the one that takes precedence is the last one in the cmsg list. This patch has the side effect that raw_send_hdrinc now interprets cmsg IP_TOS. Reviewed-by: Willem de Bruijn Suggested-by: Ferenc Fejes Signed-off-by: Anna Emese Nyiri --- include/net/inet_sock.h | 2 +- include/net/ip.h | 2 +- include/net/sock.h | 4 +++- net/can/raw.c | 2 +- net/core/sock.c | 7 +++++++ net/ipv4/ip_output.c | 4 ++-- net/ipv4/ip_sockglue.c | 2 +- net/ipv4/raw.c | 2 +- net/ipv6/ip6_output.c | 3 ++- net/ipv6/ping.c | 1 + net/ipv6/raw.c | 3 ++- net/ipv6/udp.c | 1 + net/packet/af_packet.c | 2 +- 13 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h index 56d8bc5593d3..3ccbad881d74 100644 --- a/include/net/inet_sock.h +++ b/include/net/inet_sock.h @@ -172,7 +172,7 @@ struct inet_cork { u8 tx_flags; __u8 ttl; __s16 tos; - char priority; + u32 priority; __u16 gso_size; u32 ts_opt_id; u64 transmit_time; diff --git a/include/net/ip.h b/include/net/ip.h index 0e548c1f2a0e..9f5e33e371fc 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -81,7 +81,6 @@ struct ipcm_cookie { __u8 protocol; __u8 ttl; __s16 tos; - char priority; __u16 gso_size; }; @@ -96,6 +95,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm, ipcm_init(ipcm); ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark); + ipcm->sockc.priority = READ_ONCE(inet->sk.sk_priority); ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags); ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if); ipcm->addr = inet->inet_saddr; diff --git a/include/net/sock.h b/include/net/sock.h index 7464e9f9f47c..316a34d6c48b 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1814,13 +1814,15 @@ struct sockcm_cookie { u32 mark; u32 tsflags; u32 ts_opt_id; + u32 priority; }; static inline void sockcm_init(struct sockcm_cookie *sockc, const struct sock *sk) { *sockc = (struct sockcm_cookie) { - .tsflags = READ_ONCE(sk->sk_tsflags) + .tsflags = READ_ONCE(sk->sk_tsflags), + .priority = READ_ONCE(sk->sk_priority), }; } diff --git a/net/can/raw.c b/net/can/raw.c index 255c0a8f39d6..46e8ed9d64da 100644 --- a/net/can/raw.c +++ b/net/can/raw.c @@ -962,7 +962,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) } skb->dev = dev; - skb->priority = READ_ONCE(sk->sk_priority); + skb->priority = sockc.priority; skb->mark = READ_ONCE(sk->sk_mark); skb->tstamp = sockc.transmit_time; diff --git a/net/core/sock.c b/net/core/sock.c index 9016f984d44e..a3d9941c1d32 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2947,6 +2947,13 @@ int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg, case SCM_RIGHTS: case SCM_CREDENTIALS: break; + case SO_PRIORITY: + if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32))) + return -EINVAL; + if (!sk_set_prio_allowed(sk, *(u32 *)CMSG_DATA(cmsg))) + return -EPERM; + sockc->priority = *(u32 *)CMSG_DATA(cmsg); + break; default: return -EINVAL; } diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 0065b1996c94..cd3e788600cc 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1328,7 +1328,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, cork->ttl = ipc->ttl; cork->tos = ipc->tos; cork->mark = ipc->sockc.mark; - cork->priority = ipc->priority; + cork->priority = ipc->sockc.priority; cork->transmit_time = ipc->sockc.transmit_time; cork->tx_flags = 0; sock_tx_timestamp(sk, &ipc->sockc, &cork->tx_flags); @@ -1465,7 +1465,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk, ip_options_build(skb, opt, cork->addr, rt); } - skb->priority = (cork->tos != -1) ? cork->priority: READ_ONCE(sk->sk_priority); + skb->priority = cork->priority; skb->mark = cork->mark; if (sk_is_tcp(sk)) skb_set_delivery_time(skb, cork->transmit_time, SKB_CLOCK_MONOTONIC); diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c index cf377377b52d..f6a03b418dde 100644 --- a/net/ipv4/ip_sockglue.c +++ b/net/ipv4/ip_sockglue.c @@ -315,7 +315,7 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, if (val < 0 || val > 255) return -EINVAL; ipc->tos = val; - ipc->priority = rt_tos2priority(ipc->tos); + ipc->sockc.priority = rt_tos2priority(ipc->tos); break; case IP_PROTOCOL: if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c index 0e9e01967ec9..4304a68d1db0 100644 --- a/net/ipv4/raw.c +++ b/net/ipv4/raw.c @@ -358,7 +358,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4, skb_reserve(skb, hlen); skb->protocol = htons(ETH_P_IP); - skb->priority = READ_ONCE(sk->sk_priority); + skb->priority = sockc->priority; skb->mark = sockc->mark; skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, sk->sk_clockid); skb_dst_set(skb, &rt->dst); diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index f7b4608bb316..ec9673b7ab16 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1401,6 +1401,7 @@ static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork, cork->base.gso_size = ipc6->gso_size; cork->base.tx_flags = 0; cork->base.mark = ipc6->sockc.mark; + cork->base.priority = ipc6->sockc.priority; sock_tx_timestamp(sk, &ipc6->sockc, &cork->base.tx_flags); if (ipc6->sockc.tsflags & SOCKCM_FLAG_TS_OPT_ID) { cork->base.flags |= IPCORK_TS_OPT_ID; @@ -1939,7 +1940,7 @@ struct sk_buff *__ip6_make_skb(struct sock *sk, hdr->saddr = fl6->saddr; hdr->daddr = *final_dst; - skb->priority = READ_ONCE(sk->sk_priority); + skb->priority = cork->base.priority; skb->mark = cork->base.mark; if (sk_is_tcp(sk)) skb_set_delivery_time(skb, cork->base.transmit_time, SKB_CLOCK_MONOTONIC); diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c index 88b3fcacd4f9..46b8adf6e7f8 100644 --- a/net/ipv6/ping.c +++ b/net/ipv6/ping.c @@ -119,6 +119,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) return -EINVAL; ipcm6_init_sk(&ipc6, sk); + ipc6.sockc.priority = READ_ONCE(sk->sk_priority); ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); ipc6.sockc.mark = READ_ONCE(sk->sk_mark); diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c index 8476a3944a88..a45aba090aa4 100644 --- a/net/ipv6/raw.c +++ b/net/ipv6/raw.c @@ -619,7 +619,7 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, skb_reserve(skb, hlen); skb->protocol = htons(ETH_P_IPV6); - skb->priority = READ_ONCE(sk->sk_priority); + skb->priority = sockc->priority; skb->mark = sockc->mark; skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, sk->sk_clockid); @@ -780,6 +780,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ipcm6_init(&ipc6); ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); ipc6.sockc.mark = fl6.flowi6_mark; + ipc6.sockc.priority = READ_ONCE(sk->sk_priority); if (sin6) { if (addr_len < SIN6_LEN_RFC2133) diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index d766fd798ecf..7c14c449804c 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1448,6 +1448,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ipc6.gso_size = READ_ONCE(up->gso_size); ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); ipc6.sockc.mark = READ_ONCE(sk->sk_mark); + ipc6.sockc.priority = READ_ONCE(sk->sk_priority); /* destination address check */ if (sin6) { diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 886c0dd47b66..f8d87d622699 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -3126,7 +3126,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) skb->protocol = proto; skb->dev = dev; - skb->priority = READ_ONCE(sk->sk_priority); + skb->priority = sockc.priority; skb->mark = sockc.mark; skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid); From patchwork Thu Dec 5 13:31:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna Nyiri X-Patchwork-Id: 13895342 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C506219A9B for ; Thu, 5 Dec 2024 13:31:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405519; cv=none; b=R5geAk6abncd71oxoUgNOzTE75gB8br+jkY1fSwyVn5VFeqP3ALqKVWDYmbs/2j+NrHG+9yi+wbpCTF9w/eGf5PQaG7e4rnNIMICiURphWpGR3PwIHnacMUI/1FqXiUU53xK7NKLYMiYdYMInjZLRjLjGJOziCOTuz+28qiDnYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405519; c=relaxed/simple; bh=S3CCHUywvGD8qfDi7dyr7uupCCIODj9jz85uHMlYPP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V60vDPCf+ibMfz+rgCpO2acSP+yr6+QqAbIc8NEDJLceP9DAs0x7ANG+NNuuOfMaDhM1HNZ+aiiuDdQxUo5o2/YMB6TZcdHVGFNBcK/pcOV/1C8TFtcEs//RRkYJuQIp5bE25JAiOOCdl3HXmJN8c3Ilr8z/F9ZRNIEgVSAhIhQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JPCB87JF; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JPCB87JF" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-434a2f3bae4so9927055e9.3 for ; Thu, 05 Dec 2024 05:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733405515; x=1734010315; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=x0ZRQMk2CGXqpoVWIuM6uZi5DuwSjgZRELAS6N0IOis=; b=JPCB87JFsBoWM9aOzyJi7vSWu4d3c/d1IXZ2pJ1cIP3UwlrmlV92ZGljJQ8OLoDGdz qYuXUVpuBlqV4YD+sRmCVBd1eLzvE7DvqliPMG5reJz+/XzGrbM5YE9/HH/IDzG+ByD7 ljKNNlAyjdUcrlxHcdV5JFsozMllNRBe7tzJssVmQQlXSbQCmnhOjKh52pyYirwwv6hl ZPWi1BL3e0N+3X8UGiRuDuanOxTyEPnupx0I5x8gimOFEsqVhEz9SFQmH8a48lcpq0WB J9hqeLGymwpD7O7UEULJxfw8FPVLluvjbetroccRUhquhoOi18X57e7QmZ2lC7ZoOo3k QfFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733405515; x=1734010315; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x0ZRQMk2CGXqpoVWIuM6uZi5DuwSjgZRELAS6N0IOis=; b=VKYko/3nMQPENW/ntkt18Gnfiywgd9RSr3EgHQGdBlolWYqq5vSovggcR3KS/qrTi2 dt9DQTIpP7s8XzG6h9w7I+qfQVk2W+LBni7h5ldaCZNwhMD2hfAtT0runvptW2TgOrCi vi9V+gObrK5VTTkaQdYCv9JhaE9bMyJD2k81KmoJjjG3ijMKBm/ovmMQw2LKQBJynQEq mb0oDFhx5FGUGyUbvPRKfFX53+QSp0e/kMOlg+fynfcHY0xFw7dEz6pALtsagDcwWp1c r4KgEquxsARSUANynvooEAN1PK4K/eObjsfprsZBFZNxDcauazIvN1Xkn6I9yRtCXSod i3xQ== X-Gm-Message-State: AOJu0YwiOm9Q3WMnYS39CmVlvu0K14C5WqnYIeNR1SULuQSo67E43r4s fhX2o9FSQvuAO56kY47r9BU2NjH6gS3Os9u1da8Hez+4ad/2nssbmooFhD9WSgY= X-Gm-Gg: ASbGncuKjnpFR1FxhEkxjCLHWLn3u1eYwRzPC2nzGRgaEAVWKPnoG6o89+ZM+3twbKM NTnAE1br4SdmsjF72W23bWva6f0M0u9mnn7YtZctFFNHohao6m2Ujww5SxL3NJGLjQq8z1IEhKz 9EE3QBmP7loLp6ACEehzS1kBfEPUbaRDp6mDL4pqGpYYsYDQ/piBEPtRaAO8B+deLGo4+aTROog xKYwLSSzi+ocVL1U6vrwQgSsP5tHture3DBQd/uztgFEzVdgUt8d9uoJzclVepbEU+u8NQ9eJvm Pz17JngESnuB+BhH4eJzd470FB8GJ9MLAe2eS4ehv+941MCY6IiiNtuA1g== X-Google-Smtp-Source: AGHT+IFFJUrhKgC63y8QEgCUKVGN55uMdJP92WWuUYFkpUju6TOaaAL/eHW7SE1i8Apm3eYijLiYFA== X-Received: by 2002:a05:600c:220b:b0:434:a815:2b5d with SMTP id 5b1f17b1804b1-434d1116ebbmr87167805e9.24.1733405514838; Thu, 05 Dec 2024 05:31:54 -0800 (PST) Received: from localhost.localdomain (20014C4E37C0C700ABF575982C3B3E76.dsl.pool.telekom.hu. [2001:4c4e:37c0:c700:abf5:7598:2c3b:3e76]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434d5280fc4sm60852465e9.24.2024.12.05.05.31.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 05:31:54 -0800 (PST) From: Anna Emese Nyiri To: netdev@vger.kernel.org Cc: fejes@inf.elte.hu, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, idosch@idosch.org, Anna Emese Nyiri Subject: [PATCH net-next v5 3/4] selftests: net: test SO_PRIORITY ancillary data with cmsg_sender Date: Thu, 5 Dec 2024 14:31:11 +0100 Message-ID: <20241205133112.17903-4-annaemesenyiri@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241205133112.17903-1-annaemesenyiri@gmail.com> References: <20241205133112.17903-1-annaemesenyiri@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Extend cmsg_sender.c with a new option '-Q' to send SO_PRIORITY ancillary data. cmsg_so_priority.sh script added to validate SO_PRIORITY behavior by creating VLAN device with egress QoS mapping and testing packet priorities using flower filters. Verify that packets with different priorities are correctly matched and counted by filters for multiple protocols and IP versions. Suggested-by: Ido Schimmel Signed-off-by: Anna Emese Nyiri --- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/cmsg_sender.c | 11 +- .../testing/selftests/net/cmsg_so_priority.sh | 151 ++++++++++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/net/cmsg_so_priority.sh diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index cb2fc601de66..f09bd96cc978 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -32,6 +32,7 @@ TEST_PROGS += ioam6.sh TEST_PROGS += gro.sh TEST_PROGS += gre_gso.sh TEST_PROGS += cmsg_so_mark.sh +TEST_PROGS += cmsg_so_priority.sh TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh TEST_PROGS += netns-name.sh TEST_PROGS += nl_netdev.py diff --git a/tools/testing/selftests/net/cmsg_sender.c b/tools/testing/selftests/net/cmsg_sender.c index 876c2db02a63..99b0788f6f0c 100644 --- a/tools/testing/selftests/net/cmsg_sender.c +++ b/tools/testing/selftests/net/cmsg_sender.c @@ -59,6 +59,7 @@ struct options { unsigned int proto; } sock; struct option_cmsg_u32 mark; + struct option_cmsg_u32 priority; struct { bool ena; unsigned int delay; @@ -97,6 +98,8 @@ static void __attribute__((noreturn)) cs_usage(const char *bin) "\n" "\t\t-m val Set SO_MARK with given value\n" "\t\t-M val Set SO_MARK via setsockopt\n" + "\t\t-P val Set SO_PRIORITY via setsockopt\n" + "\t\t-Q val Set SO_PRIORITY via cmsg\n" "\t\t-d val Set SO_TXTIME with given delay (usec)\n" "\t\t-t Enable time stamp reporting\n" "\t\t-f val Set don't fragment via cmsg\n" @@ -115,7 +118,7 @@ static void cs_parse_args(int argc, char *argv[]) { int o; - while ((o = getopt(argc, argv, "46sS:p:P:m:M:n:d:tf:F:c:C:l:L:H:")) != -1) { + while ((o = getopt(argc, argv, "46sS:p:P:m:M:n:d:tf:F:c:C:l:L:H:Q:")) != -1) { switch (o) { case 's': opt.silent_send = true; @@ -148,6 +151,10 @@ static void cs_parse_args(int argc, char *argv[]) opt.mark.ena = true; opt.mark.val = atoi(optarg); break; + case 'Q': + opt.priority.ena = true; + opt.priority.val = atoi(optarg); + break; case 'M': opt.sockopt.mark = atoi(optarg); break; @@ -252,6 +259,8 @@ cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz) ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, SOL_SOCKET, SO_MARK, &opt.mark); + ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, + SOL_SOCKET, SO_PRIORITY, &opt.priority); ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, SOL_IPV6, IPV6_DONTFRAG, &opt.v6.dontfrag); ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, diff --git a/tools/testing/selftests/net/cmsg_so_priority.sh b/tools/testing/selftests/net/cmsg_so_priority.sh new file mode 100755 index 000000000000..016458b219ba --- /dev/null +++ b/tools/testing/selftests/net/cmsg_so_priority.sh @@ -0,0 +1,151 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source lib.sh + +IP4=192.0.2.1/24 +TGT4=192.0.2.2 +TGT4_RAW=192.0.2.3 +IP6=2001:db8::1/64 +TGT6=2001:db8::2 +TGT6_RAW=2001:db8::3 +PORT=1234 +DELAY=4000 +TOTAL_TESTS=0 +FAILED_TESTS=0 + +if ! command -v jq &> /dev/null; then + echo "Error: jq is not installed." >&2 + exit 1 +fi + +check_result() { + ((TOTAL_TESTS++)) + if [ "$1" -ne 0 ]; then + ((FAILED_TESTS++)) + fi +} + +cleanup() +{ + cleanup_ns $NS +} + +trap cleanup EXIT + +setup_ns NS + +create_filter() { + local handle=$1 + local vlan_prio=$2 + local ip_type=$3 + local proto=$4 + local dst_ip=$5 + local ip_proto + + if [[ "$proto" == "u" ]]; then + ip_proto="udp" + elif [[ "$ip_type" == "ipv4" && "$proto" == "i" ]]; then + ip_proto="icmp" + elif [[ "$ip_type" == "ipv6" && "$proto" == "i" ]]; then + ip_proto="icmpv6" + fi + + tc -n $NS filter add dev dummy1 \ + egress pref 1 handle "$handle" proto 802.1q \ + flower vlan_prio "$vlan_prio" vlan_ethtype "$ip_type" \ + dst_ip "$dst_ip" ${ip_proto:+ip_proto $ip_proto} \ + action pass +} + +ip -n $NS link set dev lo up +ip -n $NS link add name dummy1 up type dummy + +ip -n $NS link add link dummy1 name dummy1.10 up type vlan id 10 \ + egress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 + +ip -n $NS address add $IP4 dev dummy1.10 +ip -n $NS address add $IP6 dev dummy1.10 + +ip netns exec $NS sysctl -wq net.ipv4.ping_group_range='0 2147483647' + +ip -n $NS neigh add $TGT4 lladdr 00:11:22:33:44:55 nud permanent \ + dev dummy1.10 +ip -n $NS neigh add $TGT6 lladdr 00:11:22:33:44:55 nud permanent \ + dev dummy1.10 +ip -n $NS neigh add $TGT4_RAW lladdr 00:11:22:33:44:66 nud permanent \ + dev dummy1.10 +ip -n $NS neigh add $TGT6_RAW lladdr 00:11:22:33:44:66 nud permanent \ + dev dummy1.10 + +tc -n $NS qdisc add dev dummy1 clsact + +FILTER_COUNTER=10 + +for i in 4 6; do + for proto in u i r; do + echo "Test IPV$i, prot: $proto" + for priority in {0..7}; do + if [[ $i == 4 && $proto == "r" ]]; then + TGT=$TGT4_RAW + elif [[ $i == 6 && $proto == "r" ]]; then + TGT=$TGT6_RAW + elif [ $i == 4 ]; then + TGT=$TGT4 + else + TGT=$TGT6 + fi + + handle="${FILTER_COUNTER}${priority}" + + create_filter $handle $priority ipv$i $proto $TGT + + pkts=$(tc -n $NS -j -s filter show dev dummy1 egress \ + | jq ".[] | select(.options.handle == ${handle}) | \ + .options.actions[0].stats.packets") + + if [[ $pkts == 0 ]]; then + check_result 0 + else + echo "prio $priority: expected 0, got $pkts" + check_result 1 + fi + + ip netns exec $NS ./cmsg_sender -$i -Q $priority -d "${DELAY}" \ + -p $proto $TGT $PORT + + pkts=$(tc -n $NS -j -s filter show dev dummy1 egress \ + | jq ".[] | select(.options.handle == ${handle}) | \ + .options.actions[0].stats.packets") + if [[ $pkts == 1 ]]; then + check_result 0 + else + echo "prio $priority -Q: expected 1, got $pkts" + check_result 1 + fi + + ip netns exec $NS ./cmsg_sender -$i -P $priority -d "${DELAY}" \ + -p $proto $TGT $PORT + + pkts=$(tc -n $NS -j -s filter show dev dummy1 egress \ + | jq ".[] | select(.options.handle == ${handle}) | \ + .options.actions[0].stats.packets") + if [[ $pkts == 2 ]]; then + check_result 0 + else + echo "prio $priority -P: expected 2, got $pkts" + check_result 1 + fi + done + FILTER_COUNTER=$((FILTER_COUNTER + 10)) + done +done + +if [ $FAILED_TESTS -ne 0 ]; then + echo "FAIL - $FAILED_TESTS/$TOTAL_TESTS tests failed" + exit 1 +else + echo "OK - All $TOTAL_TESTS tests passed" + exit 0 +fi + From patchwork Thu Dec 5 13:31:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna Nyiri X-Patchwork-Id: 13895343 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 351BB21A43C for ; Thu, 5 Dec 2024 13:31:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405520; cv=none; b=C4s6n2HmjdPD9ZlS2z2MehMe8fwYyJJaAbLOeeHCKKP6lP8TFgMdVEAsrGueSGn/KGPnCsMOTYGmA/hEDgG47F5f2e9goMymohWTRzPr1hTg4Txg57dG1wRIPCoDpBcqD2GEWyX6V3KaxAl29KzoE/BHcjqEivQIHq4VvPlLdjo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733405520; c=relaxed/simple; bh=pnK0GrIz1FMHcguqJbgJkCYwdu8RP/GPCfcJtIY5EoA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tY3kz7iTh6+5TlXgofL45JlfZ0yf0RM/3yJMZ0yCnUHCqhPNDjoUazcRJWz5/1po/BAiaqVqiaHudN/2QWzKeRzkGg2ffnVHnGfOEynwWvNhrbmL59KDlwJn5w9v+mfXfedd72qLgXwhQy/V8yELUvs62uApaFuFbpvlqq19yxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HdWg+htl; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HdWg+htl" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-434aa222d96so10437235e9.0 for ; Thu, 05 Dec 2024 05:31:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733405516; x=1734010316; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mwKWQ5+9YFIJcuWjk96iuJPnMhE/dQhD2eAjqiRGLCA=; b=HdWg+htlF2qrOERMNt/f+TaVTNE4B2kmoDwB4r+6kt2RXRsJwnr4ByuaDGp0ySJRRB jOHjKq7HF682eH50xLkIfkowTNzW7F4WdXnQsm/w5bgPDAgiBUV+J0SdUlNVxx2FTFdA G4yU/WAlys9N/iMtEww78C7DoXm98iu3EmujsvQOASjoiEPmwCa5xBiCDwJ7mQqvB50/ cSzVLh+AF0P/7nQSlN79Tj2v627q2+xeOd6cROI51bcIrJLogkyNz9HGDWnPC0J0W1mA iEmp7WA/omJbidlWj9LLWT/NI4G3NI58s51pA0Z+Due5JjFZA7r6uF54qGJ3f+7OV/WK kt7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733405516; x=1734010316; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mwKWQ5+9YFIJcuWjk96iuJPnMhE/dQhD2eAjqiRGLCA=; b=lgi35DJYwTVMifr8G8UIoVFQRFokpwixaA3mVx6v9HcEAuRTLtSoUxVm+/uoT+EH2T 3c8xPmoBnrTVJ9u3foa9xEog2POXeT0+EnwSGCsb8JQ00NL8yY7zP2de9anu+44nhivr zTmwa6LSkGaAm8fl2rQDaantR5kmhCYj6P+ovso/vbQX1FVZoqwhyhfzM7UaSXgivXlp KjLwbHDlO2jNJxuFFVpqhssRK3mi/UzQOPzab/kBSVZiW14BmWvO38K32P0wlKttvf6C FLW+KFzPq8jVcAu4SMSjBS6O3kOshOef9Z+DFumgkUUYPip5trE+3xEFjteb6UvU3qEJ hc6g== X-Gm-Message-State: AOJu0Yyv5wRsbxua0GRRjf3zuRGSFjPdtrIyuzm4VHVbpmkZjjoXm4WO 6vdc+oiAuke6tm+venIXM+Nw6bD+eol9isoPoY+S2vND5qTGVmSbMmcLpnl+jGk= X-Gm-Gg: ASbGncucac1U68gMFSyFTYVWIRHyDIj2ZKmrHrHkqW+iEkaSmT6z+GWYCFYEEoXClxC HdmeBeqx6bgKZb3/O6WvrJyCgkSoIuSdEKmfj/SdAOhNeUNxRzzu+0xWFVdKzate2VlV9OOD+TY Wzit21+jRTpjAbPQ9tnSX1eKws2nGa+pf3cO9OOVDNmGHsfwmFkihapjlxjY7zP3zb18f3rxrTH xlpSjNTLFP4Fc4Pcy/66ip7V63f58i40a55988UfeUtXPDioJnnWGaldQlxzQSHPHQMvRXqNcem cxgCAK+WOTiSauSTtUF7Wh5zoXhxr7HfLoOsQ5QHrlApGBiBFeVWZSNzQA== X-Google-Smtp-Source: AGHT+IHslG3Dx933n1j1dbznTUxGHlm/FLrepi+RIm6DYH8F1Dsk/tSojEJ5oN2wEAMI7Gf6h/KmSQ== X-Received: by 2002:a05:600c:1911:b0:431:5f8c:ccb9 with SMTP id 5b1f17b1804b1-434d09d1c0emr111648805e9.17.1733405516202; Thu, 05 Dec 2024 05:31:56 -0800 (PST) Received: from localhost.localdomain (20014C4E37C0C700ABF575982C3B3E76.dsl.pool.telekom.hu. [2001:4c4e:37c0:c700:abf5:7598:2c3b:3e76]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434d5280fc4sm60852465e9.24.2024.12.05.05.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 05:31:55 -0800 (PST) From: Anna Emese Nyiri To: netdev@vger.kernel.org Cc: fejes@inf.elte.hu, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, idosch@idosch.org, Anna Emese Nyiri Subject: [PATCH net-next v5 4/4] sock: Introduce SO_RCVPRIORITY socket option Date: Thu, 5 Dec 2024 14:31:12 +0100 Message-ID: <20241205133112.17903-5-annaemesenyiri@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241205133112.17903-1-annaemesenyiri@gmail.com> References: <20241205133112.17903-1-annaemesenyiri@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add new socket option, SO_RCVPRIORITY, to include SO_PRIORITY in the ancillary data returned by recvmsg(). This is analogous to the existing support for SO_RCVMARK, as implemented in commit <6fd1d51cfa253> ("net: SO_RCVMARK socket option for SO_MARK with recvmsg()"). Suggested-by: Ferenc Fejes Signed-off-by: Anna Emese Nyiri Reviewed-by: Willem de Bruijn --- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ include/net/sock.h | 4 +++- include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 8 ++++++++ net/socket.c | 11 +++++++++++ tools/include/uapi/asm-generic/socket.h | 2 ++ 9 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index 302507bf9b5d..3df5f2dd4c0f 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -148,6 +148,8 @@ #define SCM_TS_OPT_ID 81 +#define SO_RCVPRIORITY 82 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index d118d4731580..22fa8f19924a 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -159,6 +159,8 @@ #define SCM_TS_OPT_ID 81 +#define SO_RCVPRIORITY 82 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index d268d69bfcd2..aa9cd4b951fe 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -140,6 +140,8 @@ #define SCM_TS_OPT_ID 0x404C +#define SO_RCVPRIORITY 0x404D + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 113cd9f353e3..5b464a568664 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -141,6 +141,8 @@ #define SCM_TS_OPT_ID 0x005a +#define SO_RCVPRIORITY 0x005b + #if !defined(__KERNEL__) diff --git a/include/net/sock.h b/include/net/sock.h index 316a34d6c48b..d4bdd3286e03 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -953,6 +953,7 @@ enum sock_flags { SOCK_XDP, /* XDP is attached */ SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */ SOCK_RCVMARK, /* Receive SO_MARK ancillary data with packet */ + SOCK_RCVPRIORITY, /* Receive SO_PRIORITY ancillary data with packet */ }; #define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE)) @@ -2660,7 +2661,8 @@ static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk, { #define FLAGS_RECV_CMSGS ((1UL << SOCK_RXQ_OVFL) | \ (1UL << SOCK_RCVTSTAMP) | \ - (1UL << SOCK_RCVMARK)) + (1UL << SOCK_RCVMARK) |\ + (1UL << SOCK_RCVPRIORITY)) #define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \ SOF_TIMESTAMPING_RAW_HARDWARE) diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index deacfd6dd197..aa5016ff3d91 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -143,6 +143,8 @@ #define SCM_TS_OPT_ID 81 +#define SO_RCVPRIORITY 82 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) diff --git a/net/core/sock.c b/net/core/sock.c index a3d9941c1d32..f9f4d976141e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1518,6 +1518,10 @@ int sk_setsockopt(struct sock *sk, int level, int optname, case SO_RCVMARK: sock_valbool_flag(sk, SOCK_RCVMARK, valbool); break; + + case SO_RCVPRIORITY: + sock_valbool_flag(sk, SOCK_RCVPRIORITY, valbool); + break; case SO_RXQ_OVFL: sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool); @@ -1947,6 +1951,10 @@ int sk_getsockopt(struct sock *sk, int level, int optname, v.val = sock_flag(sk, SOCK_RCVMARK); break; + case SO_RCVPRIORITY: + v.val = sock_flag(sk, SOCK_RCVPRIORITY); + break; + case SO_RXQ_OVFL: v.val = sock_flag(sk, SOCK_RXQ_OVFL); break; diff --git a/net/socket.c b/net/socket.c index 9a117248f18f..79d08b734f7c 100644 --- a/net/socket.c +++ b/net/socket.c @@ -1008,12 +1008,23 @@ static void sock_recv_mark(struct msghdr *msg, struct sock *sk, } } +static void sock_recv_priority(struct msghdr *msg, struct sock *sk, + struct sk_buff *skb) +{ + if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) { + __u32 priority = skb->priority; + + put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority); + } +} + void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk, struct sk_buff *skb) { sock_recv_timestamp(msg, sk, skb); sock_recv_drops(msg, sk, skb); sock_recv_mark(msg, sk, skb); + sock_recv_priority(msg, sk, skb); } EXPORT_SYMBOL_GPL(__sock_recv_cmsgs); diff --git a/tools/include/uapi/asm-generic/socket.h b/tools/include/uapi/asm-generic/socket.h index 281df9139d2b..ffff554a5230 100644 --- a/tools/include/uapi/asm-generic/socket.h +++ b/tools/include/uapi/asm-generic/socket.h @@ -126,6 +126,8 @@ #define SCM_TS_OPT_ID 78 +#define SO_RCVPRIORITY 79 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))