saving 14 bytes per PPPoE pbuf; not allocating struct eth_hdr two times per packet

This commit is contained in:
Sylvain Rochet 2012-07-15 14:56:30 +02:00
parent 94c35184a9
commit d2b2ae09e6

View File

@ -634,6 +634,14 @@ pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
return ERR_IF; return ERR_IF;
} }
/* make room for Ethernet header - should not fail */
if (pbuf_header(pb, (u16_t)(sizeof(struct eth_hdr))) != 0) {
/* bail out */
PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_output: could not allocate room for Ethernet header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
LINK_STATS_INC(link.lenerr);
pbuf_free(pb);
return ERR_BUF;
}
ethhdr = (struct eth_hdr *)pb->payload; ethhdr = (struct eth_hdr *)pb->payload;
etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC; etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
ethhdr->type = htons(etype); ethhdr->type = htons(etype);
@ -682,15 +690,14 @@ pppoe_send_padi(struct pppoe_softc *sc)
LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff", LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff); sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
/* FIXME: PBUF_LINK already allocate a eth_hdr */
/* allocate a buffer */ /* allocate a buffer */
pb = pbuf_alloc(PBUF_LINK, (u16_t)(sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len), PBUF_RAM); pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) { if (!pb) {
return ERR_MEM; return ERR_MEM;
} }
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload + sizeof (struct eth_hdr); p = (u8_t*)pb->payload;
/* fill in pkt */ /* fill in pkt */
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len); PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME); PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
@ -935,12 +942,12 @@ pppoe_send_padr(struct pppoe_softc *sc)
} }
LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff", LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff); sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
pb = pbuf_alloc(PBUF_LINK, (u16_t)(sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len), PBUF_RAM); pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) { if (!pb) {
return ERR_MEM; return ERR_MEM;
} }
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload + sizeof (struct eth_hdr); p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len); PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME); PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
#ifdef PPPOE_TODO #ifdef PPPOE_TODO
@ -975,12 +982,13 @@ pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
err_t res; err_t res;
u8_t *p; u8_t *p;
pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN, PBUF_RAM); pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
if (!pb) { if (!pb) {
return ERR_MEM; return ERR_MEM;
} }
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
pbuf_header(pb, sizeof(struct eth_hdr));
ethhdr = (struct eth_hdr *)pb->payload; ethhdr = (struct eth_hdr *)pb->payload;
ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC); ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
MEMCPY(&ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr)); MEMCPY(&ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
@ -1014,12 +1022,12 @@ pppoe_send_pado(struct pppoe_softc *sc)
len += 2 + 2 + sizeof(sc); len += 2 + 2 + sizeof(sc);
/* include hunique */ /* include hunique */
len += 2 + 2 + sc->sc_hunique_len; len += 2 + 2 + sc->sc_hunique_len;
pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM); pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) { if (!pb) {
return ERR_MEM; return ERR_MEM;
} }
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload + sizeof (struct eth_hdr); p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len); PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE); PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
PPPOE_ADD_16(p, sizeof(sc)); PPPOE_ADD_16(p, sizeof(sc));
@ -1051,12 +1059,12 @@ pppoe_send_pads(struct pppoe_softc *sc)
l1 = strlen(sc->sc_service_name); l1 = strlen(sc->sc_service_name);
len += l1; len += l1;
} }
pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM); pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) { if (!pb) {
return ERR_MEM; return ERR_MEM;
} }
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload + sizeof (struct eth_hdr); p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len); PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME); PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
if (sc->sc_service_name != NULL) { if (sc->sc_service_name != NULL) {
@ -1088,16 +1096,16 @@ pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
len = pb->tot_len; len = pb->tot_len;
/* make room for Ethernet + PPPoE header - should not fail */ /* make room for PPPoE header - should not fail */
if (pbuf_header(pb, sizeof(struct eth_hdr) + PPPOE_HEADERLEN) != 0) { if (pbuf_header(pb, (u16_t)(PPPOE_HEADERLEN)) != 0) {
/* bail out */ /* bail out */
PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num)); PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for PPPoE header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
LINK_STATS_INC(link.lenerr); LINK_STATS_INC(link.lenerr);
pbuf_free(pb); pbuf_free(pb);
return ERR_BUF; return ERR_BUF;
} }
p = (u8_t*)pb->payload + sizeof(struct eth_hdr); p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, 0, sc->sc_session, len); PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
return pppoe_output(sc, pb); return pppoe_output(sc, pb);