gluon-simple-tc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #define _GNU_SOURCE
  23. #include <errno.h>
  24. #include <error.h>
  25. #include <stdbool.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <arpa/inet.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <net/if.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/pkt_cls.h>
  35. #include <linux/pkt_sched.h>
  36. #include <linux/rtnetlink.h>
  37. #include <netlink/msg.h>
  38. #include <netlink/attr.h>
  39. #include <netlink/socket.h>
  40. static struct nl_cb *cb;
  41. static struct nl_sock *sock;
  42. static double ticks;
  43. static unsigned ifindex;
  44. static bool nlexpect;
  45. static int nlerror;
  46. static inline void exit_errno(const char *message) {
  47. error(1, errno, "error: %s", message);
  48. }
  49. static inline void warn_errno(const char *message) {
  50. error(0, errno, "warning: %s", message);
  51. }
  52. static void read_psched(void) {
  53. uint32_t clock_res;
  54. uint32_t t2us;
  55. uint32_t us2t;
  56. FILE *f = fopen("/proc/net/psched", "r");
  57. if (!f || fscanf(f, "%08x %08x %08x", &t2us, &us2t, &clock_res) != 3)
  58. exit_errno("error reading /proc/net/psched");
  59. fclose(f);
  60. /* compatibility hack from iproute... */
  61. if (clock_res == 1000000000)
  62. t2us = us2t;
  63. ticks = (double)t2us / us2t * clock_res;
  64. }
  65. static struct nl_msg * prepare_tcmsg(int type, int flags, uint32_t parent, uint32_t handle, uint32_t info) {
  66. struct nl_msg *msg = nlmsg_alloc_simple(type, flags);
  67. if (!msg)
  68. exit_errno("nlmsg_alloc_simple");
  69. struct tcmsg tcmsg;
  70. memset(&tcmsg, 0, sizeof(tcmsg));
  71. tcmsg.tcm_family = AF_UNSPEC;
  72. tcmsg.tcm_ifindex = ifindex;
  73. tcmsg.tcm_parent = parent;
  74. tcmsg.tcm_handle = handle;
  75. tcmsg.tcm_info = info;
  76. nlmsg_append(msg, &tcmsg, sizeof(tcmsg), NLMSG_ALIGNTO);
  77. return msg;
  78. }
  79. static int error_handler(struct sockaddr_nl *nla __attribute__((unused)), struct nlmsgerr *nlerr, void *arg __attribute__((unused))) {
  80. if (!nlexpect || (nlerr->error != -ENOENT && nlerr->error != -EINVAL))
  81. nlerror = -nlerr->error;
  82. return NL_STOP;
  83. }
  84. static bool do_send(struct nl_msg *msg, bool expect) {
  85. nlerror = 0;
  86. nlexpect = expect;
  87. nl_send_auto_complete(sock, msg);
  88. nlmsg_free(msg);
  89. nl_wait_for_ack(sock);
  90. if (nlerror) {
  91. error(0, nlerror, "netlink");
  92. errno = nlerror;
  93. return false;
  94. }
  95. return true;
  96. }
  97. static inline unsigned get_xmittime(double rate, unsigned size) {
  98. return ticks * (size/rate);
  99. }
  100. static void complete_rate(struct tc_ratespec *r, uint32_t rtab[256]) {
  101. r->linklayer = TC_LINKLAYER_ETHERNET;
  102. r->cell_align = -1;
  103. r->cell_log = 3;
  104. unsigned i;
  105. for (i = 0; i < 256; i++)
  106. rtab[i] = get_xmittime(r->rate, (i + 1) << 3);
  107. }
  108. static void do_ingress(double rate) {
  109. if (!do_send(prepare_tcmsg(RTM_DELQDISC, 0, TC_H_INGRESS, 0xffff0000, 0), true))
  110. return;
  111. if (rate < 0)
  112. return;
  113. struct nl_msg *msg = prepare_tcmsg(RTM_NEWQDISC, NLM_F_CREATE | NLM_F_EXCL, TC_H_INGRESS, 0xffff0000, 0);
  114. nla_put_string(msg, TCA_KIND, "ingress");
  115. if (!do_send(msg, false))
  116. return;
  117. msg = prepare_tcmsg(RTM_NEWTFILTER, NLM_F_CREATE | NLM_F_EXCL, 0xffff0000, 0, TC_H_MAKE(0, htons(ETH_P_ALL)));
  118. const unsigned buffer = 10240;
  119. struct tc_police p;
  120. memset(&p, 0, sizeof(p));
  121. /* Range check has been done in main() */
  122. p.rate.rate = rate;
  123. p.burst = get_xmittime(p.rate.rate, buffer);
  124. p.action = TC_POLICE_SHOT;
  125. uint32_t rtab[256];
  126. complete_rate(&p.rate, rtab);
  127. nla_put_string(msg, TCA_KIND, "basic");
  128. struct nlattr *opts = nla_nest_start(msg, TCA_OPTIONS);
  129. struct nlattr *police = nla_nest_start(msg, TCA_BASIC_POLICE);
  130. nla_put(msg, TCA_POLICE_TBF, sizeof(p), &p);
  131. nla_put(msg, TCA_POLICE_RATE, sizeof(rtab), rtab);
  132. nla_nest_end(msg, police);
  133. nla_nest_end(msg, opts);
  134. do_send(msg, false);
  135. }
  136. static void do_egress(double rate) {
  137. if (!do_send(prepare_tcmsg(RTM_DELQDISC, 0, TC_H_ROOT, 0, 0), true))
  138. return;
  139. if (rate < 0)
  140. return;
  141. struct nl_msg *msg = prepare_tcmsg(RTM_NEWQDISC, NLM_F_CREATE | NLM_F_EXCL, TC_H_ROOT, 0, 0);
  142. const unsigned buffer = 2048;
  143. struct tc_tbf_qopt opt;
  144. memset(&opt, 0, sizeof(opt));
  145. /* Range check has been done in main() */
  146. opt.rate.rate = rate;
  147. opt.limit = 0.05*rate + buffer;
  148. opt.buffer = get_xmittime(opt.rate.rate, buffer);
  149. uint32_t rtab[256];
  150. complete_rate(&opt.rate, rtab);
  151. nla_put_string(msg, TCA_KIND, "tbf");
  152. struct nlattr *opts = nla_nest_start(msg, TCA_OPTIONS);
  153. nla_put(msg, TCA_TBF_PARMS, sizeof(opt), &opt);
  154. nla_put(msg, TCA_TBF_BURST, sizeof(buffer), &buffer);
  155. nla_put(msg, TCA_TBF_RTAB, sizeof(rtab), rtab);
  156. nla_nest_end(msg, opts);
  157. do_send(msg, false);
  158. }
  159. static inline void usage(void) {
  160. fprintf(stderr, "Usage: gluon-simple-tc <interface> <ingress Kbit/s>|- <egress Kbit/s>|-\n");
  161. exit(1);
  162. }
  163. static inline void maxrate(void) {
  164. error(1, 0, "error: maximum allowed rate it about 2^25 Kbit/s");
  165. }
  166. int main(int argc, char *argv[]) {
  167. if (argc != 4)
  168. usage();
  169. double ingress = -1, egress = -1;
  170. char *end;
  171. ifindex = if_nametoindex(argv[1]);
  172. if (!ifindex)
  173. error(1, 0, "invalid interface: %s", argv[1]);
  174. if (strcmp(argv[2], "-") != 0) {
  175. ingress = strtod(argv[2], &end);
  176. if (*end || ingress < 0)
  177. usage();
  178. ingress *= 125;
  179. if (ingress >= (1ull << 32))
  180. maxrate();
  181. }
  182. if (strcmp(argv[3], "-") != 0) {
  183. egress = strtod(argv[3], &end);
  184. if (*end || egress < 0)
  185. usage();
  186. egress *= 125;
  187. if (egress >= (1ull << 32))
  188. maxrate();
  189. }
  190. read_psched();
  191. cb = nl_cb_alloc(NL_CB_DEFAULT);
  192. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
  193. sock = nl_socket_alloc_cb(cb);
  194. if (!sock)
  195. exit_errno("nl_socket_alloc");
  196. if (nl_connect(sock, NETLINK_ROUTE))
  197. exit_errno("nl_connect");
  198. do_ingress(ingress);
  199. do_egress(egress);
  200. nl_socket_free(sock);
  201. nl_cb_put(cb);
  202. return 0;
  203. }