Lines Matching refs:sk

82 #define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \
87 void SOCK_DEBUG(const struct sock *sk, const char *msg, ...)
273 * @sk_napi_id: id of the last napi context to receive data for sk
529 void (*sk_state_change)(struct sock *sk);
530 void (*sk_data_ready)(struct sock *sk);
531 void (*sk_write_space)(struct sock *sk);
532 void (*sk_error_report)(struct sock *sk);
533 int (*sk_backlog_rcv)(struct sock *sk,
536 struct sk_buff* (*sk_validate_xmit_skb)(struct sock *sk,
540 void (*sk_destruct)(struct sock *sk);
565 * when sk_user_data's sk is added to the bpf's reuseport_array.
579 * @sk: socket
581 static inline bool sk_user_data_is_nocopy(const struct sock *sk)
583 return ((uintptr_t)sk->sk_user_data & SK_USER_DATA_NOCOPY);
586 #define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
593 * @sk: socket
596 * The caller must be holding sk->sk_callback_lock.
599 __locked_read_sk_user_data_with_flags(const struct sock *sk,
603 (uintptr_t)rcu_dereference_check(__sk_user_data(sk),
604 lockdep_is_held(&sk->sk_callback_lock));
618 * @sk: socket
622 __rcu_dereference_sk_user_data_with_flags(const struct sock *sk,
625 uintptr_t sk_user_data = (uintptr_t)rcu_dereference(__sk_user_data(sk));
634 #define rcu_dereference_sk_user_data(sk) \
635 __rcu_dereference_sk_user_data_with_flags(sk, 0)
636 #define __rcu_assign_sk_user_data_with_flags(sk, ptr, flags) \
642 rcu_assign_pointer(__sk_user_data((sk)), \
645 #define rcu_assign_sk_user_data(sk, ptr) \
646 __rcu_assign_sk_user_data_with_flags(sk, ptr, 0)
649 struct net *sock_net(const struct sock *sk)
651 return read_pnet(&sk->sk_net);
655 void sock_net_set(struct sock *sk, struct net *net)
657 write_pnet(&sk->sk_net, net);
671 int sk_set_peek_off(struct sock *sk, int val);
673 static inline int sk_peek_offset(const struct sock *sk, int flags)
676 return READ_ONCE(sk->sk_peek_off);
682 static inline void sk_peek_offset_bwd(struct sock *sk, int val)
684 s32 off = READ_ONCE(sk->sk_peek_off);
688 WRITE_ONCE(sk->sk_peek_off, off);
692 static inline void sk_peek_offset_fwd(struct sock *sk, int val)
694 sk_peek_offset_bwd(sk, -val);
725 static inline struct sock *sk_next(const struct sock *sk)
727 return hlist_entry_safe(sk->sk_node.next, struct sock, sk_node);
730 static inline struct sock *sk_nulls_next(const struct sock *sk)
732 return (!is_a_nulls(sk->sk_nulls_node.next)) ?
733 hlist_nulls_entry(sk->sk_nulls_node.next,
738 static inline bool sk_unhashed(const struct sock *sk)
740 return hlist_unhashed(&sk->sk_node);
743 static inline bool sk_hashed(const struct sock *sk)
745 return !sk_unhashed(sk);
753 static inline void __sk_del_node(struct sock *sk)
755 __hlist_del(&sk->sk_node);
759 static inline bool __sk_del_node_init(struct sock *sk)
761 if (sk_hashed(sk)) {
762 __sk_del_node(sk);
763 sk_node_init(&sk->sk_node);
770 when sk is ALREADY grabbed f.e. it is found in hash table
775 static __always_inline void sock_hold(struct sock *sk)
777 refcount_inc(&sk->sk_refcnt);
783 static __always_inline void __sock_put(struct sock *sk)
785 refcount_dec(&sk->sk_refcnt);
788 static inline bool sk_del_node_init(struct sock *sk)
790 bool rc = __sk_del_node_init(sk);
794 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
795 __sock_put(sk);
799 #define sk_del_node_init_rcu(sk) sk_del_node_init(sk)
801 static inline bool __sk_nulls_del_node_init_rcu(struct sock *sk)
803 if (sk_hashed(sk)) {
804 hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
810 static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)
812 bool rc = __sk_nulls_del_node_init_rcu(sk);
816 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
817 __sock_put(sk);
822 static inline void __sk_add_node(struct sock *sk, struct hlist_head *list)
824 hlist_add_head(&sk->sk_node, list);
827 static inline void sk_add_node(struct sock *sk, struct hlist_head *list)
829 sock_hold(sk);
830 __sk_add_node(sk, list);
833 static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
835 sock_hold(sk);
836 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
837 sk->sk_family == AF_INET6)
838 hlist_add_tail_rcu(&sk->sk_node, list);
840 hlist_add_head_rcu(&sk->sk_node, list);
843 static inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list)
845 sock_hold(sk);
846 hlist_add_tail_rcu(&sk->sk_node, list);
849 static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
851 hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
854 static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nulls_head *list)
856 hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list);
859 static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
861 sock_hold(sk);
862 __sk_nulls_add_node_rcu(sk, list);
865 static inline void __sk_del_bind_node(struct sock *sk)
867 __hlist_del(&sk->sk_bind_node);
870 static inline void sk_add_bind_node(struct sock *sk,
873 hlist_add_head(&sk->sk_bind_node, list);
876 static inline void __sk_del_bind2_node(struct sock *sk)
878 __hlist_del(&sk->sk_bind2_node);
881 static inline void sk_add_bind2_node(struct sock *sk, struct hlist_head *list)
883 hlist_add_head(&sk->sk_bind2_node, list);
920 static inline struct user_namespace *sk_user_ns(const struct sock *sk)
926 return sk->sk_socket->file->f_cred->user_ns;
940 SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */
971 static inline void sock_set_flag(struct sock *sk, enum sock_flags flag)
973 __set_bit(flag, &sk->sk_flags);
976 static inline void sock_reset_flag(struct sock *sk, enum sock_flags flag)
978 __clear_bit(flag, &sk->sk_flags);
981 static inline void sock_valbool_flag(struct sock *sk, enum sock_flags bit,
985 sock_set_flag(sk, bit);
987 sock_reset_flag(sk, bit);
990 static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
992 return test_bit(flag, &sk->sk_flags);
1014 static inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask)
1016 return gfp_mask | (sk->sk_allocation & __GFP_MEMALLOC);
1019 static inline void sk_acceptq_removed(struct sock *sk)
1021 WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog - 1);
1024 static inline void sk_acceptq_added(struct sock *sk)
1026 WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog + 1);
1030 * return READ_ONCE(sk->sk_ack_backlog) >= READ_ONCE(sk->sk_max_ack_backlog);
1033 static inline bool sk_acceptq_is_full(const struct sock *sk)
1035 return READ_ONCE(sk->sk_ack_backlog) > READ_ONCE(sk->sk_max_ack_backlog);
1041 static inline int sk_stream_min_wspace(const struct sock *sk)
1043 return READ_ONCE(sk->sk_wmem_queued) >> 1;
1046 static inline int sk_stream_wspace(const struct sock *sk)
1048 return READ_ONCE(sk->sk_sndbuf) - READ_ONCE(sk->sk_wmem_queued);
1051 static inline void sk_wmem_queued_add(struct sock *sk, int val)
1053 WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val);
1056 static inline void sk_forward_alloc_add(struct sock *sk, int val)
1058 /* Paired with lockless reads of sk->sk_forward_alloc */
1059 WRITE_ONCE(sk->sk_forward_alloc, sk->sk_forward_alloc + val);
1062 void sk_stream_write_space(struct sock *sk);
1065 static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
1070 if (!sk->sk_backlog.tail)
1071 WRITE_ONCE(sk->sk_backlog.head, skb);
1073 sk->sk_backlog.tail->next = skb;
1075 WRITE_ONCE(sk->sk_backlog.tail, skb);
1084 static inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit)
1086 unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
1092 static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb,
1095 if (sk_rcvqueues_full(sk, limit))
1103 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
1106 __sk_add_backlog(sk, skb);
1107 sk->sk_backlog.len += skb->truesize;
1111 int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
1113 INDIRECT_CALLABLE_DECLARE(int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb));
1114 INDIRECT_CALLABLE_DECLARE(int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb));
1116 static inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1119 return __sk_backlog_rcv(sk, skb);
1121 return INDIRECT_CALL_INET(sk->sk_backlog_rcv,
1124 sk, skb);
1127 static inline void sk_incoming_cpu_update(struct sock *sk)
1131 if (unlikely(READ_ONCE(sk->sk_incoming_cpu) != cpu))
1132 WRITE_ONCE(sk->sk_incoming_cpu, cpu);
1147 static inline void sock_rps_record_flow(const struct sock *sk)
1151 /* Reading sk->sk_rxhash might incur an expensive cache line
1156 * IPv4: inet_sk(sk)->inet_daddr
1157 * IPv6: ipv6_addr_any(&sk->sk_v6_daddr)
1161 if (sk->sk_state == TCP_ESTABLISHED) {
1165 sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
1171 static inline void sock_rps_save_rxhash(struct sock *sk,
1178 if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash))
1179 WRITE_ONCE(sk->sk_rxhash, skb->hash);
1183 static inline void sock_rps_reset_rxhash(struct sock *sk)
1187 WRITE_ONCE(sk->sk_rxhash, 0);
1206 int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
1207 int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
1208 void sk_stream_wait_close(struct sock *sk, long timeo_p);
1209 int sk_stream_error(struct sock *sk, int flags, int err);
1210 void sk_stream_kill_queues(struct sock *sk);
1211 void sk_set_memalloc(struct sock *sk);
1212 void sk_clear_memalloc(struct sock *sk);
1214 void __sk_flush_backlog(struct sock *sk);
1216 static inline bool sk_flush_backlog(struct sock *sk)
1218 if (unlikely(READ_ONCE(sk->sk_backlog.tail))) {
1219 __sk_flush_backlog(sk);
1225 int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb);
1239 static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1242 memset(sk, 0, offsetof(struct sock, sk_node.next));
1243 memset(&sk->sk_node.pprev, 0,
1251 void (*close)(struct sock *sk,
1253 int (*pre_connect)(struct sock *sk,
1256 int (*connect)(struct sock *sk,
1259 int (*disconnect)(struct sock *sk, int flags);
1261 struct sock * (*accept)(struct sock *sk, int flags, int *err,
1264 int (*ioctl)(struct sock *sk, int cmd,
1266 int (*init)(struct sock *sk);
1267 void (*destroy)(struct sock *sk);
1268 void (*shutdown)(struct sock *sk, int how);
1269 int (*setsockopt)(struct sock *sk, int level,
1272 int (*getsockopt)(struct sock *sk, int level,
1275 void (*keepalive)(struct sock *sk, int valbool);
1277 int (*compat_ioctl)(struct sock *sk,
1280 int (*sendmsg)(struct sock *sk, struct msghdr *msg,
1282 int (*recvmsg)(struct sock *sk, struct msghdr *msg,
1285 int (*bind)(struct sock *sk,
1287 int (*bind_add)(struct sock *sk,
1290 int (*backlog_rcv) (struct sock *sk,
1295 void (*release_cb)(struct sock *sk);
1297 /* Keeping track of sk's, looking them up, and port selection methods. */
1298 int (*hash)(struct sock *sk);
1299 void (*unhash)(struct sock *sk);
1300 void (*rehash)(struct sock *sk);
1301 int (*get_port)(struct sock *sk, unsigned short snum);
1302 void (*put_port)(struct sock *sk);
1304 int (*psock_update_sk_prot)(struct sock *sk,
1315 int (*forward_alloc_get)(const struct sock *sk);
1318 bool (*stream_memory_free)(const struct sock *sk, int wake);
1319 bool (*sock_is_readable)(struct sock *sk);
1321 void (*enter_memory_pressure)(struct sock *sk);
1322 void (*leave_memory_pressure)(struct sock *sk);
1369 int (*diag_destroy)(struct sock *sk, int err);
1376 INDIRECT_CALLABLE_DECLARE(bool tcp_stream_memory_free(const struct sock *sk, int wake));
1378 static inline int sk_forward_alloc_get(const struct sock *sk)
1381 if (sk->sk_prot->forward_alloc_get)
1382 return sk->sk_prot->forward_alloc_get(sk);
1384 return READ_ONCE(sk->sk_forward_alloc);
1387 static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
1389 if (READ_ONCE(sk->sk_wmem_queued) >= READ_ONCE(sk->sk_sndbuf))
1392 return sk->sk_prot->stream_memory_free ?
1393 INDIRECT_CALL_INET_1(sk->sk_prot->stream_memory_free,
1394 tcp_stream_memory_free, sk, wake) : true;
1397 static inline bool sk_stream_memory_free(const struct sock *sk)
1399 return __sk_stream_memory_free(sk, 0);
1402 static inline bool __sk_stream_is_writeable(const struct sock *sk, int wake)
1404 return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) &&
1405 __sk_stream_memory_free(sk, wake);
1408 static inline bool sk_stream_is_writeable(const struct sock *sk)
1410 return __sk_stream_is_writeable(sk, 0);
1413 static inline int sk_under_cgroup_hierarchy(struct sock *sk,
1417 return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data),
1424 static inline bool sk_has_memory_pressure(const struct sock *sk)
1426 return sk->sk_prot->memory_pressure != NULL;
1429 static inline bool sk_under_global_memory_pressure(const struct sock *sk)
1431 return sk->sk_prot->memory_pressure &&
1432 !!READ_ONCE(*sk->sk_prot->memory_pressure);
1435 static inline bool sk_under_memory_pressure(const struct sock *sk)
1437 if (!sk->sk_prot->memory_pressure)
1440 if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
1441 mem_cgroup_under_socket_pressure(sk->sk_memcg))
1444 return !!READ_ONCE(*sk->sk_prot->memory_pressure);
1454 sk_memory_allocated(const struct sock *sk)
1456 return proto_memory_allocated(sk->sk_prot);
1463 sk_memory_allocated_add(struct sock *sk, int amt)
1468 local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1470 __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1471 atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1477 sk_memory_allocated_sub(struct sock *sk, int amt)
1482 local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1484 __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1485 atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1492 static inline void sk_sockets_allocated_dec(struct sock *sk)
1494 percpu_counter_add_batch(sk->sk_prot->sockets_allocated, -1,
1498 static inline void sk_sockets_allocated_inc(struct sock *sk)
1500 percpu_counter_add_batch(sk->sk_prot->sockets_allocated, 1,
1505 sk_sockets_allocated_read_positive(struct sock *sk)
1507 return percpu_counter_read_positive(sk->sk_prot->sockets_allocated);
1560 static inline int __sk_prot_rehash(struct sock *sk)
1562 sk->sk_prot->unhash(sk);
1563 return sk->sk_prot->hash(sk);
1597 int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind);
1598 int __sk_mem_schedule(struct sock *sk, int size, int kind);
1599 void __sk_mem_reduce_allocated(struct sock *sk, int amount);
1600 void __sk_mem_reclaim(struct sock *sk, int amount);
1606 static inline long sk_prot_mem_limits(const struct sock *sk, int index)
1608 return READ_ONCE(sk->sk_prot->sysctl_mem[index]);
1616 static inline bool sk_has_account(struct sock *sk)
1619 return !!sk->sk_prot->memory_allocated;
1622 static inline bool sk_wmem_schedule(struct sock *sk, int size)
1626 if (!sk_has_account(sk))
1628 delta = size - sk->sk_forward_alloc;
1629 return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_SEND);
1633 sk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size)
1637 if (!sk_has_account(sk))
1639 delta = size - sk->sk_forward_alloc;
1640 return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_RECV) ||
1644 static inline int sk_unused_reserved_mem(const struct sock *sk)
1648 if (likely(!sk->sk_reserved_mem))
1651 unused_mem = sk->sk_reserved_mem - sk->sk_wmem_queued -
1652 atomic_read(&sk->sk_rmem_alloc);
1657 static inline void sk_mem_reclaim(struct sock *sk)
1661 if (!sk_has_account(sk))
1664 reclaimable = sk->sk_forward_alloc - sk_unused_reserved_mem(sk);
1667 __sk_mem_reclaim(sk, reclaimable);
1670 static inline void sk_mem_reclaim_final(struct sock *sk)
1672 sk->sk_reserved_mem = 0;
1673 sk_mem_reclaim(sk);
1676 static inline void sk_mem_charge(struct sock *sk, int size)
1678 if (!sk_has_account(sk))
1680 sk_forward_alloc_add(sk, -size);
1683 static inline void sk_mem_uncharge(struct sock *sk, int size)
1685 if (!sk_has_account(sk))
1687 sk_forward_alloc_add(sk, size);
1688 sk_mem_reclaim(sk);
1698 #define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
1700 sk->sk_lock.owned = 0; \
1701 init_waitqueue_head(&sk->sk_lock.wq); \
1702 spin_lock_init(&(sk)->sk_lock.slock); \
1703 debug_check_no_locks_freed((void *)&(sk)->sk_lock, \
1704 sizeof((sk)->sk_lock)); \
1705 lockdep_set_class_and_name(&(sk)->sk_lock.slock, \
1707 lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \
1710 static inline bool lockdep_sock_is_held(const struct sock *sk)
1712 return lockdep_is_held(&sk->sk_lock) ||
1713 lockdep_is_held(&sk->sk_lock.slock);
1716 void lock_sock_nested(struct sock *sk, int subclass);
1718 static inline void lock_sock(struct sock *sk)
1720 lock_sock_nested(sk, 0);
1723 void __lock_sock(struct sock *sk);
1724 void __release_sock(struct sock *sk);
1725 void release_sock(struct sock *sk);
1734 bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock);
1738 * @sk: socket
1749 static inline bool lock_sock_fast(struct sock *sk)
1752 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1754 return __lock_sock_fast(sk);
1758 static inline bool lock_sock_fast_nested(struct sock *sk)
1760 mutex_acquire(&sk->sk_lock.dep_map, SINGLE_DEPTH_NESTING, 0, _RET_IP_);
1762 return __lock_sock_fast(sk);
1767 * @sk: socket
1773 static inline void unlock_sock_fast(struct sock *sk, bool slow)
1774 __releases(&sk->sk_lock.slock)
1777 release_sock(sk);
1778 __release(&sk->sk_lock.slock);
1780 mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
1781 spin_unlock_bh(&sk->sk_lock.slock);
1785 void sockopt_lock_sock(struct sock *sk);
1786 void sockopt_release_sock(struct sock *sk);
1804 static inline void sock_owned_by_me(const struct sock *sk)
1807 WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks);
1811 static inline bool sock_owned_by_user(const struct sock *sk)
1813 sock_owned_by_me(sk);
1814 return sk->sk_lock.owned;
1817 static inline bool sock_owned_by_user_nocheck(const struct sock *sk)
1819 return sk->sk_lock.owned;
1822 static inline void sock_release_ownership(struct sock *sk)
1824 if (sock_owned_by_user_nocheck(sk)) {
1825 sk->sk_lock.owned = 0;
1828 mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
1835 struct sock *sk = (struct sock *)csk;
1837 return !sock_owned_by_user_nocheck(sk) &&
1838 !spin_is_locked(&sk->sk_lock.slock);
1843 void sk_free(struct sock *sk);
1844 void sk_destruct(struct sock *sk);
1845 struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority);
1846 void sk_free_unlock_clone(struct sock *sk);
1848 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
1852 struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
1864 int sk_setsockopt(struct sock *sk, int level, int optname,
1869 int sk_getsockopt(struct sock *sk, int level, int optname,
1875 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1879 static inline struct sk_buff *sock_alloc_send_skb(struct sock *sk,
1883 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
1886 void *sock_kmalloc(struct sock *sk, int size, gfp_t priority);
1887 void sock_kfree_s(struct sock *sk, void *mem, int size);
1888 void sock_kzfree_s(struct sock *sk, void *mem, int size);
1889 void sk_send_sigurg(struct sock *sk);
1891 static inline void sock_replace_proto(struct sock *sk, struct proto *proto)
1893 if (sk->sk_socket)
1894 clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
1895 WRITE_ONCE(sk->sk_prot, proto);
1905 const struct sock *sk)
1908 .tsflags = READ_ONCE(sk->sk_tsflags)
1912 int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg,
1914 int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
1930 int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len);
1946 void sk_common_release(struct sock *sk);
1953 void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid);
1958 void sock_init_data(struct socket *sock, struct sock *sk);
1986 static inline void sock_put(struct sock *sk)
1988 if (refcount_dec_and_test(&sk->sk_refcnt))
1989 sk_free(sk);
1994 void sock_gen_put(struct sock *sk);
1996 int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested,
1998 static inline int sk_receive_skb(struct sock *sk, struct sk_buff *skb,
2001 return __sk_receive_skb(sk, skb, nested, 1, true);
2004 static inline void sk_tx_queue_set(struct sock *sk, int tx_queue)
2012 WRITE_ONCE(sk->sk_tx_queue_mapping, tx_queue);
2017 static inline void sk_tx_queue_clear(struct sock *sk)
2022 WRITE_ONCE(sk->sk_tx_queue_mapping, NO_QUEUE_MAPPING);
2025 static inline int sk_tx_queue_get(const struct sock *sk)
2027 if (sk) {
2031 int val = READ_ONCE(sk->sk_tx_queue_mapping);
2039 static inline void __sk_rx_queue_set(struct sock *sk,
2048 unlikely(READ_ONCE(sk->sk_rx_queue_mapping) != rx_queue))
2049 WRITE_ONCE(sk->sk_rx_queue_mapping, rx_queue);
2054 static inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb)
2056 __sk_rx_queue_set(sk, skb, true);
2059 static inline void sk_rx_queue_update(struct sock *sk, const struct sk_buff *skb)
2061 __sk_rx_queue_set(sk, skb, false);
2064 static inline void sk_rx_queue_clear(struct sock *sk)
2067 WRITE_ONCE(sk->sk_rx_queue_mapping, NO_QUEUE_MAPPING);
2071 static inline int sk_rx_queue_get(const struct sock *sk)
2074 if (sk) {
2075 int res = READ_ONCE(sk->sk_rx_queue_mapping);
2085 static inline void sk_set_socket(struct sock *sk, struct socket *sock)
2087 sk->sk_socket = sock;
2090 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
2093 return &rcu_dereference_raw(sk->sk_wq)->wait;
2102 static inline void sock_orphan(struct sock *sk)
2104 write_lock_bh(&sk->sk_callback_lock);
2105 sock_set_flag(sk, SOCK_DEAD);
2106 sk_set_socket(sk, NULL);
2107 sk->sk_wq = NULL;
2108 write_unlock_bh(&sk->sk_callback_lock);
2111 static inline void sock_graft(struct sock *sk, struct socket *parent)
2113 WARN_ON(parent->sk);
2114 write_lock_bh(&sk->sk_callback_lock);
2115 rcu_assign_pointer(sk->sk_wq, &parent->wq);
2116 parent->sk = sk;
2117 sk_set_socket(sk, parent);
2118 sk->sk_uid = SOCK_INODE(parent)->i_uid;
2119 security_sock_graft(sk, parent);
2120 write_unlock_bh(&sk->sk_callback_lock);
2123 kuid_t sock_i_uid(struct sock *sk);
2124 unsigned long __sock_i_ino(struct sock *sk);
2125 unsigned long sock_i_ino(struct sock *sk);
2127 static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
2129 return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
2139 static inline void sk_set_txhash(struct sock *sk)
2142 WRITE_ONCE(sk->sk_txhash, net_tx_rndhash());
2145 static inline bool sk_rethink_txhash(struct sock *sk)
2147 if (sk->sk_txhash && sk->sk_txrehash == SOCK_TXREHASH_ENABLED) {
2148 sk_set_txhash(sk);
2155 __sk_dst_get(const struct sock *sk)
2157 return rcu_dereference_check(sk->sk_dst_cache,
2158 lockdep_sock_is_held(sk));
2162 sk_dst_get(const struct sock *sk)
2167 dst = rcu_dereference(sk->sk_dst_cache);
2174 static inline void __dst_negative_advice(struct sock *sk)
2176 struct dst_entry *ndst, *dst = __sk_dst_get(sk);
2182 rcu_assign_pointer(sk->sk_dst_cache, ndst);
2183 sk_tx_queue_clear(sk);
2184 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
2189 static inline void dst_negative_advice(struct sock *sk)
2191 sk_rethink_txhash(sk);
2192 __dst_negative_advice(sk);
2196 __sk_dst_set(struct sock *sk, struct dst_entry *dst)
2200 sk_tx_queue_clear(sk);
2201 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
2202 old_dst = rcu_dereference_protected(sk->sk_dst_cache,
2203 lockdep_sock_is_held(sk));
2204 rcu_assign_pointer(sk->sk_dst_cache, dst);
2209 sk_dst_set(struct sock *sk, struct dst_entry *dst)
2213 sk_tx_queue_clear(sk);
2214 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
2215 old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst);
2220 __sk_dst_reset(struct sock *sk)
2222 __sk_dst_set(sk, NULL);
2226 sk_dst_reset(struct sock *sk)
2228 sk_dst_set(sk, NULL);
2231 struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
2233 struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
2235 static inline void sk_dst_confirm(struct sock *sk)
2237 if (!READ_ONCE(sk->sk_dst_pending_confirm))
2238 WRITE_ONCE(sk->sk_dst_pending_confirm, 1);
2244 struct sock *sk = skb->sk;
2246 if (sk && READ_ONCE(sk->sk_dst_pending_confirm))
2247 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
2252 bool sk_mc_loop(struct sock *sk);
2254 static inline bool sk_can_gso(const struct sock *sk)
2256 return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);
2259 void sk_setup_caps(struct sock *sk, struct dst_entry *dst);
2261 static inline void sk_gso_disable(struct sock *sk)
2263 sk->sk_gso_disabled = 1;
2264 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
2267 static inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb,
2276 } else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
2285 static inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb,
2290 err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy),
2298 static inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *from,
2305 err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off,
2311 sk_wmem_queued_add(sk, copy);
2312 sk_mem_charge(sk, copy);
2318 * @sk: socket
2322 static inline int sk_wmem_alloc_get(const struct sock *sk)
2324 return refcount_read(&sk->sk_wmem_alloc) - 1;
2329 * @sk: socket
2333 static inline int sk_rmem_alloc_get(const struct sock *sk)
2335 return atomic_read(&sk->sk_rmem_alloc);
2340 * @sk: socket
2344 static inline bool sk_has_allocations(const struct sock *sk)
2346 return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
2368 * wq = rcu_dereference(sk->sk_wq);
2407 static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
2410 u32 txhash = READ_ONCE(sk->sk_txhash);
2418 void skb_set_owner_w(struct sk_buff *skb, struct sock *sk);
2428 static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
2431 skb->sk = sk;
2433 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
2434 sk_mem_charge(sk, skb->truesize);
2437 static inline __must_check bool skb_set_owner_sk_safe(struct sk_buff *skb, struct sock *sk)
2439 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
2442 skb->sk = sk;
2448 static inline struct sk_buff *skb_clone_and_charge_r(struct sk_buff *skb, struct sock *sk)
2450 skb = skb_clone(skb, sk_gfp_mask(sk, GFP_ATOMIC));
2452 if (sk_rmem_schedule(sk, skb, skb->truesize)) {
2453 skb_set_owner_r(skb, sk);
2470 void sk_reset_timer(struct sock *sk, struct timer_list *timer,
2473 void sk_stop_timer(struct sock *sk, struct timer_list *timer);
2475 void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer);
2477 int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
2479 void (*destructor)(struct sock *sk,
2481 int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
2483 int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
2486 static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2488 return sock_queue_rcv_skb_reason(sk, skb, NULL);
2491 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
2492 struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
2498 static inline int sock_error(struct sock *sk)
2505 if (likely(data_race(!sk->sk_err)))
2508 err = xchg(&sk->sk_err, 0);
2512 void sk_error_report(struct sock *sk);
2514 static inline unsigned long sock_wspace(struct sock *sk)
2518 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
2519 amt = sk->sk_sndbuf - refcount_read(&sk->sk_wmem_alloc);
2527 * We use sk->sk_wq_raw, from contexts knowing this
2530 static inline void sk_set_bit(int nr, struct sock *sk)
2533 !sock_flag(sk, SOCK_FASYNC))
2536 set_bit(nr, &sk->sk_wq_raw->flags);
2539 static inline void sk_clear_bit(int nr, struct sock *sk)
2542 !sock_flag(sk, SOCK_FASYNC))
2545 clear_bit(nr, &sk->sk_wq_raw->flags);
2548 static inline void sk_wake_async(const struct sock *sk, int how, int band)
2550 if (sock_flag(sk, SOCK_FASYNC)) {
2552 sock_wake_async(rcu_dereference(sk->sk_wq), how, band);
2567 static inline void sk_stream_moderate_sndbuf(struct sock *sk)
2571 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
2574 val = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
2575 val = max_t(u32, val, sk_unused_reserved_mem(sk));
2577 WRITE_ONCE(sk->sk_sndbuf, max_t(u32, val, SOCK_MIN_SNDBUF));
2582 * @sk: socket
2596 static inline struct page_frag *sk_page_frag(struct sock *sk)
2598 if (sk->sk_use_task_frag)
2601 return &sk->sk_frag;
2604 bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag);
2609 static inline bool sock_writeable(const struct sock *sk)
2611 return refcount_read(&sk->sk_wmem_alloc) < (READ_ONCE(sk->sk_sndbuf) >> 1);
2624 static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
2626 return noblock ? 0 : sk->sk_rcvtimeo;
2629 static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
2631 return noblock ? 0 : sk->sk_sndtimeo;
2634 static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
2636 int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len);
2667 sock_skb_set_dropcount(const struct sock *sk, struct sk_buff *skb)
2669 SOCK_SKB_CB(skb)->dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ?
2670 atomic_read(&sk->sk_drops) : 0;
2673 static inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb)
2677 atomic_add(segs, &sk->sk_drops);
2680 static inline ktime_t sock_read_timestamp(struct sock *sk)
2687 seq = read_seqbegin(&sk->sk_stamp_seq);
2688 kt = sk->sk_stamp;
2689 } while (read_seqretry(&sk->sk_stamp_seq, seq));
2693 return READ_ONCE(sk->sk_stamp);
2697 static inline void sock_write_timestamp(struct sock *sk, ktime_t kt)
2700 write_seqlock(&sk->sk_stamp_seq);
2701 sk->sk_stamp = kt;
2702 write_sequnlock(&sk->sk_stamp_seq);
2704 WRITE_ONCE(sk->sk_stamp, kt);
2708 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
2710 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
2714 sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
2717 u32 tsflags = READ_ONCE(sk->sk_tsflags);
2725 if (sock_flag(sk, SOCK_RCVTSTAMP) ||
2730 __sock_recv_timestamp(msg, sk, skb);
2732 sock_write_timestamp(sk, kt);
2734 if (sock_flag(sk, SOCK_WIFI_STATUS) && skb_wifi_acked_valid(skb))
2735 __sock_recv_wifi_status(msg, sk, skb);
2738 void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
2742 static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
2751 if (sk->sk_flags & FLAGS_RECV_CMSGS ||
2752 READ_ONCE(sk->sk_tsflags) & TSFLAGS_ANY)
2753 __sock_recv_cmsgs(msg, sk, skb);
2754 else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
2755 sock_write_timestamp(sk, skb->tstamp);
2756 else if (unlikely(sock_read_timestamp(sk) == SK_DEFAULT_STAMP))
2757 sock_write_timestamp(sk, 0);
2764 * @sk: socket sending this packet
2771 static inline void _sock_tx_timestamp(struct sock *sk, __u16 tsflags,
2778 *tskey = atomic_inc_return(&sk->sk_tskey) - 1;
2780 if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS)))
2784 static inline void sock_tx_timestamp(struct sock *sk, __u16 tsflags,
2787 _sock_tx_timestamp(sk, tsflags, tx_flags, NULL);
2792 _sock_tx_timestamp(skb->sk, tsflags, &skb_shinfo(skb)->tx_flags,
2796 static inline bool sk_is_inet(const struct sock *sk)
2798 int family = READ_ONCE(sk->sk_family);
2803 static inline bool sk_is_tcp(const struct sock *sk)
2805 return sk_is_inet(sk) &&
2806 sk->sk_type == SOCK_STREAM &&
2807 sk->sk_protocol == IPPROTO_TCP;
2810 static inline bool sk_is_udp(const struct sock *sk)
2812 return sk_is_inet(sk) &&
2813 sk->sk_type == SOCK_DGRAM &&
2814 sk->sk_protocol == IPPROTO_UDP;
2817 static inline bool sk_is_stream_unix(const struct sock *sk)
2819 return sk->sk_family == AF_UNIX && sk->sk_type == SOCK_STREAM;
2824 * @sk: socket to eat this skb from
2830 static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb)
2832 __skb_unlink(skb, &sk->sk_receive_queue);
2849 static inline bool sk_fullsock(const struct sock *sk)
2851 return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
2855 sk_is_refcounted(struct sock *sk)
2857 /* Only full sockets have sk->sk_flags. */
2858 return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE);
2870 if (skb->sk) {
2871 struct sock *sk = skb->sk;
2876 *refcounted = sk_is_refcounted(sk);
2878 skb->sk = NULL;
2879 return sk;
2894 struct sock *sk = skb->sk;
2896 if (sk && sk_fullsock(sk) && sk->sk_validate_xmit_skb) {
2897 skb = sk->sk_validate_xmit_skb(sk, dev, skb);
2913 static inline bool sk_listener(const struct sock *sk)
2915 return (1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV);
2918 void sock_enable_timestamp(struct sock *sk, enum sock_flags flag);
2919 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
2922 bool sk_ns_capable(const struct sock *sk,
2924 bool sk_capable(const struct sock *sk, int cap);
2925 bool sk_net_capable(const struct sock *sk, int cap);
2927 void sk_get_meminfo(const struct sock *sk, u32 *meminfo);
2951 static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto)
2955 return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_wmem_offset));
2960 static inline int sk_get_rmem0(const struct sock *sk, const struct proto *proto)
2964 return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_rmem_offset));
2973 static inline void sk_pacing_shift_update(struct sock *sk, int val)
2975 if (!sk || !sk_fullsock(sk) || READ_ONCE(sk->sk_pacing_shift) == val)
2977 WRITE_ONCE(sk->sk_pacing_shift, val);
2985 static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
2987 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
2993 mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif);
3000 void sock_def_readable(struct sock *sk);
3002 int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk);
3003 void sock_set_timestamp(struct sock *sk, int optname, bool valbool);
3004 int sock_set_timestamping(struct sock *sk, int optname,
3007 void sock_enable_timestamps(struct sock *sk);
3008 void sock_no_linger(struct sock *sk);
3009 void sock_set_keepalive(struct sock *sk);
3010 void sock_set_priority(struct sock *sk, u32 priority);
3011 void sock_set_rcvbuf(struct sock *sk, int val);
3012 void sock_set_mark(struct sock *sk, u32 val);
3013 void sock_set_reuseaddr(struct sock *sk);
3014 void sock_set_reuseport(struct sock *sk);
3015 void sock_set_sndtimeo(struct sock *sk, s64 secs);
3017 int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len);
3023 int sock_ioctl_inout(struct sock *sk, unsigned int cmd,
3025 int sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
3026 static inline bool sk_is_readable(struct sock *sk)
3028 if (sk->sk_prot->sock_is_readable)
3029 return sk->sk_prot->sock_is_readable(sk);