slirp: fix packet requeue issue in batchq
PATCH 2/2 slirp: fix packet requeue issue in batchq
From: Zhi Yong Wu <[email protected]>
This patch fixes the slirp crash in current QEMU upstream.
--- slirp/if.c | 37 ++++++++++++++++++++++++++++++------- slirp/mbuf.c | 3 +-- 2 files changed, 31 insertions(+), 9 deletions(-)
slirp/if.c +30 -7
@@ -20,8 +20,15 @@ ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)static void ifs_remque (struct mbuf *ifm) { - ifm->ifs_prev->ifs_next = ifm->ifs_next; - ifm->ifs_next->ifs_prev = ifm->ifs_prev; +if (ifm->ifs_next->ifs_next == ifm + && ifm->ifs_next->ifs_prev == ifm) { +ifs_init (ifm->ifs_next); + }else { + ifm->ifs_prev->ifs_next = ifm->ifs_next; + ifm->ifs_next->ifs_prev = ifm->ifs_prev; + } + +ifs_init (ifm); }void
@@ -154,14 +161,18 @@ if_start(Slirp *slirp) {uint64_t now =qemu_get_clock_ns (rt_clock);int requeued =0 ; -struct mbuf *ifm, *ifqt; +struct mbuf *ifm, *ifqt, *ifm_next; -DEBUG_CALL ("if_start" ); +DEBUG_CALL ("if_start" ); -if (slirp->if_queued ==0 ) -return ;/* Nothing to do */ +if (slirp->if_queued ==0 ) +return ;/* Nothing to do */ + + slirp->next_m = &slirp->if_batchq; again: + ifm_next =NULL ; +/* check if we can really output */ if (!slirp_can_output (slirp->opaque))return ;
@@ -190,6 +201,7 @@ if_start(Slirp *slirp)/* If there are more packets for this session, re-queue them */ if (ifm->ifs_next !=/* ifm->ifs_prev != */ ifm) {insque (ifm->ifs_next, ifqt); + ifm_next = ifm->ifs_next;ifs_remque (ifm); }
@@ -209,7 +221,18 @@ if_start(Slirp *slirp)m_free (ifm); }else {/* re-queue */ -insque (ifm, ifqt); +if (ifm_next) { +/*restore the original state of bachq*/ +remque (ifm_next); +insque (ifm, ifqt); + ifm_next->ifs_prev->ifs_next = ifm; + ifm->ifs_prev = ifm_next->ifs_prev; + ifm->ifs_next = ifm_next; + ifm_next->ifs_prev = ifm; + }else { +insque (ifm, ifqt); + } + requeued++; } }
slirp/mbuf.c +1 -2
@@ -68,8 +68,7 @@ m_get(Slirp *slirp) m->m_size =SLIRP_MSIZE - offsetof(struct mbuf , m_dat); m->m_data = m->m_dat; m->m_len =0 ; - m->m_nextpkt =NULL ; - m->m_prevpkt =NULL ; +ifs_init (m); m->arp_requested = false; m->expiration_date = (uint64_t )-1 ; end_error:
Signature · 1 line
1.7.6
Quoted 19 lines · On 2012-02-15 09:13, [email protected] wrote:
From: Zhi Yong Wu <[email protected]>
This patch fixes the slirp crash in current QEMU upstream.
Signed-off-by Zhi Yong Wu <[email protected]>--- slirp/if.c | 37 ++++++++++++++++++++++++++++++------- slirp/mbuf.c | 3 +-- 2 files changed, 31 insertions(+), 9 deletions(-)slirp/if.c +0
-1@@ -20,8 +20,15 @@ ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)static void ifs_remque (struct mbuf *ifm) { - ifm->ifs_prev->ifs_next = ifm->ifs_next;
slirp/if.c ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead) · line 31
Quoted 10 lines
slirp/if.c +9
-1- ifm->ifs_next->ifs_prev = ifm->ifs_prev; +if (ifm->ifs_next->ifs_next == ifm + && ifm->ifs_next->ifs_prev == ifm) { +ifs_init (ifm->ifs_next); + }else { + ifm->ifs_prev->ifs_next = ifm->ifs_next; + ifm->ifs_next->ifs_prev = ifm->ifs_prev; + } + +ifs_init (ifm);
This looks, well, interesting. Can you explain the logic? We really need to document the queuing mechanics.
Quoted 7 lines
slirp/if.c +0
-0}void @@ -154,14 +161,18 @@ if_start(Slirp *slirp) {uint64_t now =qemu_get_clock_ns (rt_clock);int requeued =0 ;
slirp/if.c if_start(Slirp *slirp) · line 169
Quoted 10 lines
slirp/if.c +4
-4-struct mbuf *ifm, *ifqt; +struct mbuf *ifm, *ifqt, *ifm_next; -DEBUG_CALL ("if_start" ); +DEBUG_CALL ("if_start" ); -if (slirp->if_queued ==0 ) -return ;/* Nothing to do */ +if (slirp->if_queued ==0 ) +return ;/* Nothing to do */
Even if the result looks funny, let's not touch lines just for indention (and braces would be missing anyway).
slirp/if.c if_start(Slirp *slirp) · line 171
Quoted 2 lines
slirp/if.c +2
-0+ + slirp->next_m = &slirp->if_batchq;
Have you understood the difference between the natural order of if_batchq and next_m? I still wonder what the latter is good for.
Quoted 18 lines
slirp/if.c +3
-0again: + ifm_next =NULL ; +/* check if we can really output */ if (!slirp_can_output (slirp->opaque))return ;@@ -190,6 +201,7 @@ if_start(Slirp *slirp)/* If there are more packets for this session, re-queue them */ if (ifm->ifs_next !=/* ifm->ifs_prev != */ ifm) {insque (ifm->ifs_next, ifqt); + ifm_next = ifm->ifs_next;ifs_remque (ifm); }@@ -209,7 +221,18 @@ if_start(Slirp *slirp)m_free (ifm); }else {
slirp/if.c if_start(Slirp *slirp) · line 231
Quoted 10 lines
slirp/if.c +8
-1/* re-queue */ -insque (ifm, ifqt); +if (ifm_next) { +/*restore the original state of bachq*/ +remque (ifm_next); +insque (ifm, ifqt); + ifm_next->ifs_prev->ifs_next = ifm; + ifm->ifs_prev = ifm_next->ifs_prev; + ifm->ifs_next = ifm_next; + ifm_next->ifs_prev = ifm;
So is this only about the correct ordering or also about pointer correctness?
Quoted 12 lines
slirp/if.c +4
-0+ }else { +insque (ifm, ifqt); + } + requeued++; } }slirp/mbuf.c +0
-0
slirp/mbuf.c m_get(Slirp *slirp) · line 74
Quoted 9 lines
slirp/mbuf.c +1
-2@@ -68,8 +68,7 @@ m_get(Slirp *slirp) m->m_size =SLIRP_MSIZE - offsetof(struct mbuf , m_dat); m->m_data = m->m_dat; m->m_len =0 ; - m->m_nextpkt =NULL ; - m->m_prevpkt =NULL ; +ifs_init (m); m->arp_requested = false; m->expiration_date = (uint64_t )-1 ; end_error:
Thanks for digging into this, but I really think it needs more comments and potentially even some cleanups.
Jan
Quoted 25 lines · Jan Kiszka
Quoted 24 lines · On 2012-02-15 09:13, [email protected] wrote:
From: Zhi Yong Wu <[email protected]>
This patch fixes the slirp crash in current QEMU upstream.
Signed-off-by Zhi Yong Wu <[email protected]>---
slirp/if.c | 37 ++++++++++++++++++++++++++++++-------slirp/mbuf.c| 3 +--2 files changed, 31 insertions(+), 9 deletions(-)slirp/if.c +0
-0@@ -20,8 +20,15 @@ ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)static void
ifs_remque(struct mbuf *ifm)
{
-ifm->ifs_prev->ifs_next=ifm->ifs_next;
-ifm->ifs_next->ifs_prev=ifm->ifs_prev;
+ if (ifm->ifs_next->ifs_next== ifm
+ &&ifm->ifs_next->ifs_prev== ifm) {
+ ifs_init(ifm->ifs_next);
+ } else {
Quoted 8 lines
Quoted 5 lines
+ ifm->ifs_prev->ifs_next =
ifm->ifs_next;
+ ifm->ifs_next->ifs_prev =ifm->ifs_prev;
+ }
+
+ ifs_init(ifm);This looks, well, interesting. Can you explain the logic? We really need to document the queuing mechanics.
ifm->ifs_prev->ifs_next = ifm->ifs_next;
ifm->ifs_next->ifs_prev = ifm->ifs_prev;+ ifs_init(ifm);
Sorry, actually we only need to add one line of code.
Quoted 14 lines
Quoted 14 lines
}
void
slirp/if.c +2
-2@@ -154,14 +161,18 @@ if_start(Slirp *slirp) { uint64_t now = qemu_get_clock_ns(rt_clock); int requeued = 0; - struct mbuf *ifm, *ifqt; + struct mbuf *ifm, *ifqt, *ifm_next; - DEBUG_CALL("if_start"); + DEBUG_CALL("if_start");
Quoted 7 lines
slirp/if.c if_start(Slirp *slirp) · line 169
Quoted 4 lines
slirp/if.c +2
-2- if (slirp->if_queued == 0) - return; /* Nothing to do */ + if (slirp->if_queued == 0) + return; /* Nothing to do */Even if the result looks funny, let's not touch lines just for indention (and braces would be missing anyway).
OK. undo them to their original state.
Quoted 6 lines
slirp/if.c if_start(Slirp *slirp) · line 171
Quoted 3 lines
slirp/if.c +2
-0+ + slirp->next_m = &slirp->if_batchq;Have you understood the difference between the natural order of
if_batchqandnext_m? I still wonder what the latter is good for.
Sorry, the line of code should be removed. next_m will point to next packet which will be handled, if there's multiple session, it will point to the head packet for next session; if there's one session, it will point to batchq.
Quoted 16 lines
Quoted 16 lines
again:
+ ifm_next = NULL;
+
/* check if we can really output */
if (!slirp_can_output(slirp->opaque))
return;slirp/if.c +1
-0@@ -190,6 +201,7 @@ if_start(Slirp *slirp) /* If there are more packets for this session, re-queue them */ if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) { insque(ifm->ifs_next, ifqt); + ifm_next = ifm->ifs_next; ifs_remque(ifm); }
Quoted 16 lines
slirp/if.c if_start(Slirp *slirp) · line 231
Quoted 13 lines
slirp/if.c +8
-1@@ -209,7 +221,18 @@ if_start(Slirp *slirp) m_free(ifm); } else { /* re-queue */ - insque(ifm, ifqt); + if (ifm_next) { + /*restore the original state of bachq*/ + remque(ifm_next); + insque(ifm, ifqt); + ifm_next->ifs_prev->ifs_next = ifm; + ifm->ifs_prev = ifm_next->ifs_prev; + ifm->ifs_next = ifm_next; + ifm_next->ifs_prev = ifm;So is this only about the correct ordering or also about pointer correctness?
The former. If ifm need to be re-queued, it will be put to where it originally is.
Quoted 28 lines
Quoted 21 lines
slirp/if.c +4
-0+ } else { + insque(ifm, ifqt); + } + requeued++; } }slirp/mbuf.c +1
-2@@ -68,8 +68,7 @@ m_get(Slirp *slirp) m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat); m->m_data = m->m_dat; m->m_len = 0; - m->m_nextpkt = NULL; - m->m_prevpkt = NULL; + ifs_init(m); m->arp_requested = false; m->expiration_date = (uint64_t)-1;Quoted 1 line
end_error:
Thanks for digging into this, but I really think it needs more comments and potentially even some cleanups.
Jan
Signature · 3 lines
Regards, Zhi Yong Wu