X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=src%2Fkernel%2Fswift.c;h=b21c106daa19915fbfe9c9f5cb413dfad1493884;hb=c7ff89f0ab1d13c324b9772e55095b8a651cc603;hp=5ac64edc28a24c91b5a1bc14d01498a6e0e66ec4;hpb=035781747487d5d0724fd8a5a53fe953494112bd;p=swifty.git diff --git a/src/kernel/swift.c b/src/kernel/swift.c index 5ac64ed..b21c106 100644 --- a/src/kernel/swift.c +++ b/src/kernel/swift.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -14,8 +15,8 @@ MODULE_LICENSE("GPL"); struct swift_sock { struct inet_sock sock; /* swift socket speciffic data */ - __be16 src; - __be16 dst; + uint8_t src; + uint8_t dst; }; static struct swift_sock * sock_port_map[MAX_SWIFT_PORT]; @@ -30,7 +31,7 @@ static inline struct swifthdr * swift_hdr(const struct sk_buff * skb) return (struct swifthdr *) skb_transport_header(skb); } -static inline __be16 get_next_free_port(void) +static inline uint8_t get_next_free_port(void) { int i; for (i = MIN_SWIFT_PORT; i < MAX_SWIFT_PORT; i ++) @@ -39,17 +40,17 @@ static inline __be16 get_next_free_port(void) return 0; } -static inline void swift_unhash(__be16 port) +static inline void swift_unhash(uint8_t port) { sock_port_map[port] = NULL; } -static inline void swift_hash(__be16 port, struct swift_sock *ssh) +static inline void swift_hash(uint8_t port, struct swift_sock *ssh) { sock_port_map[port] = ssh; } -static inline struct swift_sock * swift_lookup(__be16 port) +static inline struct swift_sock * swift_lookup(uint8_t port) { return sock_port_map[port]; } @@ -59,7 +60,7 @@ static int swift_release(struct socket *sock) struct sock *sk = sock->sk; struct swift_sock * ssk = swift_sk(sk); - if (!sk) + if (unlikely(!sk)) return 0; swift_unhash(ssk->src); @@ -84,33 +85,27 @@ static int swift_bind(struct socket *sock, struct sockaddr *addr, int addr_len) struct sockaddr_swift *swift_addr; struct swift_sock *ssk; int err; - __be16 port; + uint8_t port; - err = -EINVAL; - if (addr_len < sizeof(struct sockaddr_swift)) { + if (unlikely(addr_len < sizeof(struct sockaddr_swift))) { log_error("Invalid size for sockaddr\n"); + err = -EINVAL; goto out; } swift_addr = (struct sockaddr_swift *) addr; - err = -EINVAL; - if (swift_addr->sin_family != AF_INET) { - log_error("Invalid family for sockaddr\n"); - goto out; - } - - port = ntohs(swift_addr->sin_port); + port = swift_addr->dests[0].port; - err = -EINVAL; - if (port == 0 || port >= MAX_SWIFT_PORT) { + if (unlikely(port == 0 || port >= MAX_SWIFT_PORT)) { log_error("Invalid value for sockaddr port (%u)\n", port); + err = -EINVAL; goto out; } - err = -EADDRINUSE; - if (swift_lookup(port) != NULL) { + if (unlikely(swift_lookup(port) != NULL)) { log_error("Port %u already in use\n", port); + err = -EADDRINUSE; goto out; } @@ -124,7 +119,7 @@ static int swift_bind(struct socket *sock, struct sockaddr *addr, int addr_len) return 0; out: - return -EINVAL; + return err; } static int swift_connect(struct socket *sock, struct sockaddr *addr, int addr_len, int flags) @@ -136,53 +131,57 @@ static int swift_connect(struct socket *sock, struct sockaddr *addr, int addr_le log_debug("swift_connect\n"); - err = -EINVAL; - if (sock == NULL) { + if (unlikely(sock == NULL)) { log_error("Sock is NULL\n"); + err = -EINVAL; goto out; } sk = sock->sk; - err = -EINVAL; - if (sk == NULL) { + if (unlikely(sk == NULL)) { log_error("Sock->sk is NULL\n"); + err = -EINVAL; goto out; } - + isk = inet_sk(sk); ssk = swift_sk(sk); - if (ssk->src != 0) { + if (unlikely(ssk->src != 0)) { log_error("ssk->src is not NULL\n"); + err = -EINVAL; goto out; } - err = -EINVAL; - if (addr) { + if (likely(addr)) { struct sockaddr_swift * swift_addr = (struct sockaddr_swift *) addr; - err = -EINVAL; - if (addr_len < sizeof(*swift_addr) || swift_addr->sin_family != AF_INET) { + if (unlikely(addr_len < sizeof(*swift_addr) || + addr_len < swift_addr->count * sizeof(struct swift_dest) || + swift_addr->count <= 0)) { log_error("Invalid size or address family\n"); + err = -EINVAL; goto out; } - ssk->dst = ntohs(swift_addr->sin_port); - if (ssk->dst == 0 || ssk->dst >= MAX_SWIFT_PORT) { + ssk->dst = swift_addr->dests[0].port; + if (unlikely(ssk->dst == 0 || ssk->dst >= MAX_SWIFT_PORT)) { log_error("Invalid value for destination port(%u)\n", ssk->dst); + err = -EINVAL; goto out; } - isk->inet_daddr = swift_addr->sin_addr.s_addr; + isk->inet_daddr = swift_addr->dests[0].addr; log_debug("Received from user space destination port=%u and address=%u\n", ssk->dst, isk->inet_daddr); } else { log_error("Invalid swift_addr (NULL)\n"); + err = -EINVAL; goto out; } - err = -ENOMEM; ssk->src = get_next_free_port(); - if (ssk->src == 0) { + if (unlikely(ssk->src == 0)) { log_error("No free ports\n"); + err = -ENOMEM; goto out; } @@ -192,15 +191,14 @@ static int swift_connect(struct socket *sock, struct sockaddr *addr, int addr_le out: return err; - } static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) { int err; - __be16 dport; + uint8_t dport; __be32 daddr; - __be16 sport; + uint8_t sport; struct sk_buff * skb; struct sock * sk; struct inet_sock * isk; @@ -210,16 +208,16 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr int totlen; struct rtable * rt = NULL; - err = -EINVAL; - if (sock == NULL) { + if (unlikely(sock == NULL)) { log_error("Sock is NULL\n"); + err = -EINVAL; goto out; } sk = sock->sk; - err = -EINVAL; - if (sk == NULL) { + if (unlikely(sk == NULL)) { log_error("Sock->sk is NULL\n"); + err = -EINVAL; goto out; } @@ -228,10 +226,10 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr sport = ssk->src; if (sport == 0) { - err = -ENOMEM; sport = get_next_free_port(); - if (sport == 0) { + if (unlikely(sport == 0)) { log_error("No free ports\n"); + err = -ENOMEM; goto out; } } @@ -239,24 +237,27 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr if (msg->msg_name) { struct sockaddr_swift * swift_addr = (struct sockaddr_swift *) msg->msg_name; - err = -EINVAL; - if (msg->msg_namelen < sizeof(*swift_addr) || swift_addr->sin_family != AF_INET) { - log_error("Invalid size or address family\n"); + if (unlikely(msg->msg_namelen < sizeof(*swift_addr) || + msg->msg_namelen < swift_addr->count * sizeof(struct swift_dest) || + swift_addr->count <= 0)) { + log_error("Invalid size for msg_name\n"); + err = -EINVAL; goto out; } - dport = ntohs(swift_addr->sin_port); - if (dport == 0 || dport >= MAX_SWIFT_PORT) { + dport = swift_addr->dests[0].port; + if (unlikely(dport == 0 || dport >= MAX_SWIFT_PORT)) { log_error("Invalid value for destination port(%u)\n", dport); + err = -EINVAL; goto out; } - daddr = swift_addr->sin_addr.s_addr; + daddr = swift_addr->dests[0].addr; log_debug("Received from user space destination port=%u and address=%u\n", dport, daddr); } else { - err = -EDESTADDRREQ; - if (!ssk->dst || !isk->inet_daddr) { + if (unlikely(!ssk->dst || !isk->inet_daddr)) { log_error("No destination port/address\n"); + err = -EDESTADDRREQ; goto out; } dport = ssk->dst; @@ -268,7 +269,7 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr totlen = len + sizeof(struct swifthdr) + sizeof(struct iphdr); skb = sock_alloc_send_skb(sk, totlen, msg->msg_flags & MSG_DONTWAIT, &err); - if (!skb) { + if (unlikely(!skb)) { log_error("sock_alloc_send_skb failed\n"); goto out; } @@ -282,14 +283,14 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr log_debug("Reseted transport header\n"); shdr = (struct swifthdr *) skb_transport_header(skb); - shdr->dst = ntohs(dport); - shdr->src = ntohs(sport); + shdr->dst = dport; + shdr->src = sport; shdr->len = ntohs(len + sizeof(struct swifthdr)); log_debug("payload=%p\n", skb_put(skb, len)); err = skb_copy_datagram_from_iovec(skb, sizeof(struct swifthdr), msg->msg_iov, 0, len); - if (err) { + if (unlikely(err)) { log_error("skb_copy_datagram_from_iovec failed\n"); goto out_free; } @@ -304,15 +305,19 @@ static int swift_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr .flags = inet_sk_flowi_flags(sk), }; err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0); - if (err) { + if (unlikely(err)) { log_error("Route lookup failed\n"); goto out_free; } +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) + sk_dst_set(sk, dst_clone(&rt->u.dst)); +#else sk_dst_set(sk, dst_clone(&rt->dst)); +#endif } err = ip_queue_xmit(skb); - if (!err) + if (likely(!err)) log_debug("Sent %u bytes on wire\n", len); else log_error("ip_queue_xmit failed\n"); @@ -333,8 +338,10 @@ static int swift_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr struct sock * sk = sock->sk; int err, copied; + log_debug("Trying to receive sock=%p sk=%p flags=%d\n", sock, sk, flags); + skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); - if (!skb) { + if (unlikely(!skb)) { log_error("skb_recv_datagram\n"); goto out; } @@ -351,7 +358,7 @@ static int swift_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr } err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); - if (err) { + if (unlikely(err)) { log_error("skb_copy_datagram_iovec\n"); goto out_free; } @@ -375,11 +382,11 @@ static int swift_rcv(struct sk_buff *skb) struct swifthdr *shdr; struct swift_sock *ssk; __be16 len; - __be16 src, dst; + uint8_t src, dst; struct sockaddr_swift * swift_addr; int err; - if (!pskb_may_pull(skb, sizeof(struct swifthdr))) { + if (unlikely(!pskb_may_pull(skb, sizeof(struct swifthdr)))) { log_error("Insufficient space for header\n"); goto drop; } @@ -387,19 +394,19 @@ static int swift_rcv(struct sk_buff *skb) shdr = (struct swifthdr *) skb->data; len = ntohs(shdr->len); - if (skb->len < len) { + if (unlikely(skb->len < len)) { log_error("Malformed packet (packet_len=%u, skb_len=%u)\n", len, skb->len); goto drop; } - if (len < sizeof(struct swifthdr)) { + if (unlikely(len < sizeof(struct swifthdr))) { log_error("Malformed packet (packet_len=%u sizeof(swifthdr)=%u\n", len, sizeof(struct swifthdr)); goto drop; } - src = ntohs(shdr->src); - dst = ntohs(shdr->dst); - if (src == 0 || dst == 0 || src >= MAX_SWIFT_PORT || dst >= MAX_SWIFT_PORT) { + src = shdr->src; + dst = shdr->dst; + if (unlikely(src == 0 || dst == 0 || src >= MAX_SWIFT_PORT || dst >= MAX_SWIFT_PORT)) { log_error("Malformed packet (src=%u, dst=%u)\n", shdr->src, shdr->dst); goto drop; } @@ -420,14 +427,13 @@ static int swift_rcv(struct sk_buff *skb) BUILD_BUG_ON(sizeof(struct sockaddr_swift) > sizeof(skb->cb)); swift_addr = (struct sockaddr_swift *) skb->cb; - swift_addr->sin_family = AF_INET; - swift_addr->sin_port = shdr->src; - swift_addr->sin_addr.s_addr = ip_hdr(skb)->saddr; + swift_addr->dests[0].port = shdr->src; + swift_addr->dests[0].addr = ip_hdr(skb)->saddr; - log_debug("Setting sin_port=%u, sin_addr=%u\n", ntohs(shdr->src), swift_addr->sin_addr.s_addr); + log_debug("Setting sin_port=%u, sin_addr=%u\n", ntohs(shdr->src), swift_addr->dests[0].addr); err = ip_queue_rcv_skb((struct sock *) &ssk->sock, skb); - if (err) { + if (unlikely(err)) { log_error("ip_queu_rcv_skb\n"); consume_skb(skb); } @@ -484,13 +490,13 @@ static int __init swift_init(void) int rc; rc = proto_register(&swift_prot, 1); - if (rc) { + if (unlikely(rc)) { log_error("Error registering swift protocol\n"); goto out; } rc = inet_add_protocol(&swift_protocol, IPPROTO_SWIFT); - if (rc) { + if (unlikely(rc)) { log_error("Error adding swift protocol\n"); goto out_unregister; }