Freertos and TCP/IP Communication: the Lwip Library
Total Page:16
File Type:pdf, Size:1020Kb
Advanced School on Programmable System-on-Chip for Scientific Instrumentation FreeRTOS and TCP/IP communication: the lwIP library Fernando Rincón [email protected] Smr3160 – ICTP (Nov. & Dic. 2017) Con%en%& ● T'e l$IP TCP/IP s%ac) – The ne%work &%ac) – The socke% conce*% ● +**#ica%ion Arc'i%ec%,res ● #$IP and FreeRT!S ● '%%*&-(($$$.xi#in..com(video(&oc/net$orking-wi%'0#$i*01oc,&ed01ree0 r%o&.h%ml 2 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) #$IP TCP/IP stac) ● #$IP stand& for 2i/'%$ei/'t IP: – Sma## foo%*rin% im*#emen%a%ion – S*ecia##3 $el# &,i%ed 1or embedded sy&%ems ● Su**orts a large n,mber of protoco#& – 5DP, TCP, ICMP6 ARP, ... ● API&- – Ber)e#e3 &oc)et&- ● re9,ire& an !.S. – Ra$ API ● Wi%' or $i%'o,t !S ● 7ore contro#6 b,t more comp#e. to u&e ● Inc#,ded in xilin. SD; – +#&o incl,des driver for <i#in. =%'erne% driver – <+PP1026 is %'e reference a**#ica%ion no%e 3 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) T'e network stack ● T'e net$ork desi/n i& organi>ed a& a layer s%ac). ● =ac' layer provides a set o1 service& to t'e up*er layer and req,ires &ervice& from t'e lo$er layer. ● T'e la3er 'n' o1 a node main%ain& a virt,a# conver&a%ion wi%' t'e same #a3er t'e des%ina%ion node. T'a% conver&a%ion m,&% mee% a s*eci@c *ro%oco#. 4 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Network sockets ● Soc)et- – 8a&ic a4&%rac%ion for ne%$ork pro/rammin/ – Com4ina%ion o1 IP + port – In%er-proce&& comm,nica%ion 5 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Network sockets ● From t'e programming poin% o1 view – Channel a4&%rac%ion ● 8er)eley &oc)e%& (BSD &oc)et& | P!SIX &oc)et&) – De fac%o s%andard API ● lwip_socket(AF_INET, SOCK_STREAM, 0) 2$IP Soc)et API – B#i%e’ version o1 BSD socke%&& client server *roces& *roces& write read 83%es → socket read socket write E 83%es 6 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) +**lication Architectures: su*erloo* ● T$o main a#%erna%ive&- – Su*erloop – mu#%i0%'readed ● S,*erloo*- – 1orever #oop %'a% &e9,ences %'e &e% of %a&)& – int main() { Typica# in &%anda#one im*#emen%a%ion& init_system(); – … Pros- While(1) { ● Sim*#e do_a(); ● do_b(); No OS over'ead do_c(); – Con& ) ● // You’l never get here DiFc,#% %o &ca#e (#ow n,m4er o1 %a&)&) ) ● DiFc,#% %o 4a#ance %ime and %a&)& *rioritie& 7 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) +**#ication architectures: m,#ti0%'readed ● 7,#%i0%'readed: – m,#%iple threads s*a$n to carry o,% m,#%iple ta&)& conc,rren%#3 – =ach ta&) 'a& diferen% priorit3 and timing re9,iremen%& – Re9,ire& an opera%in/ s3&%em – Pro&- ● 7ore mod,#ar arc'itec%,re ● Ta&)& can be *re0empted. Avoid priori%3 inver&ion – Con&- ● 7ore com*#e. and e.%ra over'ead ● Hi/'er memor3 re9,iremen%& ● T'read e.ec,tion i& diFc,#% to te&% 8 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) FreeRTOS Ap*lication Architecture 9 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) FreeRTOS net$ork ap*lication ● Basic %em*#a%e 1or socke% orien%ed *ro/rammin/- – Firs% &e%,p a ne%$ork %'read and &%ar% FreeRTOS %a&) &chedu#er – T'en %'e ne%work %'read- ● Initia#ize& #wip ● Con@/,re& a ne%$or) in%er1ace ● Rise& %'e in%er1ace ● S%ar%& ano%'er %'read 1or %he rece*%ion ● In&%a##& an3 o%'er ne%$or) %a&)& (ne$ %hread&) re9,ired 43 %'e a**#ica%ion – WeC## &,**o&e an ec'o &erver ● Fina##y it de#e%e& it&e#1 – T'ere1ore a1%er ini%ia#i>a%ion &evera# %'reads are active- ● Rece*%ion ● Echo &erver 10 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) FreeRTOS net$ork ap*lication ● Ini%ia#i>a%ion: :e need FreeRT!S %o &%art tas) e.ecu%ion 1or %'e rest o1 %'e *roced,re =9,iva#en% %o a FreeRTOS ta&) 11 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) FreeRTOS net$ork ap*lication ● Ne%$or) confgura%ion t'read Sta%ic IP con@/,ra%ion I (con%in,es in %'e nex% &#ide) 12 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) FreeRTOS net$ork ap*lication ● Ne%$or) confgura%ion t'read Ta&)s t'a% wi## remain active: ● Network data recep%ion ● Echo server (t'a% processes da%a received) This ta&) is J,&% 1or con@/,ra%ion *,rpo&es 13 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Network programmin/ concep%&- Error contro# ● Ne%$or) communica%ion a% t'e IP level i& ,nre#ia4#e ● If relia4i#i%y i& req,ired a% t'e trans*ort layer. Error con%ro# i& re&*onsi4#e for- – De%ec% and discard corr,*% packe%& – ;ee* %rack o1 lo&% and discarded packe%& (re&end) – Discard duplica%e& – 8,Ger o,%0o10order packe%& ● Imp#emen%ed t'ro,/'- – Se9,ence n,m4ers (in *acke%&) – +cknowled/emen% and timers 14 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Network programming concepts: Flo$ con%ro# ● 8o%' t'e sender and receiver adJ,&% t'e tran&mi&&ion s*eed ● &#iding windo$- – 8,Ger u&ed to make %'e tran&mis&ion more eFcien% ● See videos – a& well a& to con%rol the fo$ o1 da%a so tha% the de&%ina%ion doe& no% become overwhelmed wi%h da%a ● T'e de&tination can reduce the si>e o1 t'e window 15 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) 5DP ● 5nrelia4#e *rotoco# – No error con%rol ● T'e client i/nore& i1 %'e pac)et arrived correc%ly to %'e &erver – No fow con%rol ● No $ay %o adj,&t %'e &peed of 4o%' %'e &ender and receiver ● 8,% %'en wh3 using 5DPL – =.%remel3 sim*#e (minim,m over'ead) D The fa&%es% wa3 (lowe&% #a%enc3) – Con%rol can be provided 43 de Aplica%ion La3er ● 8,t %'atC& on you as a *ro/rammer ● No 'elp from %'e ne%$or) &tac) – T'ere are ma3 a**#ication& w'ere loosin/ par% of t'e in1ormation can be %olerated: ● =.. Video conference 16 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) 5DP ● UDP timing diagram T'i& packet $i#l be di&carded 17 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) 5DP ● UDP soc)et Pro/rammin/ Ko$ 18 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) TCP ● Connec%ion0orien%ed *ro%ocol ● Re#iab#e – Retransmission of lo&t or corr,pted pac)e%& – C,m,#ative and se#ective AC;& ● Comp#e. *rotocol $i%' mu#%ip#e *'ase& ('ig'er #atenc36 #o$er %'ro,/'*,%) – Connection e&ta4#ishmen% – Data tran&1er – Connection teardo$n ● 5&ed $'en #oo&in/ in1orma%ion canC% 4e %o#era%ed – HTTP / HTTPS – =0mail6 te.t me&sa/in/ 19 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) TCP ● TCP communica%ion fo$ – Da%a tran&1ers m,&% 4e ackno$ledge& – 8,% ackno$led/e& are packed. No% one per packe% received 20 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) TCP ● TCP soc)et Programming fo$ 21 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) TCP 22 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) TCP 23 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) #$IP performance ● De*end& on – %he concre%e hard$are (a#&o the CP5 no% j,&% t'e ne%wor) in%erface) – and API u&ed (RA: or socke%) 24 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Network too#s: netcat ● T'e s$i&& arm3 kni1e for ne%$or) o*era%ions ● Can be con@/,red to be a c#ient or a &erver- – 5DP exam*le- ● Server: nc 0#, -p <port> PN'ostnameOQ ● Clien%: nc -, N'os%nameO <*or%O – TCP exam*#e- ● Server: nc 0l -p <port> PN'ostnameOQ ● Clien%: nc <'os%nameO <por%O 25 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Net$ork tools: Wireshark ● Mo&% po*,#ar net$or) traFc snifer ● Ca*%,re& and ana#3>es any kind o1 ne%$or) traFc – :i## he#p yo, to unders%and t'e ne%work &%ac) – Pro%ocol di&&ec%or ● 7,#%i0*#a%1orm (even mo4i#e p'one&) – Free so1%$are 26 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017) Net$ork tools: Wireshark Ca*%ured pac)e%& Pac)et enca*&,#a%ion Pac)et con%en%& 27 FreeRT!S + lwIP Smr3160 – ICTP (Nov. & Dic. 2017).