Guest User

Untitled

a guest
Sep 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.05 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 1995 Danny Gasparovski.
  3.  *
  4.  * Please read the file COPYRIGHT for the
  5.  * terms and conditions of the copyright.
  6.  */
  7.  
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. struct quehead {
  14.     struct quehead *qh_link;
  15.     struct quehead *qh_rlink;
  16. };
  17.  
  18. struct Slirp {
  19.     struct quehead if_fastq;   /* fast queue (for interactive data) */
  20.     struct quehead if_batchq;  /* queue for non-interactive data */
  21.     struct mbuf *next_m;    /* pointer to next mbuf to output */
  22.     bool if_start_busy;     /* avoid if_start recursion */
  23. };
  24.  
  25. typedef struct Slirp Slirp;
  26.  
  27. struct socket {
  28.     int so_queued;
  29.     int so_nqueued;
  30.     int so_iptos;
  31. };
  32.  
  33. #define IPTOS_LOWDELAY 1
  34.  
  35. struct mbuf {
  36.     struct  mbuf *m_next;       /* Linked list of mbufs */
  37.     struct  mbuf *m_prev;
  38.     struct  mbuf *m_nextpkt;    /* Next packet in queue/record */
  39.     struct  mbuf *m_prevpkt;    /* Flags aren't used in the output queue */
  40.     struct  socket *m_so;
  41.     Slirp *slirp;
  42.     uint64_t expiration_date;
  43.     int encap_result;
  44. };
  45.  
  46. #define QEMU_CLOCK_REALTIME 1
  47. static uint64_t qemu_clock_get_ns(int x)
  48. {
  49.     return 0;
  50. }
  51.  
  52. static int if_encap(Slirp *slirp, struct mbuf *ifm)
  53. {
  54.     printf("if_encap(%p) returning %d\n", ifm, ifm->encap_result);
  55.     return ifm->encap_result;
  56. }
  57.  
  58. static void m_free(void *foo)
  59. {
  60. }
  61.  
  62. #define ifq_prev m_prev
  63. #define ifq_next m_next
  64. #define ifs_prev m_prevpkt
  65. #define ifs_next m_nextpkt
  66. #define ifq_so m_so
  67.  
  68. inline void
  69. insque(void *a, void *b)
  70. {
  71.     register struct quehead *element = (struct quehead *) a;
  72.     register struct quehead *head = (struct quehead *) b;
  73.     element->qh_link = head->qh_link;
  74.     head->qh_link = (struct quehead *)element;
  75.     element->qh_rlink = (struct quehead *)head;
  76.     ((struct quehead *)(element->qh_link))->qh_rlink
  77.     = (struct quehead *)element;
  78. }
  79.  
  80. inline void
  81. remque(void *a)
  82. {
  83.   register struct quehead *element = (struct quehead *) a;
  84.   ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
  85.   ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
  86.   element->qh_rlink = NULL;
  87. }
  88.  
  89. static void
  90. ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
  91. {
  92.     ifm->ifs_next = ifmhead->ifs_next;
  93.     ifmhead->ifs_next = ifm;
  94.     ifm->ifs_prev = ifmhead;
  95.     ifm->ifs_next->ifs_prev = ifm;
  96. }
  97.  
  98. static inline void ifs_init(struct mbuf *ifm)
  99. {
  100.     ifm->ifs_next = ifm->ifs_prev = ifm;
  101. }
  102.  
  103. static void
  104. ifs_remque(struct mbuf *ifm)
  105. {
  106.     ifm->ifs_prev->ifs_next = ifm->ifs_next;
  107.     ifm->ifs_next->ifs_prev = ifm->ifs_prev;
  108. }
  109.  
  110. void
  111. if_init(Slirp *slirp)
  112. {
  113.     slirp->if_fastq.qh_link = slirp->if_fastq.qh_rlink = &slirp->if_fastq;
  114.     slirp->if_batchq.qh_link = slirp->if_batchq.qh_rlink = &slirp->if_batchq;
  115.     slirp->next_m = (struct mbuf *) &slirp->if_batchq;
  116. }
  117.  
  118. void if_start(Slirp *slirp);
  119.  
  120. /*
  121.  * if_output: Queue packet into an output queue.
  122.  * There are 2 output queue's, if_fastq and if_batchq.
  123.  * Each output queue is a doubly linked list of double linked lists
  124.  * of mbufs, each list belonging to one "session" (socket).  This
  125.  * way, we can output packets fairly by sending one packet from each
  126.  * session, instead of all the packets from one session, then all packets
  127.  * from the next session, etc.  Packets on the if_fastq get absolute
  128.  * priority, but if one session hogs the link, it gets "downgraded"
  129.  * to the batchq until it runs out of packets, then it'll return
  130.  * to the fastq (eg. if the user does an ls -alR in a telnet session,
  131.  * it'll temporarily get downgraded to the batchq)
  132.  */
  133. void
  134. if_output(struct socket *so, struct mbuf *ifm)
  135. {
  136.     Slirp *slirp = ifm->slirp;
  137.     struct mbuf *ifq;
  138.     int on_fastq = 1;
  139.  
  140.     /*
  141.      * See if there's already a batchq list for this session.
  142.      * This can include an interactive session, which should go on fastq,
  143.      * but gets too greedy... hence it'll be downgraded from fastq to batchq.
  144.      * We mustn't put this packet back on the fastq (or we'll send it out of order)
  145.      * XXX add cache here?
  146.      */
  147.     for (ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
  148.          (struct quehead *) ifq != &slirp->if_batchq;
  149.          ifq = ifq->ifq_prev) {
  150.         if (so == ifq->ifq_so) {
  151.             /* A match! */
  152.             ifm->ifq_so = so;
  153.             ifs_insque(ifm, ifq->ifs_prev);
  154.             goto diddit;
  155.         }
  156.     }
  157.  
  158.     /* No match, check which queue to put it on */
  159.     if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
  160.         ifq = (struct mbuf *) slirp->if_fastq.qh_rlink;
  161.         on_fastq = 1;
  162.         /*
  163.          * Check if this packet is a part of the last
  164.          * packet's session
  165.          */
  166.         if (ifq->ifq_so == so) {
  167.             ifm->ifq_so = so;
  168.             ifs_insque(ifm, ifq->ifs_prev);
  169.             goto diddit;
  170.         }
  171.         } else {
  172.         ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
  173.                 /* Set next_m if the queue was empty so far */
  174.                 if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
  175.                     slirp->next_m = ifm;
  176.                 }
  177.         }
  178.  
  179.     /* Create a new doubly linked list for this session */
  180.     ifm->ifq_so = so;
  181.     ifs_init(ifm);
  182.     insque(ifm, ifq);
  183.  
  184. diddit:
  185.     if (so) {
  186.         /* Update *_queued */
  187.         so->so_queued++;
  188.         so->so_nqueued++;
  189.         /*
  190.          * Check if the interactive session should be downgraded to
  191.          * the batchq.  A session is downgraded if it has queued 6
  192.          * packets without pausing, and at least 3 of those packets
  193.          * have been sent over the link
  194.          * (XXX These are arbitrary numbers, probably not optimal..)
  195.          */
  196.         if (on_fastq && ((so->so_nqueued >= 6) &&
  197.                  (so->so_nqueued - so->so_queued) >= 3)) {
  198.  
  199.             /* Remove from current queue... */
  200.             remque(ifm->ifs_next);
  201.  
  202.             /* ...And insert in the new.  That'll teach ya! */
  203.             insque(ifm->ifs_next, &slirp->if_batchq);
  204.         }
  205.     }
  206.  
  207. #ifndef FULL_BOLT
  208.     /*
  209.      * This prevents us from malloc()ing too many mbufs
  210.      */
  211.     if_start(ifm->slirp);
  212. #endif
  213. }
  214.  
  215. /*
  216.  * Send a packet
  217.  * We choose a packet based on its position in the output queues;
  218.  * If there are packets on the fastq, they are sent FIFO, before
  219.  * everything else.  Otherwise we choose the first packet from the
  220.  * batchq and send it.  the next packet chosen will be from the session
  221.  * after this one, then the session after that one, and so on..  So,
  222.  * for example, if there are 3 ftp session's fighting for bandwidth,
  223.  * one packet will be sent from the first session, then one packet
  224.  * from the second session, then one packet from the third, then back
  225.  * to the first, etc. etc.
  226.  */
  227. void if_start(Slirp *slirp)
  228. {
  229.     uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  230.     bool from_batchq, next_from_batchq;
  231.     struct mbuf *ifm, *ifm_next, *ifqt;
  232.  
  233.     if (slirp->if_start_busy) {
  234.         return;
  235.     }
  236.     slirp->if_start_busy = true;
  237.  
  238.     if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
  239.         ifm_next = (struct mbuf *) slirp->if_fastq.qh_link;
  240.         next_from_batchq = false;
  241.     } else if ((struct quehead *) slirp->next_m != &slirp->if_batchq) {
  242.         /* Nothing on fastq, pick up from batchq via next_m */
  243.         ifm_next = slirp->next_m;
  244.         next_from_batchq = true;
  245.     } else {
  246.         ifm_next = NULL;
  247.     }
  248.  
  249.     printf("\nif_start loop begins\n");
  250.     while (ifm_next) {
  251.         ifm = ifm_next;
  252.         printf("ifm_next=%p (session=%p), slirp->next_m=%p\n",
  253.         ifm, ifm->m_so, slirp->next_m);
  254.         from_batchq = next_from_batchq;
  255.  
  256.         ifm_next = ifm->ifq_next;
  257.         if ((struct quehead *) ifm_next == &slirp->if_fastq) {
  258.             /* No more packets in fastq, switch to batchq */
  259.             ifm_next = slirp->next_m;
  260.             next_from_batchq = true;
  261.         }
  262.         if ((struct quehead *) ifm_next == &slirp->if_batchq) {
  263.             /* end of batchq */
  264.             ifm_next = NULL;
  265.         }
  266.  
  267.         /* Try to send packet unless it already expired */
  268.         if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
  269.             /* Packet is delayed due to pending ARP or NDP resolution */
  270.             continue;
  271.         }
  272.  
  273.         if (ifm == slirp->next_m) {
  274.             /* Set which packet to send on next iteration */
  275.             slirp->next_m = ifm->ifq_next;
  276.         }
  277.  
  278.         /* Remove it from the queue */
  279.         ifqt = ifm->ifq_prev;
  280.         remque(ifm);
  281.  
  282.         /* If there are more packets for this session, re-queue them */
  283.         if (ifm->ifs_next != ifm) {
  284.             struct mbuf *next = ifm->ifs_next;
  285.  
  286.             insque(next, ifqt);
  287.             ifs_remque(ifm);
  288.  
  289.             if (!from_batchq) {
  290.                 /* Next packet in fastq is from the same session */
  291.                 ifm_next = next;
  292.                 next_from_batchq = false;
  293.             } else if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
  294.                 /* Set next_m and ifm_next if the session packet is now the
  295.                  * only one on batchq */
  296.                 slirp->next_m = ifm_next = next;
  297.             }
  298.         }
  299.  
  300.         /* Update so_queued */
  301.         if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
  302.             /* If there's no more queued, reset nqueued */
  303.             ifm->ifq_so->so_nqueued = 0;
  304.         }
  305.  
  306.         m_free(ifm);
  307.     }
  308.  
  309.     slirp->if_start_busy = false;
  310. }
  311.  
  312. int main(int argc, char **argv)
  313. {
  314.     Slirp s = {0};
  315.     if_init(&s);
  316.  
  317.     struct mbuf p0 = {0};
  318.     struct socket s0 = {0};
  319.     struct mbuf p1 = {0};
  320.     struct socket s1 = {0};
  321.     struct mbuf p2 = {0};
  322.     struct mbuf p3 = {0};
  323.  
  324.     printf("s0                        = %p\n", &s0);
  325.     printf("s1                        = %p\n", &s1);
  326.     printf("\n");
  327.     printf("p0                        = %p\n", &p0);
  328.     printf("p1                        = %p\n", &p1);
  329.     printf("p2                        = %p\n", &p2);
  330.     printf("p3                        = %p\n", &p3);
  331.     printf("&slirp->if_batchq         = %p\n", &s.if_batchq);
  332.  
  333.     p0.slirp = &s;
  334.     p0.encap_result = 0;
  335.     if_output(&s0, &p0);
  336.  
  337.     p1.slirp = &s;
  338.     p1.encap_result = 0;
  339.     if_output(&s0, &p1);
  340.  
  341.     p0.encap_result = 1;
  342.     p2.slirp = &s;
  343.     p2.encap_result = 1;
  344.     if_output(&s1, &p2);
  345.  
  346.     p3.slirp = &s;
  347.     p3.encap_result = 1;
  348.     if_output(&s0, &p3);
  349.  
  350.     printf("\nFinal results:\n");
  351.     printf("slirp->if_batchq.qh_link  = %p\n", s.if_batchq.qh_link);
  352.     printf("slirp->if_batchq.qh_rlink = %p\n", s.if_batchq.qh_rlink);
  353.     printf("slirp->next_m             = %p\n", s.next_m);
  354.  
  355.     return 0;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment