Tải bản đầy đủ - 0 (trang)
9 Mbufs (Memory Buffers) and Output Processing

9 Mbufs (Memory Buffers) and Output Processing

Tải bản đầy đủ - 0trang

structure(namedserv)andthesixth

argumentspecifiesitslength(whichwe'll

seelateris16bytes).Oneofthefirst

thingsdonebythesocketlayerforthis

systemcallistoverifythatthese

argumentsarevalid(i.e.,thepointer

pointstoapieceofmemoryintheaddress

spaceoftheprocess)andthencopythe

socketaddressstructureintoanmbuf.

Figure1.6showstheresultingmbuf.



Figure1.6.Mbufcontainingdestination

addressforsendto.



Thefirst20bytesofthembufisaheader



containinginformationaboutthembuf.

This20-byteheadercontainsfour4-byte

fieldsandtwo2-bytefields.Thetotalsize

ofthembufis128bytes.

Mbufscanbelinkedtogetherusingthe

m_nextandm_nextpktmembers,aswe'll

seeshortly.Botharenullpointersinthis

example,whichisastand-alonembuf.

Them_datamemberpointstothedatain

thembufandthem_lenmemberspecifies

itslength.Forthisexample,m_datapoints

tothefirstbyteofdatainthembuf(the

byteimmediatelyfollowingthembuf

header).Thefinal92bytesofthembuf

dataarea(108-16)areunused(the

shadedportionofFigure1.6).

Them_typememberspecifiesthetypeof

datacontainedinthembuf,whichforthis

exampleisMT_SONAME(socketname).

Thefinalmemberintheheader,m_flags,

iszerointhisexample.



MbufContainingData



Continuingourexample,thesocketlayer

copiesthedatabufferspecifiedinthecall

tosendtointooneormorembufs.The

secondargumenttosendtospecifiesthe

startofthedatabuffer(buff),andthe

thirdargumentisitssizeinbytes(150).

Figure1.7showshowtwombufsholdthe

150bytesofdata.



Figure1.7.Twombufsholding150bytes

ofdata.



Thisarrangementiscalledanmbufchain.

Them_nextmemberineachmbuflinks

togetherallthembufsinachain.

Thenextchangeweseeistheadditionof



twomembers,m_pkthdr.lenand

m_pkthdr.rcvif,tothembufheaderinthe

firstmbufofthechain.Thesetwo

memberscomprisethepacketheaderand

areusedonlyinthefirstmbufofachain.

Them_flagsmembercontainsthevalue

M_PKTHDRtoindicatethatthismbuf

containsapacketheader.Thelenmember

ofthepacketheaderstructurecontains

thetotallengthofthembufchain(150in

thisexample),andthenextmember,rcvif,

we'llseelatercontainsapointertothe

receivedinterfacestructureforreceived

packets.

Sincembufsarealways128bytes,

providing100bytesofdatastorageinthe

firstmbufonthechainand108bytesof

storageinallsubsequentmbufsonthe

chain,twombufsareneededtostore150

bytesofdata.We'llseelaterthatwhenthe

amountofdataexceeds208bytes,instead

ofusingthreeormorembufs,adifferent

techniqueisusedalargerbuffer,typically

1024or2048bytes,calledaclusteris

used.

Onereasonformaintainingapacket



headerwiththetotallengthinthefirst

mbufonthechainistoavoidhavingtogo

throughallthembufsonthechaintosum

theirm_lenmemberswhenthetotal

lengthisneeded.



PrependingIPandUDPHeaders

Afterthesocketlayercopiesthe

destinationsocketaddressstructureinto

anmbuf(Figure1.6)andthedataintoan

mbufchain(Figure1.7),theprotocollayer

correspondingtothesocketdescriptor(a

UDPsocket)iscalled.Specifically,theUDP

outputroutineiscalledandpointerstothe

mbufsthatwe'veexaminedarepassedas

arguments.Thisroutineneedstoprepend

anIPheaderandaUDPheaderinfrontof

the150bytesofdata,fillintheheaders,

andpassthembufstotheIPoutput

routine.

Thewaythatdataisprependedtothe

mbufchaininFigure1.7istoallocate

anothermbuf,makeitthefrontofthe

chain,andcopythepacketheaderfrom

thembufwith100bytesofdataintothe



newmbuf.Thisgivesusthethreembufs

showninFigure1.8.



Figure1.8.MbufchainfromFigure1.7

withanothermbufforIPandUDP

headersprepended.





TheIPheaderandUDPheaderarestored

attheendofthenewmbufthatbecomes

theheadofthechain.Thisallowsforany

lower-layerprotocols(e.g.,theinterface

layer)toprependitsheadersinfrontof

theIPheaderifnecessary,withouthaving

tocopytheIPandUDPheaders.The

m_datapointerinthefirstmbufpointsto

thestartofthesetwoheaders,andm_len

is28.Futureheadersthatfitinthe72

bytesofunusedspacebetweenthepacket



headerandtheIPheadercanbe

prependedbeforetheIPheaderby

adjustingthem_datapointerandthe

m_lenaccordingly.Shortlywe'llseethat

theEthernetheaderisbuilthereinthis

fashion.

Noticethatthepacketheaderhasbeen

movedfromthembufwith100bytesof

dataintothenewmbuf.Thepacketheader

mustalwaysbeinthefirstmbufonthe

chain.Toaccommodatethismovementof

thepacketheader,theM_PKTHDRflagis

setinthefirstmbufandclearedinthe

secondmbuf.Thespacepreviously

occupiedbythepacketheaderinthe

secondmbufisnowunused.Finally,the

lengthmemberinthepacketheaderis

incrementedby28bytestobecome178.

TheUDPoutputroutinethenfillsinthe

UDPheaderandasmuchoftheIPheader

asitcan.Forexample,thedestination

addressintheIPheadercanbeset,but

theIPchecksumwillbeleftfortheIP

outputroutinetocalculateandstore.

TheUDPchecksumiscalculatedand



storedintheUDPheader.Noticethatthis

requiresacompletepassofthe150bytes

ofdatastoredinthembufchain.Sofar

thekernelhasmadetwocompletepasses

ofthe150bytesofuserdata:onceto

copythedatafromtheuser'sbufferinto

thekernel'smbufs,andnowtocalculate

theUDPchecksum.Extrapassesoverthe

datacandegradetheprotocol's

performance,andinlaterchapterswe

describealternativeimplementation

techniquesthatavoidunnecessarypasses.

AtthispointtheUDPoutputroutinecalls

theIPoutputroutine,passingapointerto

thembufchainforIPtooutput.



IPOutput

TheIPoutputroutinefillsintheremaining

fieldsintheIPheaderincludingtheIP

checksum,determinestheoutgoing

interfacetowhichthedatagramshouldbe

given(thisistheIProutingfunction),

fragmentstheIPdatagramifnecessary,

andcallstheinterfaceoutputfunction.



Assumingtheoutgoinginterfaceisan

Ethernet,ageneral-purposeEthernet

outputfunctioniscalled,againwitha

pointertothembufchainasanargument.



EthernetOutput

ThefirstfunctionoftheEthernetoutput

functionistoconvertthe32-bitIPaddress

intoitscorresponding48-bitEthernet

address.ThisisdoneusingARP(Address

ResolutionProtocol)andmayinvolve

sendinganARPrequestontheEthernet

andwaitingforanARPreply.Whilethis

takesplace,thembufchaintobeoutputis

held,waitingforthereply.

TheEthernetoutputroutinethenprepends

a14-byteEthernetheadertothefirst

mbufinthechain,immediatelybeforethe

IPheader(Figure1.8).Thiscontainsthe

6-byteEthernetdestinationaddress,6byteEthernetsourceaddress,and2-byte

Ethernetframetype.

Thembufchainisthenaddedtotheendof

theoutputqueuefortheinterface.Ifthe



interfaceisnotcurrentlybusy,the

interface's"startoutput"routineiscalled

directly.Iftheinterfaceisbusy,itsoutput

routinewillprocessthenewmbufonits

queuewhenitisfinishedwiththebuffers

alreadyonitsoutputqueue.

Whentheinterfaceprocessesanmbuf

that'sonitsoutputqueue,itcopiesthe

datatoitstransmitbufferandinitiatesthe

output.Inourexample,192bytesare

copiedtothetransmitbuffer:the14-byte

Ethernetheader,20-byteIPheader,8-byte

UDPheader,and150bytesofuserdata.

Thisisthethirdcompletepassofthedata

bythekernel.Oncethedataiscopied

fromthembufchainintothedevice's

transmitbuffer,thembufchainisreleased

bytheEthernetdevicedriver.Thethree

mbufsareputbackintothekernel'spool

offreembufs.



SummaryofUDPOutput

InFigure1.9wegiveanoverviewofthe

processingthattakesplacewhena

processcallssendtototransmitasingle



UDPdatagram.Therelationshipofthe

processingthatwe'vedescribedtothe

threelayersofkernelcode(Figure1.3)is

alsoshown.



Figure1.9.Processingperformedbythe

threelayersforsimpleUDPoutput.



Functioncallspasscontrolfromthesocket

layertotheUDPoutputroutine,totheIP

outputroutine,andthentotheEthernet

outputroutine.Eachfunctioncallpassesa

pointertothembufchaintobeoutput.At

thelowestlayer,thedevicedriver,the

mbufchainisplacedonthedevice's

outputqueueandthedeviceisstarted,if

necessary.Thefunctioncallsreturnin



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

9 Mbufs (Memory Buffers) and Output Processing

Tải bản đầy đủ ngay(0 tr)

×