歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核--網絡協議棧深入分析(四)--套接字內核初始化和創建過程

Linux內核--網絡協議棧深入分析(四)--套接字內核初始化和創建過程

日期:2017/3/1 10:09:43   编辑:Linux內核

本文分析基於Linux Kernel 3.2.1

更多請查看 Linux內核--網絡內核實現分析

1、系統初始化過程中會調用sock_init函數進行套接字的初始化,主要是進行緩存的初始化

  1. static int __init sock_init(void)
  2. {
  3. int err;
  4. /*
  5. * 初始化.sock緩存
  6. */
  7. sk_init();
  8. /*
  9. * 初始化sk_buff緩存
  10. */
  11. skb_init();
  12. /*
  13. * 初始化協議模塊緩存
  14. */
  15. init_inodecache();
  16. //注冊文件系統類型
  17. err = register_filesystem(&sock_fs_type);
  18. if (err)
  19. goto out_fs;
  20. sock_mnt = kern_mount(&sock_fs_type);
  21. if (IS_ERR(sock_mnt)) {
  22. err = PTR_ERR(sock_mnt);
  23. goto out_mount;
  24. }
  25. /* The real protocol initialization is performed in later initcalls.
  26. */
  27. #ifdef CONFIG_NETFILTER
  28. netfilter_init();
  29. #endif
  30. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  31. skb_timestamping_init();
  32. #endif
  33. out:
  34. return err;
  35. out_mount:
  36. unregister_filesystem(&sock_fs_type);
  37. out_fs:
  38. goto out;
  39. }

2、INET協議族的初始化函數

  1. static int __init inet_init(void)
  2. {
  3. struct sk_buff *dummy_skb;
  4. struct inet_protosw *q;
  5. struct list_head *r;
  6. int rc = -EINVAL;
  7. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  8. sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
  9. if (!sysctl_local_reserved_ports)
  10. goto out;
  11. //下面注冊傳輸層協議操作集
  12. rc = proto_register(&tcp_prot, 1);
  13. if (rc)
  14. goto out_free_reserved_ports;
  15. rc = proto_register(&udp_prot, 1);
  16. if (rc)
  17. goto out_unregister_tcp_proto;
  18. rc = proto_register(&raw_prot, 1);
  19. if (rc)
  20. goto out_unregister_udp_proto;
  21. rc = proto_register(&ping_prot, 1);
  22. if (rc)
  23. goto out_unregister_raw_proto;
  24. //注冊INET協議族的handler
  25. (void)sock_register(&inet_family_ops);
  26. #ifdef CONFIG_SYSCTL
  27. ip_static_sysctl_init();
  28. #endif
  29. /*
  30. * Add all the base protocols.
  31. */
  32. //將INET協議族協議數據包接收函數添加到系統中
  33. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  34. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  35. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  36. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  37. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  38. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  39. #ifdef CONFIG_IP_MULTICAST
  40. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  41. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  42. #endif
  43. /* Register the socket-side information for inet_create. */
  44. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  45. INIT_LIST_HEAD(r);
  46. //將inetsw_array中的元素按套接字類型注冊到inetsw鏈表數組中
  47. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  48. inet_register_protosw(q);
  49. /*
  50. * Set the ARP module up
  51. */
  52. arp_init();
  53. /*
  54. * Set the IP module up
  55. */
  56. ip_init();
  57. tcp_v4_init();
  58. /* Setup TCP slab cache for open requests. */
  59. tcp_init();
  60. /* Setup UDP memory threshold */
  61. udp_init();
  62. /* Add UDP-Lite (RFC 3828) */
  63. udplite4_register();
  64. ping_init();
  65. /*
  66. * Set the ICMP layer up
  67. */
  68. if (icmp_init() < 0)
  69. panic("Failed to create the ICMP control socket.\n");
  70. /*
  71. * Initialise the multicast router
  72. */
  73. #if defined(CONFIG_IP_MROUTE)
  74. if (ip_mr_init())
  75. printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
  76. #endif
  77. /*
  78. * Initialise per-cpu ipv4 mibs
  79. */
  80. if (init_ipv4_mibs())
  81. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
  82. ipv4_proc_init();
  83. ipfrag_init();
  84. dev_add_pack(&ip_packet_type);
  85. rc = 0;
  86. out:
  87. return rc;
  88. out_unregister_raw_proto:
  89. proto_unregister(&raw_prot);
  90. out_unregister_udp_proto:
  91. proto_unregister(&udp_prot);
  92. out_unregister_tcp_proto:
  93. proto_unregister(&tcp_prot);
  94. out_free_reserved_ports:
  95. kfree(sysctl_local_reserved_ports);
  96. goto out;
  97. }
Copyright © Linux教程網 All Rights Reserved