Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ipv4: add ip_sock_set_tos

Add a helper to directly set the IP_TOS sockopt from kernel space without
going through a fake uaccess.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Acked-by: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Christoph Hellwig and committed by
David S. Miller
6ebf71ba 480aeb96

+28 -28
+3 -11
drivers/nvme/host/tcp.c
··· 1313 1313 { 1314 1314 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1315 1315 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1316 - int ret, opt, rcv_pdu_size; 1316 + int ret, rcv_pdu_size; 1317 1317 1318 1318 queue->ctrl = ctrl; 1319 1319 INIT_LIST_HEAD(&queue->send_list); ··· 1352 1352 sock_set_priority(queue->sock->sk, so_priority); 1353 1353 1354 1354 /* Set socket type of service */ 1355 - if (nctrl->opts->tos >= 0) { 1356 - opt = nctrl->opts->tos; 1357 - ret = kernel_setsockopt(queue->sock, SOL_IP, IP_TOS, 1358 - (char *)&opt, sizeof(opt)); 1359 - if (ret) { 1360 - dev_err(nctrl->device, 1361 - "failed to set IP_TOS sock opt %d\n", ret); 1362 - goto err_sock; 1363 - } 1364 - } 1355 + if (nctrl->opts->tos >= 0) 1356 + ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos); 1365 1357 1366 1358 queue->sock->sk->sk_allocation = GFP_ATOMIC; 1367 1359 nvme_tcp_set_queue_io_cpu(queue);
+2 -8
drivers/nvme/target/tcp.c
··· 1452 1452 sock_set_priority(sock->sk, so_priority); 1453 1453 1454 1454 /* Set socket type of service */ 1455 - if (inet->rcv_tos > 0) { 1456 - int tos = inet->rcv_tos; 1457 - 1458 - ret = kernel_setsockopt(sock, SOL_IP, IP_TOS, 1459 - (char *)&tos, sizeof(tos)); 1460 - if (ret) 1461 - return ret; 1462 - } 1455 + if (inet->rcv_tos > 0) 1456 + ip_sock_set_tos(sock->sk, inet->rcv_tos); 1463 1457 1464 1458 write_lock_bh(&sock->sk->sk_callback_lock); 1465 1459 sock->sk->sk_user_data = queue;
+2
include/net/ip.h
··· 765 765 return likely(mtu >= IPV4_MIN_MTU); 766 766 } 767 767 768 + void ip_sock_set_tos(struct sock *sk, int val); 769 + 768 770 #endif /* _IP_H */
+21 -9
net/ipv4/ip_sockglue.c
··· 560 560 return err; 561 561 } 562 562 563 + static void __ip_sock_set_tos(struct sock *sk, int val) 564 + { 565 + if (sk->sk_type == SOCK_STREAM) { 566 + val &= ~INET_ECN_MASK; 567 + val |= inet_sk(sk)->tos & INET_ECN_MASK; 568 + } 569 + if (inet_sk(sk)->tos != val) { 570 + inet_sk(sk)->tos = val; 571 + sk->sk_priority = rt_tos2priority(val); 572 + sk_dst_reset(sk); 573 + } 574 + } 575 + 576 + void ip_sock_set_tos(struct sock *sk, int val) 577 + { 578 + lock_sock(sk); 579 + __ip_sock_set_tos(sk, val); 580 + release_sock(sk); 581 + } 582 + EXPORT_SYMBOL(ip_sock_set_tos); 563 583 564 584 /* 565 585 * Socket option code for IP. This is the end of the line after any ··· 843 823 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE; 844 824 break; 845 825 case IP_TOS: /* This sets both TOS and Precedence */ 846 - if (sk->sk_type == SOCK_STREAM) { 847 - val &= ~INET_ECN_MASK; 848 - val |= inet->tos & INET_ECN_MASK; 849 - } 850 - if (inet->tos != val) { 851 - inet->tos = val; 852 - sk->sk_priority = rt_tos2priority(val); 853 - sk_dst_reset(sk); 854 - } 826 + __ip_sock_set_tos(sk, val); 855 827 break; 856 828 case IP_TTL: 857 829 if (optlen < 1)