Skip to content

Commit

Permalink
[NET] NETNS: Omit sock->sk_net without CONFIG_NET_NS.
Browse files Browse the repository at this point in the history
Introduce per-sock inlines: sock_net(), sock_net_set()
and per-inet_timewait_sock inlines: twsk_net(), twsk_net_set().
Without CONFIG_NET_NS, no namespace other than &init_net exists.
Let's explicitly define them to help compiler optimizations.

Signed-off-by: YOSHIFUJI Hideaki <[email protected]>
  • Loading branch information
yoshfuji committed Mar 25, 2008
1 parent c346dca commit 3b1e0a6
Show file tree
Hide file tree
Showing 69 changed files with 253 additions and 215 deletions.
4 changes: 2 additions & 2 deletions include/linux/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,15 @@ static inline struct raw6_sock *raw6_sk(const struct sock *sk)
#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */

#define INET6_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
((__sk)->sk_family == AF_INET6) && \
ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))

#define INET6_TW_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif) \
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
(*((__portpair *)&(inet_twsk(__sk)->tw_dport)) == (__ports)) && \
((__sk)->sk_family == PF_INET6) && \
(ipv6_addr_equal(&inet6_twsk(__sk)->tw_v6_daddr, (__saddr))) && \
Expand Down
8 changes: 4 additions & 4 deletions include/net/inet_hashtables.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,25 @@ typedef __u64 __bitwise __addrpair;
((__force __u64)(__be32)(__saddr)));
#endif /* __BIG_ENDIAN */
#define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
((*((__addrpair *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
#define INET_TW_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
((*((__addrpair *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
#else /* 32-bit arch */
#define INET_ADDR_COOKIE(__name, __saddr, __daddr)
#define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif) \
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
(inet_sk(__sk)->daddr == (__saddr)) && \
(inet_sk(__sk)->rcv_saddr == (__daddr)) && \
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
#define INET_TW_MATCH(__sk, __net, __hash,__cookie, __saddr, __daddr, __ports, __dif) \
(((__sk)->sk_hash == (__hash)) && ((__sk)->sk_net == (__net)) && \
(((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
(inet_twsk(__sk)->tw_daddr == (__saddr)) && \
(inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
Expand Down
18 changes: 18 additions & 0 deletions include/net/inet_timewait_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,22 @@ extern void inet_twsk_schedule(struct inet_timewait_sock *tw,
const int timeo, const int timewait_len);
extern void inet_twsk_deschedule(struct inet_timewait_sock *tw,
struct inet_timewait_death_row *twdr);

static inline
struct net *twsk_net(const struct inet_timewait_sock *twsk)
{
#ifdef CONFIG_NET_NS
return twsk->tw_net;
#else
return &init_net;
#endif
}

static inline
void twsk_net_set(struct inet_timewait_sock *twsk, const struct net *net)
{
#ifdef CONFIG_NET_NS
twsk->tw_net = net;
#endif
}
#endif /* _INET_TIMEWAIT_SOCK_ */
4 changes: 2 additions & 2 deletions include/net/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static inline int ip_route_connect(struct rtable **rp, __be32 dst,
.dport = dport } } };

int err;
struct net *net = sk->sk_net;
struct net *net = sock_net(sk);
if (!dst || !src) {
err = __ip_route_output_key(net, rp, &fl);
if (err)
Expand Down Expand Up @@ -188,7 +188,7 @@ static inline int ip_route_newports(struct rtable **rp, u8 protocol,
ip_rt_put(*rp);
*rp = NULL;
security_sk_classify_flow(sk, &fl);
return ip_route_output_flow(sk->sk_net, rp, &fl, sk, 0);
return ip_route_output_flow(sock_net(sk), rp, &fl, sk, 0);
}
return 0;
}
Expand Down
24 changes: 22 additions & 2 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ struct sock_common {
atomic_t skc_refcnt;
unsigned int skc_hash;
struct proto *skc_prot;
#ifdef CONFIG_NET_NS
struct net *skc_net;
#endif
};

/**
Expand Down Expand Up @@ -1345,6 +1347,24 @@ static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb, int copied_e
}
#endif

static inline
struct net *sock_net(const struct sock *sk)
{
#ifdef CONFIG_NET_NS
return sk->sk_net;
#else
return &init_net;
#endif
}

static inline
void sock_net_set(struct sock *sk, const struct net *net)
{
#ifdef CONFIG_NET_NS
sk->sk_net = net;
#endif
}

/*
* Kernel sockets, f.e. rtnl or icmp_socket, are a part of a namespace.
* They should not hold a referrence to a namespace in order to allow
Expand All @@ -1353,8 +1373,8 @@ static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb, int copied_e
*/
static inline void sk_change_net(struct sock *sk, struct net *net)
{
put_net(sk->sk_net);
sk->sk_net = net;
put_net(sock_net(sk));
sock_net_set(sk, net);
}

extern void sock_enable_timestamp(struct sock *sk);
Expand Down
2 changes: 1 addition & 1 deletion net/atm/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ static int svc_accept(struct socket *sock,struct socket *newsock,int flags)

lock_sock(sk);

error = svc_create(sk->sk_net, newsock,0);
error = svc_create(sock_net(sk), newsock,0);
if (error)
goto out;

Expand Down
2 changes: 1 addition & 1 deletion net/ax25/af_ax25.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ struct sock *ax25_make_new(struct sock *osk, struct ax25_dev *ax25_dev)
struct sock *sk;
ax25_cb *ax25, *oax25;

sk = sk_alloc(osk->sk_net, PF_AX25, GFP_ATOMIC, osk->sk_prot);
sk = sk_alloc(sock_net(osk), PF_AX25, GFP_ATOMIC, osk->sk_prot);
if (sk == NULL)
return NULL;

Expand Down
2 changes: 1 addition & 1 deletion net/bluetooth/l2cap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hd
goto response;
}

sk = l2cap_sock_alloc(parent->sk_net, NULL, BTPROTO_L2CAP, GFP_ATOMIC);
sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
if (!sk)
goto response;

Expand Down
2 changes: 1 addition & 1 deletion net/bluetooth/rfcomm/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc *
goto done;
}

sk = rfcomm_sock_alloc(parent->sk_net, NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
if (!sk)
goto done;

Expand Down
2 changes: 1 addition & 1 deletion net/bluetooth/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ static void sco_conn_ready(struct sco_conn *conn)

bh_lock_sock(parent);

sk = sco_sock_alloc(parent->sk_net, NULL, BTPROTO_SCO, GFP_ATOMIC);
sk = sco_sock_alloc(sock_net(parent), NULL, BTPROTO_SCO, GFP_ATOMIC);
if (!sk) {
bh_unlock_sock(parent);
goto done;
Expand Down
4 changes: 2 additions & 2 deletions net/bridge/br_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void br_ifinfo_notify(int event, struct net_bridge_port *port)
*/
static int br_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct net_device *dev;
int idx;

Expand Down Expand Up @@ -140,7 +140,7 @@ static int br_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
*/
static int br_rtm_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct ifinfomsg *ifm;
struct nlattr *protinfo;
struct net_device *dev;
Expand Down
6 changes: 3 additions & 3 deletions net/core/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,

static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct fib_rule_hdr *frh = nlmsg_data(nlh);
struct fib_rules_ops *ops = NULL;
struct fib_rule *rule, *r, *last = NULL;
Expand Down Expand Up @@ -352,7 +352,7 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)

static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct fib_rule_hdr *frh = nlmsg_data(nlh);
struct fib_rules_ops *ops = NULL;
struct fib_rule *rule, *tmp;
Expand Down Expand Up @@ -534,7 +534,7 @@ static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,

static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct fib_rules_ops *ops;
int idx = 0, family;

Expand Down
10 changes: 5 additions & 5 deletions net/core/neighbour.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ int neigh_table_clear(struct neigh_table *tbl)

static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct ndmsg *ndm;
struct nlattr *dst_attr;
struct neigh_table *tbl;
Expand Down Expand Up @@ -1544,7 +1544,7 @@ static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)

static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct ndmsg *ndm;
struct nlattr *tb[NDA_MAX+1];
struct neigh_table *tbl;
Expand Down Expand Up @@ -1812,7 +1812,7 @@ static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {

static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct neigh_table *tbl;
struct ndtmsg *ndtmsg;
struct nlattr *tb[NDTA_MAX+1];
Expand Down Expand Up @@ -1937,7 +1937,7 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)

static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
int family, tidx, nidx = 0;
int tbl_skip = cb->args[0];
int neigh_skip = cb->args[1];
Expand Down Expand Up @@ -2037,7 +2037,7 @@ static void neigh_update_notify(struct neighbour *neigh)
static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
struct netlink_callback *cb)
{
struct net * net = skb->sk->sk_net;
struct net * net = sock_net(skb->sk);
struct neighbour *n;
int rc, h, s_h = cb->args[1];
int idx, s_idx = idx = cb->args[2];
Expand Down
12 changes: 6 additions & 6 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,

static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
int idx;
int s_idx = cb->args[0];
struct net_device *dev;
Expand Down Expand Up @@ -879,7 +879,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,

static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct ifinfomsg *ifm;
struct net_device *dev;
int err;
Expand Down Expand Up @@ -921,7 +921,7 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)

static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
const struct rtnl_link_ops *ops;
struct net_device *dev;
struct ifinfomsg *ifm;
Expand Down Expand Up @@ -1000,7 +1000,7 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname,

static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
const struct rtnl_link_ops *ops;
struct net_device *dev;
struct ifinfomsg *ifm;
Expand Down Expand Up @@ -1132,7 +1132,7 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)

static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
struct ifinfomsg *ifm;
struct nlattr *tb[IFLA_MAX+1];
struct net_device *dev = NULL;
Expand Down Expand Up @@ -1227,7 +1227,7 @@ static int rtattr_max;

static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
{
struct net *net = skb->sk->sk_net;
struct net *net = sock_net(skb->sk);
rtnl_doit_func doit;
int sz_idx, kind;
int min_len;
Expand Down
10 changes: 5 additions & 5 deletions net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
{
int ret = -ENOPROTOOPT;
#ifdef CONFIG_NETDEVICES
struct net *net = sk->sk_net;
struct net *net = sock_net(sk);
char devname[IFNAMSIZ];
int index;

Expand Down Expand Up @@ -958,7 +958,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
*/
sk->sk_prot = sk->sk_prot_creator = prot;
sock_lock_init(sk);
sk->sk_net = get_net(net);
sock_net_set(sk, get_net(net));
}

return sk;
Expand All @@ -983,7 +983,7 @@ void sk_free(struct sock *sk)
printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
__func__, atomic_read(&sk->sk_omem_alloc));

put_net(sk->sk_net);
put_net(sock_net(sk));
sk_prot_free(sk->sk_prot_creator, sk);
}

Expand All @@ -1001,7 +1001,7 @@ void sk_release_kernel(struct sock *sk)

sock_hold(sk);
sock_release(sk->sk_socket);
sk->sk_net = get_net(&init_net);
sock_net_set(sk, get_net(&init_net));
sock_put(sk);
}
EXPORT_SYMBOL(sk_release_kernel);
Expand All @@ -1017,7 +1017,7 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority)
sock_copy(newsk, sk);

/* SANITY */
get_net(newsk->sk_net);
get_net(sock_net(newsk));
sk_node_init(&newsk->sk_node);
sock_lock_init(newsk);
bh_lock_sock(newsk);
Expand Down
2 changes: 1 addition & 1 deletion net/decnet/af_decnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ static int dn_accept(struct socket *sock, struct socket *newsock, int flags)

cb = DN_SKB_CB(skb);
sk->sk_ack_backlog--;
newsk = dn_alloc_sock(sk->sk_net, newsock, sk->sk_allocation);
newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation);
if (newsk == NULL) {
release_sock(sk);
kfree_skb(skb);
Expand Down
Loading

0 comments on commit 3b1e0a6

Please sign in to comment.