tcrules.awk 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. BEGIN {
  2. dmax=100
  3. if (!(linespeed > 0)) linespeed = 128
  4. FS=":"
  5. n = 0
  6. }
  7. ($1 != "") {
  8. n++
  9. class[n] = $1
  10. prio[n] = $2
  11. avgrate[n] = ($3 * linespeed / 100)
  12. pktsize[n] = $4
  13. delay[n] = $5
  14. maxrate[n] = ($6 * linespeed / 100)
  15. qdisc[n] = $7
  16. filter[n] = $8
  17. }
  18. END {
  19. allocated = 0
  20. maxdelay = 0
  21. for (i = 1; i <= n; i++) {
  22. # set defaults
  23. if (!(pktsize[i] > 0)) pktsize[i] = 1500
  24. if (!(prio[i] > 0)) prio[i] = 1
  25. allocated += avgrate[i]
  26. sum_prio += prio[i]
  27. if ((avgrate[i] > 0) && !(delay[i] > 0)) {
  28. sum_rtprio += prio[i]
  29. }
  30. }
  31. # allocation of m1 in rt classes:
  32. # sum(d * m1) must not exceed dmax * (linespeed - allocated)
  33. dmax = 0
  34. for (i = 1; i <= n; i++) {
  35. if (avgrate[i] > 0) {
  36. rtm2[i] = avgrate[i]
  37. if (delay[i] > 0) {
  38. d[i] = delay[i]
  39. } else {
  40. d[i] = 2 * pktsize[i] * 1000 / (linespeed * 1024)
  41. if (d[i] > dmax) dmax = d[i]
  42. }
  43. }
  44. }
  45. ds_avail = dmax * (linespeed - allocated)
  46. for (i = 1; i <= n; i++) {
  47. lsm1[i] = 0
  48. rtm1[i] = 0
  49. lsm2[i] = linespeed * prio[i] / sum_prio
  50. if ((avgrate[i] > 0) && (d[i] > 0)) {
  51. if (!(delay[i] > 0)) {
  52. ds = ds_avail * prio[i] / sum_rtprio
  53. ds_avail -= ds
  54. rtm1[i] = rtm2[i] + ds/d[i]
  55. }
  56. lsm1[i] = rtm1[i]
  57. }
  58. else {
  59. d[i] = 0
  60. }
  61. }
  62. # main qdisc
  63. for (i = 1; i <= n; i++) {
  64. printf "tc class add dev "device" parent 1:1 classid 1:"class[i]"0 hfsc"
  65. if (rtm1[i] > 0) {
  66. printf " rt m1 " int(rtm1[i]) "kbit d " int(d[i] * 1000) "us m2 " int(rtm2[i])"kbit"
  67. }
  68. printf " ls m1 " int(lsm1[i]) "kbit d " int(d[i] * 1000) "us m2 " int(lsm2[i]) "kbit"
  69. print " ul rate " int(maxrate[i]) "kbit"
  70. }
  71. # leaf qdisc
  72. avpkt = 1200
  73. for (i = 1; i <= n; i++) {
  74. print "tc qdisc add dev "device" parent 1:"class[i]"0 handle "class[i]"00: fq_codel limit 800 quantum 300 noecn"
  75. }
  76. # filter rule
  77. for (i = 1; i <= n; i++) {
  78. filter_cmd = "tc filter add dev "device" parent 1: prio %d handle %s fw flowid 1:%d0\n";
  79. if (direction == "up") {
  80. filter_1 = sprintf("0x%x0/0xf0", class[i])
  81. filter_2 = sprintf("0x0%x/0x0f", class[i])
  82. } else {
  83. filter_1 = sprintf("0x0%x/0x0f", class[i])
  84. filter_2 = sprintf("0x%x0/0xf0", class[i])
  85. }
  86. printf filter_cmd, class[i] * 2, filter_1, class[i]
  87. printf filter_cmd, class[i] * 2 + 1, filter_2, class[i]
  88. filterc=1
  89. if (filter[i] != "") {
  90. print " tc filter add dev "device" parent "class[i]"00: handle "filterc"0 "filter[i]
  91. filterc=filterc+1
  92. }
  93. }
  94. }