cn_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * cn_test.c
  3. *
  4. * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/timer.h>
  26. #include <linux/connector.h>
  27. static struct cb_id cn_test_id = { 0x123, 0x456 };
  28. static char cn_test_name[] = "cn_test";
  29. static struct sock *nls;
  30. static struct timer_list cn_test_timer;
  31. void cn_test_callback(void *data)
  32. {
  33. struct cn_msg *msg = (struct cn_msg *)data;
  34. printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
  35. __func__, jiffies, msg->id.idx, msg->id.val,
  36. msg->seq, msg->ack, msg->len, (char *)msg->data);
  37. }
  38. static int cn_test_want_notify(void)
  39. {
  40. struct cn_ctl_msg *ctl;
  41. struct cn_notify_req *req;
  42. struct cn_msg *msg = NULL;
  43. int size, size0;
  44. struct sk_buff *skb;
  45. struct nlmsghdr *nlh;
  46. u32 group = 1;
  47. size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
  48. size = NLMSG_SPACE(size0);
  49. skb = alloc_skb(size, GFP_ATOMIC);
  50. if (!skb) {
  51. printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
  52. size);
  53. return -ENOMEM;
  54. }
  55. nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
  56. msg = (struct cn_msg *)NLMSG_DATA(nlh);
  57. memset(msg, 0, size0);
  58. msg->id.idx = -1;
  59. msg->id.val = -1;
  60. msg->seq = 0x123;
  61. msg->ack = 0x345;
  62. msg->len = size0 - sizeof(*msg);
  63. ctl = (struct cn_ctl_msg *)(msg + 1);
  64. ctl->idx_notify_num = 1;
  65. ctl->val_notify_num = 2;
  66. ctl->group = group;
  67. ctl->len = msg->len - sizeof(*ctl);
  68. req = (struct cn_notify_req *)(ctl + 1);
  69. /*
  70. * Idx.
  71. */
  72. req->first = cn_test_id.idx;
  73. req->range = 10;
  74. /*
  75. * Val 0.
  76. */
  77. req++;
  78. req->first = cn_test_id.val;
  79. req->range = 10;
  80. /*
  81. * Val 1.
  82. */
  83. req++;
  84. req->first = cn_test_id.val + 20;
  85. req->range = 10;
  86. NETLINK_CB(skb).dst_group = ctl->group;
  87. //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
  88. netlink_unicast(nls, skb, 0, 0);
  89. printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
  90. return 0;
  91. nlmsg_failure:
  92. printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
  93. kfree_skb(skb);
  94. return -EINVAL;
  95. }
  96. static u32 cn_test_timer_counter;
  97. static void cn_test_timer_func(unsigned long __data)
  98. {
  99. struct cn_msg *m;
  100. char data[32];
  101. m = kmalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  102. if (m) {
  103. memset(m, 0, sizeof(*m) + sizeof(data));
  104. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  105. m->seq = cn_test_timer_counter;
  106. m->len = sizeof(data);
  107. m->len =
  108. scnprintf(data, sizeof(data), "counter = %u",
  109. cn_test_timer_counter) + 1;
  110. memcpy(m + 1, data, m->len);
  111. cn_netlink_send(m, 0, gfp_any());
  112. kfree(m);
  113. }
  114. cn_test_timer_counter++;
  115. mod_timer(&cn_test_timer, jiffies + HZ);
  116. }
  117. static int cn_test_init(void)
  118. {
  119. int err;
  120. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  121. if (err)
  122. goto err_out;
  123. cn_test_id.val++;
  124. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  125. if (err) {
  126. cn_del_callback(&cn_test_id);
  127. goto err_out;
  128. }
  129. init_timer(&cn_test_timer);
  130. cn_test_timer.function = cn_test_timer_func;
  131. cn_test_timer.expires = jiffies + HZ;
  132. cn_test_timer.data = 0;
  133. add_timer(&cn_test_timer);
  134. return 0;
  135. err_out:
  136. if (nls && nls->sk_socket)
  137. sock_release(nls->sk_socket);
  138. return err;
  139. }
  140. static void cn_test_fini(void)
  141. {
  142. del_timer_sync(&cn_test_timer);
  143. cn_del_callback(&cn_test_id);
  144. cn_test_id.val--;
  145. cn_del_callback(&cn_test_id);
  146. if (nls && nls->sk_socket)
  147. sock_release(nls->sk_socket);
  148. }
  149. module_init(cn_test_init);
  150. module_exit(cn_test_fini);
  151. MODULE_LICENSE("GPL");
  152. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  153. MODULE_DESCRIPTION("Connector's test module");