netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: [PATCH net-next] net/ipv4/raw Optimise ipv4 raw sends when IP_HDRINCL set.
Date: Sun, 10 May 2020 16:00:41 +0000	[thread overview]
Message-ID: <6d52098964b54d848cbfd1957f093bd8@AcuMS.aculab.com> (raw)

The final routing for ipv4 packets may be done with the IP address
from the message header not that from the address buffer.
If the addresses are different FLOWI_FLAG_KNOWN_NH must be set so
that a temporary 'struct rtable' entry is created to send the message.
However the allocate + free (under RCU) is relatively expensive
and can be avoided by a quick check shows the addresses match.

Signed-off-by: David Laight <david.laight@aculab.com>
---

This makes a considerable difference when we are sending a lot
of RTP streams from a raw socket.
IP_HDRINCL has to be set so that the calculated UDP checksum is right.

 net/ipv4/raw.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index 3183413..0a81376 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -495,6 +495,27 @@ static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
 }
 
+static bool raw_msg_addr_matches(struct msghdr *msg, __be32 daddr)
+{
+	const struct iovec *iov;
+	__be32 msg_daddr;
+
+	/* Check common case of user buffer with header in the first fragment.
+	 * If we return false the message is still sent.
+	 */
+
+	if (!iter_is_iovec(&msg->msg_iter))
+		return false;
+	iov = msg->msg_iter.iov;
+	if (!iov || iov->iov_len < 20)
+		return false;
+
+	if (get_user(msg_daddr, (__be32 __user *)(iov->iov_base + 16)))
+		return false;
+
+	return daddr == msg_daddr;
+}
+
 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct inet_sock *inet = inet_sk(sk);
@@ -626,9 +647,14 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
 			   RT_SCOPE_UNIVERSE,
 			   hdrincl ? IPPROTO_RAW : sk->sk_protocol,
-			   inet_sk_flowi_flags(sk) |
-			    (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
+			   inet_sk_flowi_flags(sk),
 			   daddr, saddr, 0, 0, sk->sk_uid);
+	/* The final message routing may be done with the destination address
+	 * in the user-supplied ipv4 header. If this differs from 'daddr' then
+	 * a temporary destination table entry has to be created.
+	 */ 
+	if (hdrincl && !raw_msg_addr_matches(msg, daddr))
+		fl4.flowi4_flags |= FLOWI_FLAG_KNOWN_NH;
 
 	if (!hdrincl) {
 		rfv.msg = msg;
-- 
1.8.1.2

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


             reply	other threads:[~2020-05-10 16:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-10 16:00 David Laight [this message]
2020-05-11 20:49 ` [PATCH net-next] net/ipv4/raw Optimise ipv4 raw sends when IP_HDRINCL set David Miller
2020-05-11 21:28   ` David Laight
2020-05-11 23:09     ` David Miller
2020-05-12  8:17       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6d52098964b54d848cbfd1957f093bd8@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).