cn_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  102. if (m) {
  103. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  104. m->seq = cn_test_timer_counter;
  105. m->len = sizeof(data);
  106. m->len =
  107. scnprintf(data, sizeof(data), "counter = %u",
  108. cn_test_timer_counter) + 1;
  109. memcpy(m + 1, data, m->len);
  110. cn_netlink_send(m, 0, GFP_ATOMIC);
  111. kfree(m);
  112. }
  113. cn_test_timer_counter++;
  114. mod_timer(&cn_test_timer, jiffies + HZ);
  115. }
  116. static int cn_test_init(void)
  117. {
  118. int err;
  119. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  120. if (err)
  121. goto err_out;
  122. cn_test_id.val++;
  123. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  124. if (err) {
  125. cn_del_callback(&cn_test_id);
  126. goto err_out;
  127. }
  128. setup_timer(&cn_test_timer, cn_test_timer_func, 0);
  129. cn_test_timer.expires = jiffies + HZ;
  130. add_timer(&cn_test_timer);
  131. return 0;
  132. err_out:
  133. if (nls && nls->sk_socket)
  134. sock_release(nls->sk_socket);
  135. return err;
  136. }
  137. static void cn_test_fini(void)
  138. {
  139. del_timer_sync(&cn_test_timer);
  140. cn_del_callback(&cn_test_id);
  141. cn_test_id.val--;
  142. cn_del_callback(&cn_test_id);
  143. if (nls && nls->sk_socket)
  144. sock_release(nls->sk_socket);
  145. }
  146. module_init(cn_test_init);
  147. module_exit(cn_test_fini);
  148. MODULE_LICENSE("GPL");
  149. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  150. MODULE_DESCRIPTION("Connector's test module");