请教几个sock函数,sk_alloc.........
sk_alloc,sk_free.sk_init,sock_wfree,sock_rfree找到源码了,但是还是不明白,高手给解释一下啊~~,最想知道sk_alloc和sk_free,字面上的意思一个是分配,一个是释放,想知道是不是可以用着个函数申请一个新的socket类型比如PF_PACKET这种???
626 struct sock *sk_alloc(int family, unsigned int __nocast priority,
627 struct proto *prot, int zero_it)
628 {
629 struct sock *sk = NULL;
630 kmem_cache_t *slab = prot->slab;
631
632 if (slab != NULL)
633 sk = kmem_cache_alloc(slab, priority);
634 else
635 sk = kmalloc(prot->obj_size, priority);
636
637 if (sk) {
638 if (zero_it) {
639 memset(sk, 0, prot->obj_size);
640 sk->sk_family = family;
641 /*
642 * See comment in struct sock definition to understand
643 * why we need sk_prot_creator -acme
644 */
645 sk->sk_prot = sk->sk_prot_creator = prot;
646 sock_lock_init(sk);
647 }
648
649 if (security_sk_alloc(sk, family, priority)) {
650 if (slab != NULL)
651 kmem_cache_free(slab, sk);
652 else
653 kfree(sk);
654 sk = NULL;
655 } else
656 __module_get(prot->owner);
657 }
658 return sk;
659 }
660
661 void sk_free(struct sock *sk)
662 {
663 struct sk_filter *filter;
664 struct module *owner = sk->sk_prot_creator->owner;
665
666 if (sk->sk_destruct)
667 sk->sk_destruct(sk);
668
669 filter = sk->sk_filter;
670 if (filter) {
671 sk_filter_release(sk, filter);
672 sk->sk_filter = NULL;
673 }
674
675 sock_disable_timestamp(sk);
676
677 if (atomic_read(&sk->sk_omem_alloc))
678 printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
679 __FUNCTION__, atomic_read(&sk->sk_omem_alloc));
680
681 security_sk_free(sk);
682 if (sk->sk_prot_creator->slab != NULL)
683 kmem_cache_free(sk->sk_prot_creator->slab, sk);
684 else
685 kfree(sk);
686 module_put(owner);
687 }
688
689 void __init sk_init(void)
690 {
691 if (num_physpages <= 4096) {
692 sysctl_wmem_max = 32767;
693 sysctl_rmem_max = 32767;
694 sysctl_wmem_default = 32767;
695 sysctl_rmem_default = 32767;
696 } else if (num_physpages >= 131072) {
697 sysctl_wmem_max = 131071;
698 sysctl_rmem_max = 131071;
699 }
700 }
701
702 /*
703* Simple resource managers for sockets.
704*/
705
706
707 /*
708* Write buffer destructor automatically called from kfree_skb.
709*/
710 void sock_wfree(struct sk_buff *skb)
711 {
712 struct sock *sk = skb->sk;
713
714 /* In case it might be waiting for more memory. */
715 atomic_sub(skb->truesize, &sk->sk_wmem_alloc);
716 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE))
717 sk->sk_write_space(sk);
718 sock_put(sk);
719 }
720
721 /*
722* Read buffer destructor automatically called from kfree_skb.
723*/
724 void sock_rfree(struct sk_buff *skb)
725 {
726 struct sock *sk = skb->sk;
727
728 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
729 }
730
731 一般情况下我们不需要使用这几个函数,Socket和Sock是一对,如果要实现一种新的协议栈,就可能会用到 sk_alloc源代码中可以看到,其实它只是分配了SK这个结构的空间,类型是sock sk
你可以看看sk_init()这个初始化函数,然后往下跟,看看内核是如何建立SOCKET接口的
页:
[1]