* Routine to copy from device local memory into mbufs. * Note that `off' argument is offset into first mbuf of target chain from * which to begin copying the data to. */
| 1056 | * which to begin copying the data to. |
| 1057 | */ |
| 1058 | struct mbuf * |
| 1059 | m_devget(char *buf, int totlen, int off, struct ifnet *ifp, |
| 1060 | void (*copy)(char *from, caddr_t to, u_int len)) |
| 1061 | { |
| 1062 | struct mbuf *m; |
| 1063 | struct mbuf *top = NULL, **mp = ⊤ |
| 1064 | int len; |
| 1065 | |
| 1066 | if (off < 0 || off > MHLEN) |
| 1067 | return (NULL); |
| 1068 | |
| 1069 | while (totlen > 0) { |
| 1070 | if (top == NULL) { /* First one, must be PKTHDR */ |
| 1071 | if (totlen + off >= MINCLSIZE) { |
| 1072 | m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); |
| 1073 | len = MCLBYTES; |
| 1074 | } else { |
| 1075 | m = m_gethdr(M_NOWAIT, MT_DATA); |
| 1076 | len = MHLEN; |
| 1077 | |
| 1078 | /* Place initial small packet/header at end of mbuf */ |
| 1079 | if (m && totlen + off + max_linkhdr <= MHLEN) { |
| 1080 | m->m_data += max_linkhdr; |
| 1081 | len -= max_linkhdr; |
| 1082 | } |
| 1083 | } |
| 1084 | if (m == NULL) |
| 1085 | return NULL; |
| 1086 | m->m_pkthdr.rcvif = ifp; |
| 1087 | m->m_pkthdr.len = totlen; |
| 1088 | } else { |
| 1089 | if (totlen + off >= MINCLSIZE) { |
| 1090 | m = m_getcl(M_NOWAIT, MT_DATA, 0); |
| 1091 | len = MCLBYTES; |
| 1092 | } else { |
| 1093 | m = m_get(M_NOWAIT, MT_DATA); |
| 1094 | len = MLEN; |
| 1095 | } |
| 1096 | if (m == NULL) { |
| 1097 | m_freem(top); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | } |
| 1101 | if (off) { |
| 1102 | m->m_data += off; |
| 1103 | len -= off; |
| 1104 | off = 0; |
| 1105 | } |
| 1106 | m->m_len = len = min(totlen, len); |
| 1107 | if (copy) |
| 1108 | copy(buf, mtod(m, caddr_t), (u_int)len); |
| 1109 | else |
| 1110 | bcopy(buf, mtod(m, caddr_t), (u_int)len); |
| 1111 | buf += len; |
| 1112 | *mp = m; |
| 1113 | mp = &m->m_next; |
| 1114 | totlen -= len; |
| 1115 | } |
no test coverage detected