aboutsummaryrefslogtreecommitdiffstats
path: root/tools/remus/kmod/sch_queue.c
blob: 92da4a75cda19adb639a673ee59ab4879397d0f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * sch_queue.c Queue traffic until an explicit release command
 *
 *             This program is free software; you can redistribute it and/or
 *             modify it under the terms of the GNU General Public License
 *             as published by the Free Software Foundation; either version
 *             2 of the License, or (at your option) any later version.
 *
 * The operation of the buffer is as follows:
 * When a checkpoint begins, a barrier is inserted into the
 *   network queue by a netlink request (it operates by storing
 *   a pointer to the next packet which arrives and blocking dequeue
 *   when that packet is at the head of the queue).
 * When a checkpoint completes (the backup acknowledges receipt),
 *   currently-queued packets are released.
 * So it supports two operations, barrier and release.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <net/pkt_sched.h>

/* xenbus directory */
#define FIFO_BUF    (10*1024*1024)

#define TCQ_CHECKPOINT 0
#define TCQ_DEQUEUE    1

struct queue_sched_data {
  /* this packet is the first packet which should not be delivered.
   * If it is NULL, queue_enqueue will set it to the next packet it sees. */
  struct sk_buff *stop;
};

struct tc_queue_qopt {
  /* 0: reset stop packet pointer
   * 1: dequeue to stop pointer */
  int action;
};

/* borrowed from drivers/xen/netback/loopback.c */
#ifdef CONFIG_X86
static int is_foreign(unsigned long pfn)
{
  /* NB. Play it safe for auto-translation mode. */
  return (xen_feature(XENFEAT_auto_translated_physmap) ||
         (phys_to_machine_mapping[pfn] & FOREIGN_FRAME_BIT));
}
#else
/* How to detect a foreign mapping? Play it safe. */
#define is_foreign(pfn)	(1)
#endif

static int skb_remove_foreign_references(struct sk_buff *skb)
{
  struct page *page;
  unsigned long pfn;
  int i, off;
  char *vaddr;

  BUG_ON(skb_shinfo(skb)->frag_list);

  for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
    pfn = page_to_pfn(skb_shinfo(skb)->frags[i].page);
    if (!is_foreign(pfn))
      continue;
    /*
      printk("foreign ref found\n");
    */
    page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
    if (unlikely(!page))
      return 0;

    vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
    off = skb_shinfo(skb)->frags[i].page_offset;
    memcpy(page_address(page) + off, vaddr + off,
          skb_shinfo(skb)->frags[i].size);
    kunmap_skb_frag(vaddr);

    put_page(skb_shinfo(skb)->frags[i].page);
    skb_shinfo(skb)->frags[i].page = page;
  }

  return 1;
}

static int queue_enqueue(struct sk_buff *skb, struct Qdisc* sch)
{
  struct queue_sched_data *q = qdisc_priv(sch);

  if (likely(sch->qstats.backlog + skb->len <= FIFO_BUF))
  {
    if (!q->stop)
      q->stop = skb;

    if (!skb_remove_foreign_references(skb)) {
      printk("error removing foreign ref\n");
      return qdisc_reshape_fail(skb, sch);
    }

    return qdisc_enqueue_tail(skb, sch);
  }
  printk("queue reported full: %d,%d\n", sch->qstats.backlog, skb->len);

  return qdisc_reshape_fail(skb, sch);
}

/* dequeue doesn't actually dequeue until the release command is
 * received. */
static inline struct sk_buff *queue_dequeue(struct Qdisc* sch)
{
  struct queue_sched_data *q = qdisc_priv(sch);
  struct sk_buff* peek;
  /*
  struct timeval tv;

  if (!q->stop) {
    do_gettimeofday(&tv);
    printk("packet dequeued at %lu.%06lu\n", tv.tv_sec, tv.tv_usec);
  }
  */

  if (sch->flags & TCQ_F_THROTTLED)
    return NULL;

  peek = (struct sk_buff *)((sch->q).next);

  /* this pointer comparison may be shady */
  if (peek == q->stop) {
    /*
    do_gettimeofday(&tv);
    printk("stop packet at %lu.%06lu\n", tv.tv_sec, tv.tv_usec);
    */

    /* this is the tail of the last round. Release it and block the queue */
    sch->flags |= TCQ_F_THROTTLED;
    return NULL;
  }

  return qdisc_dequeue_head(sch);
}

static int queue_init(struct Qdisc *sch, struct rtattr *opt)
{
  sch->flags |= TCQ_F_THROTTLED;

  return 0;
}

/* receives two messages:
 *   0: checkpoint queue (set stop to next packet)
 *   1: dequeue until stop */
static int queue_change(struct Qdisc* sch, struct rtattr* opt)
{
  struct queue_sched_data *q = qdisc_priv(sch);
  struct tc_queue_qopt* msg;
  /*
  struct timeval tv;
  */

  if (!opt || RTA_PAYLOAD(opt) < sizeof(*msg))
    return -EINVAL;

  msg = RTA_DATA(opt);

  if (msg->action == TCQ_CHECKPOINT) {
    /* reset stop */
    q->stop = NULL;
  } else if (msg->action == TCQ_DEQUEUE) {
    /* dequeue */
    sch->flags &= ~TCQ_F_THROTTLED;
    netif_schedule(sch->dev);
    /*
    do_gettimeofday(&tv);
    printk("queue release at %lu.%06lu (%d bytes)\n", tv.tv_sec, tv.tv_usec,
          sch->qstats.backlog);
    */
  } else {
    return -EINVAL;
  }

  return 0;
}

struct Qdisc_ops queue_qdisc_ops = {
  .id          =       "queue",
  .priv_size   =       sizeof(struct queue_sched_data),
  .enqueue     =       queue_enqueue,
  .dequeue     =       queue_dequeue,
  .init                =       queue_init,
  .change       =      queue_change,
  .owner       =       THIS_MODULE,
};

static int __init queue_module_init(void)
{
  printk("loading queue\n");
  return register_qdisc(&queue_qdisc_ops);
}

static void __exit queue_module_exit(void)
{
  printk("queue unloaded\n");
  unregister_qdisc(&queue_qdisc_ops);
}
module_init(queue_module_init)
module_exit(queue_module_exit)
MODULE_LICENSE("GPL");