/
windows_port.tex
1774 lines (1442 loc) · 72.1 KB
/
windows_port.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[11pt]{article}
%\usepackage[dvips]{changebar}
\usepackage{subfigure}
\usepackage{fullpage}
\usepackage{setspace}
\usepackage{times}
\usepackage{latexsym}
\usepackage{epsfig}
\usepackage{ifpdf}
%\usepackage{graphicx}
\usepackage{xspace}
\usepackage{color}
\usepackage{amsmath}
\usepackage{rotating}
\usepackage{moreverb}
\usepackage{listings}
\usepackage{alltt}
\usepackage{stmaryrd}
\usepackage{url}
%\usepackage{hyperref}
%\usepackage{tabularx}
%\usepackage[dvipdf]{graphics}
%\usepackage[dvips]{graphicx}
%\usepackage{xorp}
% The following is needed in order to make the code compatible
% with both latex/dvips and pdflatex.
%\ifx\pdftexversion\undefined
%\usepackage[dvipdf]{graphicx}
%\else
\usepackage{graphicx}
%\DeclareGraphicsRule{*}{mps}{*}{}
%\fi
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\newcommand{\etc}{\emph{etc.}\xspace}
\newcommand{\ie}{\emph{i.e.,}\xspace}
\newcommand{\eg}{\emph{e.g.,}\xspace}
%\newcommand{\comment}[1]{{\color{gray}[\textsf{#1}]}}
%\newcommand{\comment}[1]{}
% Changebar stuff
% \newenvironment{colorcode}{\color{blue}}{}
% \renewcommand{\cbstart}{\begin{colorcode}}
% \renewcommand{\cbend}{\end{colorcode}}
% \pagestyle{empty}
\begin{document}
\title{XORP Windows Support Overview \\
\vspace{1ex}
Version 1.8-CT}
\author{ XORP, Inc. and individual contributors \\
{\it http://www.candelatech.com/xorp.ct/} \\
{\it xorp-users@xorp.org}
}
\date{June 1, 2010}
\maketitle
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
As of XORP 1.7, Microsoft Windows is no longer supported. This
document is kept for posterity's sake.
This document provides an overview of XORP's support for Microsoft Windows.
Much of XORP's internal library implementation is specifically targeted
at UNIX-like operating environments. Here, we summarize the steps involved
in the initial port, as well as describing how the Windows support code
functions in detail. It is aimed at developers coming from a Windows
development background.
To summarize current Windows support at the time of writing:
\begin{itemize}
\item IPv4 unicast routing is fully supported.
\item IPv6 control plane traffic appears to work under Windows
``Longhorn'' Server, however, access to the IPv6 forwarding plane
is not supported.
\item The multicast forwarding plane is not supported. The architecture
for this in Windows differs significantly from other operating systems.
\end{itemize}
XORP is composed of around 360,000
\footnote{Measured using C and C++ Code Counter: \url{http://cccc.sourceforge.net/}}
actual lines of code at the time of writing.
Prior to the port, the code was only portable amongst
a small subset of UNIX-like operating systems. As such, the effort
considerably improved the portability of the XORP code base as a whole.
The Windows port presented the XORP team at ICSI with a significant technical
challenge which was met successfully.
A XORP router was
demonstrated in July 2005, at Microsoft Research in Redmond.
It was hosted on commodity PC hardware, running Windows Server 2003,
and successfully handled a full eBGP feed of around 120,000 IPv4 route prefixes.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{The initial port}
Work on the initial port of XORP to Microsoft Windows began
in February 2005. An iterative, bottom-up approach was used, where a build attempt
was made in several stages with different tool chains at each stage. As the
port progressed, the error feedback from the previous stage was used to rewrite
areas of the code base which did not compile or which were significantly
incompatible with Windows systems programming idioms.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Constraints}
XORP is subject to certain portability constraints. At the moment,
whenever a new platform is targeted, the following constraints apply at a minimum:
\begin{itemize}
\item The ability to run the GNU {\tt configure} script.
This requires a POSIX compliant shell.
\item The ability to read and manipulate the target's IPv4 forwarding plane.
\item The compiler must support variadic macros, either in a manner which is
C99 compatible, or with GNU extensions.
\item Changes to support the new platform should be minimally intrusive.
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{64-bit forward compatibility}
Whilst every effort has been made to use APIs which are forward compatible
with operation under Win64, it has not been possible to test XORP's
native operation on 64-bit Windows systems due to the lack of a MinGW
toolchain for these systems. The Win32 binaries have however been
compiled and run on 64-bit systems on a purely experimental basis.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Port stages}
This section describes each evolutionary stage of the Windows port,
in terms of the tool chain used.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Cygwin}
In the beginning,
Cygwin
\footnote{\url{http://www.cygwin.com/}},
a UNIX-like environment for Windows,
was used as the first stage:
\begin{itemize}
\item It provides an emulated, Windows-hosted GNU {\tt gcc} and {\tt g++} tool chain.
\item It provides a POSIX-like emulation environment.
\item It is {\em not} a Windows subsystem
\footnote{\url{http://technet.microsoft.com/en-us/library/cc767884.aspx}}
\item The binaries it produces require the use of the {\tt CYGWIN.DLL} runtime DLL,
which provides the POSIX-like emulation.
\item A POSIX-compliant shell is available, capable of running GNU {\tt configure} scripts.
\end{itemize}
The objective at this point was simply to get as much of the XORP code base
to compile as possible, in a self-hosting manner, on Windows.
Cygwin's runtime libraries are mostly compatible with POSIX and C99 standards,
making this an easier task than it would otherwise be.
However, due to the lack of access to native Windows APIs, including {\tt CreateProcess()},
the XORP Router Manager and CLI could not be ported at this stage.
Furthermore, the IPv4 forwarding table was not accessible from the FEA.
Only IPv4 networking facilities are supported by Cygwin, using a
limited subset of the Berkeley Socket APIs. There is no access to the
forwarding plane.
The use of {\tt CYGWIN.DLL} was not compatible with XORP's license at the
time of the port. The DLL itself is licensed under the GNU General Public License version 2,
{\em not} the ``Library'' version of this license. A general release of this
development code would have tainted all XORP binaries with the GPLv2 license.
Porting to Cygwin, however, allowed several more general problems in the code
base to be found and resolved, in preparation for the next stage.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{MinGW}
The next stage in the evolution of the port was to adopt the use
of Minimalist GNU for Windows (MinGW)
\footnote{\url{http://www.mingw.org/}},
a freely available port of
the GNU {\tt gcc} tool chain capable of producing native Win32 binaries:
\begin{itemize}
\item It provides a native, Windows-hosted GNU {\tt gcc} and {\tt g++} tool chain.
\item It does {\em not} provide a POSIX emulation environment.
\item It ships its own version of the Microsoft Windows SDK
headers and libraries, {\tt w32api}, which is compatible with {\tt gcc}.
\item The binaries it produces use the Windows system native runtime, {\tt MSVCRT.DLL}
\footnote{This is the C runtime library used by Windows system components;
it is not part of Microsoft Visual C++.}.
\item It is {\em not} a Windows subsystem. However, the binaries it produces
are clients of the Win32 subsystem.
\end{itemize}
Porting XORP to MinGW provided the project with the necessary means to use
native Windows system APIs.
Minimalist access to the IPv4 forwarding plane was now possible through the
IP Helper API, {\tt IPHLPAPI.DLL}.
Redistribution of binaries produced with MinGW is compatible with the Microsoft
End-User License Agreement. There are no additional requirements for the binaries
which are shipped, as the runtime DLL used is part of Windows itself, thus
resolving the incompatibility which existed between Cygwin and the
XORP license at that time.
%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Windows SDK header support}
A caveat of using MinGW is that it does not benefit from Microsoft's
ongoing support, as regards SDK headers and libraries; it is purely dependent
upon user contributions. Microsoft's SDKs are targeted at Microsoft-compatible
compilers, as such they use compiler pragmas which are not compatible with
the GNU {\tt gcc} or {\tt g++} compilers. MinGW therefore ships its own SDK-like package
known as {\tt w32api}.
%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{GNU {\tt configure} capability}
Whilst the use of MinGW satisfied the requirement that a {\tt gcc}-compatible compiler be used,
it did not satisfy the requirement to be able to run GNU {\tt configure}.
However, there is a UNIX-like environment built on MinGW, called
MSYS\footnote{\url{http://www.mingw.org/wiki/msys}}, which
contains a POSIX-compliant shell capable of running GNU {\tt configure}. By installing
an additional package, {\tt msysDTK}, it is possible to run versions of the GNU
{\tt automake}, {\tt autoconf} and {\tt libtool} tools which are specifically customized for
the MinGW and MSYS environments.
The MSYS tools use a library of their own which is derived from Cygwin, however,
this is completely separate from Cygwin itself, and is not used by MinGW compiled processes,
which are fully fledged Win32 subsystem processes.
%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{C99 and POSIX compatibility}
It was therefore possible, by using the combination of MinGW and MSYS, to begin
porting XORP to a native Windows runtime environment. This approach,
however, has problems of its own.
Unlike Cygwin, MinGW has no POSIX emulation capability. There is a very thin veneer
of C99 and POSIX glue, in the form of various library functions.
The Windows system runtime, {\tt MSVCRT.DLL} supplies emulations of certain POSIX
or C runtime functions, and macros, as a convenience. Unfortunately, not all of these
behave identically to their counterparts in the UNIX environments.
One example which stands out is Microsoft's implementation of
the {\tt snprintf()} C string formatting function. The XRL layer has a strong, indirect
dependency on this function, and it behaves quite differently in terms of
its return value. A work around for this can be seen in {\tt libxorp/c\_format.cc}.
The {\tt -DNO\_OLDNAMES} preprocessor definition can be used to omit some of these definitions
from the MinGW SDK headers. XORP's use of names affected by this definition applies
mostly to file and directory access.
%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{MinGW port evolution}
The evolution of the MinGW based port has therefore followed three stages:
\begin{itemize}
\item MinGW without {\tt -DNO\_OLDNAMES}.
The initial stage, used to wean XORP away from the dependencies on the Cygwin
environment to compile, and the most labor intensive stage of the porting process.
\item MinGW with {\tt -DNO\_OLDNAMES}.
An intermediate stage, used to wean XORP code onto using the POSIX-like
file I/O routines shipped in {\tt MSVCRT.DLL}.
At this point in development, use of the {\tt XorpFd} class was tightened, in order to detect
programmer error which might result from confusing the use of the POSIX
file descriptor type {\tt int} with the Windows type {\tt HANDLE}.
\item MinGW without {\tt -DNO\_OLDNAMES}.
The final stage of the port, used to simplify some of the header file glue
required by the Windows port, as the use of {\tt -DNO\_OLDNAMES} was found to clash
with some of the Multiple Provider Router (MPR) header files in the Windows SDK.
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Architectural Overview}
This section summarizes the main changes in the XORP architecture which
were made to accomodate Windows systems.
XORP was initially developed on the FreeBSD platform. This heritage
is reflected in the design and implementation of the {\tt EventLoop}
and {\tt SelectorList} classes in the main XORP class library,
{\tt libxorp}. FreeBSD has a history of following POSIX standards
fairly closely, therefore it has been a good choice for a reference
platform to date.
For a broader overview of the issues involved in capturing I/O
in object-oriented design patterns, please refer to \cite{schmidt:1999:AOA}.
\subsection{Portable I/O interfaces}
All I/O in XORP is event driven.
The {\tt AsyncFileReader}, {\tt AsyncFileWriter} and {\tt BufferedAsyncReader}
classes do not provide ``true'' asynchronous I/O
facilities; rather, they each capture an {\tt Adaptor}
design pattern, for building C++ event-driven I/O
facilities.
The {\tt EventLoop}
\footnote{Section 5, ``The main loop'', \cite{xorp:xorpdev_101}.}
class captures a {\tt Facade} pattern, for access to timer and
event-driven I/O facilities.
The interface is independent of the underlying operating system.
Each I/O class interacts with {\tt EventLoop} to register notifications
of pending I/O events.
\begin{figure}
\centering
\includegraphics[scale=0.5,bb=0 0 622 456]{figs/windispatcher}
% windispatcher.pdf: 622x456 pixel, 72dpi, 21.94x16.09 cm, bb=0 0 622 456
\caption{UML class diagram of portable I/O facilities}
\end{figure}
Historically, XORP only supported UNIX-like systems. Therefore,
the design of the I/O facilities is layered on top of
POSIX non-blocking I/O, as they are natively supported by such systems.
An advantage of this approach is that I/O operations are inherently
self-serializing. The complexity of I/O multiplexing has been delegated
to the kernel, by relying on the atomicity of each POSIX system call.
Explicit locking is therefore not required in
XORP processes. Parallelism within UNIX-like kernels is a wholly
separate topic, and is outside the scope of this document.
\subsection{POSIX I/O implementation}
The {\tt EventLoop} contains an instance of {\tt SelectorList},
which encapsulates POSIX {\tt select()} based I/O multiplexing
in a C++ class.
The {\tt select()} system call
is used as it is portable between POSIX systems.
The POSIX I/O model in XORP makes the following assumptions:
\begin{itemize}
\item Processes run in a single primary thread.
\item The operating system supports POSIX ``non-blocking'' I/O operations.
\item The operating system will serialize I/O on behalf of the process.
\item Notifications of I/O events are {\em level-triggered} interrupts
\footnote{This analogy is borrowed from digital logic design.}.
\end{itemize}
The implementation of {\tt SelectorList} can be considered to follow
the {\tt Reactor} object-oriented design pattern \cite{schmidt:reactor}.
\subsection{Windows I/O implementation}
The APIs available for I/O across the Windows system reflect the
Windows kernel's different design philosophy from that of UNIX.
Serialization is the responsibility of the user-space application,
as the kernel is intended to run with a high degree of parallelism.
As such, the Windows APIs for I/O are almost all implemented to block
the calling thread.
The Windows I/O model in XORP makes the following assumptions:
\begin{itemize}
\item Win32 processes may contain multiple threads.
\item Threads in the Win32 subsystem are implemented as
a thin veneer on top of Windows kernel threads.
These are treated as being cheap and easily available
\footnote{This is reflected in the logo art for the Windows NT 4.0 release.}.
\item The operating system {\em does not} support POSIX ``non-blocking'' I/O operations.
\item The operating system {\em does not} serialize I/O on behalf of the process,
unless alternative APIs are used.
\item Notifications of I/O events are {\em edge-triggered} interrupts.
\end{itemize}
The implementation of {\tt WinDispatcher} can also be considered to capture
the {\tt Reactor} pattern.
Should threading support ever be required within the XORP programming model,
the design assumptions behind the EventLoop will need to be revisited.
The UNIX design philosophy persists to this day in XORP's reference
platforms.
The use of Windows asynchronous I/O primitives, \eg I/O completion ports
\footnote{\url{http://technet.microsoft.com/en-us/sysinternals/bb963891.aspx},}
has been considered for XORP, but was ruled out as it effectively meant
re-architecting the entire I/O model to support ``true'' asynchronous I/O.
An example of a design pattern which captures the requirements
of asynchronous I/O is the {\tt Proactor} pattern \cite{schmidt:coplien_pattern_1995}.
\subsection{I/O software interrupts}
The {\tt EventLoop} has responsibility for I/O event dispatch within XORP processes.
As a result of the difference in I/O strategies between UNIX-like kernels, and the
Windows kernel, the disposition of I/O events is correspondingly different.
Here, we compare {\tt WSAEnumNetworkEvents()} with
{\tt select()} in the context of socket I/O notifications:
\begin{center}
\begin{minipage}[0]{12cm}
\begin{tabular}{ | l | l | l | l | }
\hline
Multiplexing API & Read & Write & Exception \\ \hline\hline
{\tt select()} &
Level &
Level
\footnote{{\tt select()} indicates that a given file descriptor is writable
every time it is called.} &
Level \\
{\tt WSAEnumNetworkEvents()}
\footnote{The ``signalled'' condition is cleared if a Windows Event object is supplied as an argument.}
\footnote{One, and only one, indication will be returned that a previously posted write to a socket has completed,
or that a socket now has inbound readable data.}
&
Edge
&
Edge
&
Edge
\footnote{Error returns from previous Winsock I/O calls are in fact
dispatched inline with the event code corresponding to the
previously posted operation.} \\ \hline
\end{tabular}
\end{minipage}
\end{center}
These multiplexing functions effectively poll for the
status of {\em software interrupts} on each file handle
provided as arguments.
As will be seen further on in the documentation for
{\tt WinDispatcher}
itself,
this table does not provide the full picture, as a number of other
work arounds are used there to account for the
differences in Windows I/O facilities.
The interrupts generated
by the POSIX {\tt select()} call are {\it level-triggered}; the change
in state persists in the time domain, \ie an event
condition on a POSIX file descriptor will persist between subsequent
calls to {\tt select()}, until the I/O is serviced.
With {\tt select()}, a read event persists until data is no longer
readable from the socket.
The interrupts generated by the Windows kernel API {\tt WaitForMultipleObjects()},
and the Winsock 2 API {\tt WSAEventSelect()}, are {\it edge-triggered}; the
change in state happens only once in the time domain and does not persist.
If an I/O event is not dispatched immediately upon return from the
multiplexing function in use, the event state will {\em not} be preserved
by the underlying operating system.
Workarounds for these key differences in behaviour are described
further on, in the documentation for the {\tt AsyncFileReader} and
{\tt AsyncFileWriter} classes.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Global scope changes}
This section summarizes the changes to the XORP code base,
necessary for Windows support, which affected the global C and C++
identifier scope.
The bulk of the changes to support Microsoft Windows were checked into the
original XORP CVS repository on December 21, 2005.
To discover and explore
areas of the source tree which have been conditionalized for Windows support,
the reader may wish to use a source browsing application,
{\eg Red Hat Source Navigator}, and peform a search for references to
the preprocessor conditional
{\tt HOST\_OS\_WINDOWS}.
%%%%%%%%%%%%%%%%%%%%%
\subsection{GNU {\tt configure} changes}
XORP is normally built with a fairly rigorous set of compiler warning flags.
Unfortunately, these warnings are not compatible with the MinGW system headers,
or the {\tt w32api} headers.
In the Windows SDKs as normally shipped by Microsoft, this issue is addressed
by the use of several compiler pragmas specific to Microsoft Visual C++ and
various compatible Windows-hosted compiler tool chains. In many cases,
compiler vendors, \eg Borland and Watcom, ship versions of the Microsoft SDKs
specifically tailored to the idiosyncracies of their own compilers.
These pragmas are not present in MinGW or the {\tt w32api}. Therefore, the GNU
{\tt configure} script for XORP specifically removes the following compiler
warning flags to enable the compiler to be built:
\begin{itemize}
\item {\tt -Wmissing-declarations}
\item {\tt -Wpointer-arith}
\item {\tt -Wstrict-prototypes}
\item {\tt -Wcast-align}
\end{itemize}
The {\tt configure} script is also responsible for detecting various properties
of the networking stack as seen by the build environment. Originally these
were part of {\tt configure.in} itself, however, as complexity grew, these tests
were parceled off into various scripts in the sub-directory ``config''.
The scripts forcibly check that certain Windows SDK headers are available, and
that certain multicast-related headers are included in the correct order.
For more information about working with GNU {\tt automake}, {\tt autoconf} and {\tt libtool},
please refer to \cite{goat:2000}.
Finally, XORP uses a number of default file and directory paths which are
inferred from the environment by the GNU {\tt configure} script. As the script
assumes UNIX-style paths, some conversions are performed to allow the
UNC-style or DOS-style path to be propagated into preprocessor headers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Header file changes}
It was discovered early on that several changes to the code
base were needed at global scope, to get XORP to compile in the MinGW environment.
Almost all XORP source files include the header file
{\tt libxorp/xorp.h} to import definitions which are generated by the GNU {\tt configure}
script at top level.
However, some of these definitions are not compatible with Windows APIs.
The changes to address this were split into three stages:
\begin{itemize}
\item {\tt xorp\_osdep\_begin.h}
This file contains identifiers which need to be visible after the C++
{\tt using namespace std;} declaration, but before anything else.
\item {\tt xorp\_osdep\_mid.h}
This file contains identifiers which need to be visible after {\tt libxorp/xorp.h} has pulled in
the C standard library headers, and certain other headers.
Most of the portable C functions which XORP requires, but which may
not be available on all target platforms, are prototyped here.
Most of the Windows SDK headers are referenced here also.
\item {\tt xorp\_osdep\_end.h}
This file contains identifiers which must be defined after any other definitions in {\tt libxorp/xorp.h},
but before anything which depends upon {\tt libxorp/xorp.h}.
This stage is mostly used to clean up collisions in the global C/C++ namespace
between Windows APIs imported in the previous stage, and other XORP header files.
\end{itemize}
Later, in order to obtain access to definitions exported for the Multiple
Provider Router (APIs), it was necessary to add {\tt -DMPR50=1} to the global
namespace from within the GNU {\tt configure} script.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Linkage changes}
In keeping with the principle of making minimally invasive changes to the
code base, all XORP processes are linked against the following DLL import
libraries:
\begin{itemize}
\item {\tt ADVAPI32.DLL}, the Advanced API services library, used for security and registry calls.
\item {\tt WS2\_32.DLL}, the Winsock 2
\footnote{\url{http://msdn.microsoft.com/en-us/library/ms740673(VS.85).aspx}}
API.
\item {\tt IPHLPAPI.DLL}, the IP Helper
\footnote{\url{http://msdn.microsoft.com/en-us/library/aa365798(VS.85).aspx}}
API.
\item {\tt MPRAPI.DLL}, the Multiple Provider Router
\footnote{\url{http://msdn.microsoft.com/en-us/library/aa373762(VS.85).aspx}}
(MPR) 5.0 API.
\end{itemize}
This linkage is in addition to the base Win32 services, {\tt KERNEL32.DLL}, and
the C runtime library, {\tt MSVCRT.DLL}, which are included by default in
the link map of any Win32 application compiled using MinGW.
Binaries are always targeted for the Win32 console mode subsystem. There
has, to date, been no requirement for a native graphical user interface.
The MPR subsystem in Windows is somewhat byzantine. The documentation is verbose and
scattered across many parts of the Microsoft Developer Network (MSDN).
Developers are strongly encouraged to study the example code in the SDK directly, and refer back
to the MSDN API documentation to understand individual functions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Changes to the {\tt libxorp} main class library}
The {\tt libxorp} library is a rich class library which provides most of the functionality
required by XORP processes. The bulk of the Windows support changes are located here,
as one might reasonably expect.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Low-level C support code}
\subsubsection{{\tt xlog.c}}
This file contains the low-level XORP event log functionality. As such, it
is used throughout the code base.
The changes here are to explicitly use the Standard I/O
facilities offered by {\tt MSVCRT.DLL} where available, to
work around the lack of the ISO C90 standard {\tt strftime()}
\footnote{\url{http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html}}
function,
and to account for the differences in the behaviour of
Microsoft's implementation of
{\tt snprintf()}
\footnote{\url{http://msdn.microsoft.com/en-us/library/f30dzcf6.aspx}},
as compared with a C99
\footnote{\url{http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf}}
compliant version
\footnote{\url{http://www.ijs.si/software/snprintf/}}.
\subsubsection{{\tt win\_io.c}}
This file contains Windows-specific low-level utility functions, some of which emulate
certain aspects of the POSIX non-blocking
{\tt read()}
\footnote{\url{http://opengroup.org/onlinepubs/007908775/xsh/read.html}}
API.
\begin{center}
\begin{tabular}{ | l | p{8cm} | }
\hline
Function & Purpose \\
\hline\hline
{\tt win\_con\_input\_filter()} &
Convert the raw keycode input from a Windows console window to that of a {\tt vt100} terminal. \\
{\tt win\_con\_read()} &
Emulate a POSIX non-blocking {\tt read()}
from a Windows console window {\tt HANDLE}. \\
{\tt win\_pipe\_read()} &
Emulate a POSIX non-blocking {\tt read()} from a Windows anonymous or named pipe {\tt HANDLE}. \\
{\tt win\_strerror()} &
Wrapper for {\tt FormatMessageA()}. \\
\hline
\end{tabular}
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{C++ support functions}
%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt c\_format.cc}}
This file contains the {\tt c\_format()} function, which provides C-style
formatting facilities in a C++ environment. The {\tt libxipc} library has
a strong coupling to this function. Workarounds are implemented here
to work around the lack of a C99 standards compliant {\tt snprintf()}
function in the {\tt MSVCRT.DLL} C runtime library.
%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt utils.hh}}
This header file contains the path conversion functions, which
are mostly declared {\tt inline}. These are used by the Router Manager.
The functions
{\tt unix\_path\_to\_native()} and
{\tt is\_absolute\_path()}
are particularly important, as the Router Manager
uses UNIX-style paths both internally and in the
command template files.
A group of C++ preprocessor macros act as aliases for the
different delimiter sequences on each platform:
\begin{center}
\begin{tabular}{ | l | l | }
\hline
Macro & Meaning \\ \hline\hline
{\tt PATH\_DELIMITER\_CHAR} & File/directory path delimiter as {\tt char} \\
{\tt PATH\_DELIMITER\_STRING} & File/directory path delimiter as {\tt char[]} \\
{\tt PATH\_ENV\_DELIMITER\_CHAR} & Environment variable delimiter as {\tt char} \\
{\tt PATH\_ENV\_DELIMITER\_STRING} & Environment variable delimiter as {\tt char[]} \\
{\tt EXECUTABLE\_SUFFIX } & {\tt char[]} suffix this platform requires for binary executables \\
\hline
\end{tabular}
\end{center}
%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt popen.cc}}
This file contains functions which build on the functionality of {\tt win\_io.c}
to emulate the POSIX {\tt popen()}
\footnote{\url{http://www.opengroup.org/onlinepubs/009695399/functions/popen.html}}
API function. The main changes are as follows:
\begin{itemize}
\item The Windows API functions {\tt CreateProcess()} and {\tt ResumeThread()}
are used instead of the POSIX functions {\tt fork()} and {\tt execve()}
to run child processes in a controlled manner.
\item The {\tt \_open\_osfhandle()} function from {\tt MSVCRT.DLL}
is used to implement C {\tt stdio} on top of each Windows anonymous pipe {\tt HANDLE}.
\item The Windows-specific function {\tt pgethandle()}
was introduced to allow clients to retrieve the {\tt HANDLE} of a Windows
child process, given its process ID.
{\em This code is not thread safe}, as a static array is used to hold the mapping.
\end{itemize}
XORP's emulation of {\tt popen()} on Windows does not behave identically to that in POSIX
systems, due to the following differences in pipe behaviour between these systems:
\begin{center}
\begin{tabular}{ | p{5cm} | p{5cm} | }
\hline
POSIX pipes & Windows pipes \\ \hline\hline
Non-blocking reads supported &
Non-blocking reads not {\em fully} supported \\ \hline
Non-blocking writes supported &
Writes {\em always} block writer thread, until data is read at the remote endpoint \\ \hline
In-flight data written on close &
All data lost on close \\ \hline
{\tt SIGCHLD} process signal is dispatched if child process dies prematurely &
No process signals; process handle is signalled if child process dies prematurely \\ \hline
\end{tabular}
\end{center}
The {\tt WinDispatcher} class will emulate
non-blocking reads by polling the pipe for pending input data
on a periodic quantum, {\tt POLLED\_INTERVAL\_MS}.
Pipe handles must be explicitly added to the {\tt EventLoop} in a XORP
application process to ensure that they are serviced in this way.
Finally, due to the differences between how Windows and POSIX systems
signal the premature death of a child process, the process handle must
be added to the {\tt EventLoop} and an I/O event callback. Windows
does {\em not} have an equivalent of the inline {\tt EPIPE} error
return which works consistently with Windows pipes.
Each Windows process has a {\tt HANDLE} which may be
waited on like any other Windows kernel object.
As will be seen further on in this document, the {\tt RunCommand} class
uses this technique to achieve portability.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{C++ support classes}
This sub-section documents the Windows specific changes to the
C++ classes in the core library, in order of increasing
component complexity.
One exception is
{\tt WinDispatcher}, which is complex enough to require
its own sub-section.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt XorpFd}}
This class encapsulates a file descriptor. Recall that POSIX systems
may use the C primitive data type {\tt int} to hold a file descriptor.
Windows, however, uses the type {\tt HANDLE} defined in the Windows
SDK headers.
By encapsulating file descriptors in a class, the XORP code base
could then be refactored to catch situations where these types were
being mixed inappropriately. As this is a type which is often embedded
in STL containers, various binary operators are defined inline.
The Windows version of this class has a number of additional C++ member
functions which exist to support Windows-specific code elsewhere in the
code base. Not all of these are documented, as their names are self
explanatory; please refer to the source file {\tt libxorp/xorpfd.hh}.
Their behaviour is dependent upon the type of the {\tt HANDLE} assigned
to the {\tt XorpFd} instance. Whenever a {\tt XorpFd} is assigned, the
type of the encapsulated Windows {\tt HANDLE} is re-evaluated by a
private member function, {\tt XorpFd::get\_type()}. The copy
constructor is optimized not to call this function.
Using this class also hides the code complexity which would otherwise
be introduced when calling Winsock API functions.
The types {\tt HANDLE} and {\tt SOCKET} use different underlying C types.
{\tt XorpFd} is polymorphic between them.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt IoEventType}}
This is an {\tt enum} type, introduced as an abstraction for
the types of I/O event callbacks registered with the
{\tt EventLoop}. It reflects a subset of the possible
event types in Windows, and a superset of the event types
available on POSIX systems.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt IoEventCb}}
This is a {\tt typedef} of a XORP callback type, which is used
for convenience and brevity when registering I/O event callbacks
with the {\tt EventLoop}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt TimeVal}}
This utility class is an analogue of the POSIX {\tt struct timeval}. It is
used throughout the code base.
The base Windows SDK type used is {\tt FILETIME}. This reflects the APIs
used in the low level timer code.
C++ member function overrides exist for {\tt TimeVal::copy\_in()}
and {\tt TimeVal::copy\_out()}.
Other changes exist here to allow conversion between
times measured since the UNIX epoch, January 1st 1970,
and to address the difference in the base units of time
measurement between the different operating systems.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt SystemClock}}
The {\tt SystemClock} class captures a {\tt Facade} to operating
system timing clocks.
At the time of writing, the Windows code path uses the
{\tt GetSystemTimeAsFileTime()} API.
Unfortunately, the use of this API is not robust
to external events, such as system clock
adjustment, either by the Network Time Protocol (NTP),
or by the systems administrator.
Such events may cause catastrophic failure of
XORP processes in the {\tt EventLoop}.
The POSIX implementation of this class was refactored
to use the {\tt clock\_gettime()}
\footnote{\url{http://www.opengroup.org/onlinepubs/000095399/functions/clock_getres.html}}
function with the {\tt CLOCK\_MONOTONIC}
system clock, which is robust against such failures.
It is highly recommended that this be refactored for Windows in
future.
However, this requires the newer {\tt GetTickCount64()}
API to be robust against clock wrap-around, and this API
is not available prior to Windows ``Longhorn'' Server.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt TimerList}}
This class implements timer callback facilities, in a manner which is compatible with
the XORP {\tt EventLoop}.
For convenience and portability, it provides a static member function,
{\tt TimerList::system\_sleep()},
which must be used in situations where one wishes to suspend execution for a known
period of time, in preference to operating system specific functions
such as {\tt sleep()}.
The Windows implementation of this member function uses a
{\em waitable timer object}
\footnote{\url{http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx}}
to provide the required timing resolution.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt EventLoop}}
The changes to the
{\tt EventLoop}
are mostly confined to the introducion of an abstraction
for I/O callbacks.
The dispatcher class used is conditionalized
on the preprocessor definition
{\tt HOST\_OS\_WINDOWS}.
If this definition is present,
{\tt WinDispatcher} is used, otherwise
{\tt SelectorList} is used.
Whilst these are candidates for a C++ abstract virtual base class,
this code is on the critical path.
Before the Windows port,
the code assumed that ``selectors'' could be added directly to the
{\tt SelectorList}
instance embedded in the
{\tt EventLoop}.
After the port, the
{\tt EventLoop::add\_ioevent\_cb()}
and
{\tt EventLoop::remove\_ioevent\_cb()}
member functions were added to reflect the new semantics,
where the types
{\tt IoEventType}
and
{\tt IoEventCb}
were used to capture them in a manner independent of the
underlying operating system.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt AsyncFileReader}}
In this class, the changes to support the different I/O event disposition in
Windows are conditionalized on the
{\tt EDGE\_TRIGGERED\_READ\_LATENCY}
preprocessor definition.
In Windows, notifications of asynchronous socket events are handled by a
helper thread. This is created when Winsock is first loaded. It uses a set of
private {\tt DeviceIoControl()}
\footnote{\url{http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx}}
calls to communicate with the {\tt TCPIP.SYS} driver, in order to set up private
mechanisms for event handling.
Recall that the {\tt WSAEventSelect()}
{\footnote{\url{http://msdn.microsoft.com/en-us/library/ms741576(VS.85).aspx}}
API is used within {\tt WinDispatcher}
to get asynchronous notifications of socket events.
There may be an undefined amount of latency between
the point in time when new data becomes available on the socket,
and the context switch which would let the Winsock helper thread
dispatch notification of this event to our primary thread.
Recall also that on Windows, all XORP processes run in their primary thread only.
The following steps must therefore take place to work around this idiosyncracy
of the {\tt TCPIP.SYS} layer upcall:
\begin{itemize}
\item A Winsock socket may have data pending to be read,
even though we haven't re-entered the XORP EventLoop yet.
\item We discover this condition by calling
{\tt ioctlsocket(FIONREAD)} immediately after
a successful read has taken place, to determine if data remains to
be read in Winsock's internal socket buffers.
\item If this is the case, we then immediately schedule a
{\tt XorpTask} to call
{\tt AsyncFileReader::read()}
on the next call to
{\tt EventLoop::run()},
thus avoiding the latency which would otherwise be introduced
into the I/O read path.
\end{itemize}
These techniques allow other XORP timers, tasks and I/O callbacks to run
whilst the I/O is pending.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{{\tt AsyncFileWriter}}
In this class, the changes to support the different I/O event disposition in
Windows are conditionalized on the
{\tt EDGE\_TRIGGERED\_WRITES}
preprocessor definition.
Recall that with the POSIX {\tt select()} call, an I/O write event is
{\em level-triggered}; it means that a file descriptor may be written to.
On the other hand, the disposition of an I/O write event from
{\tt WSAEventSelect()} is {\em edge-triggered}, and means that a pending
write to a socket which has previously posted, has just completed.
On Windows, an instance of {\tt AsyncFileWriter} needs to force a write to begin,
regardless of the current event condition.
Notification of a posted write requires that a context switch to
the Winsock helper thread takes place.
The following sequence of events is necessary:
\begin{itemize}
\item A {\tt XorpTask} is scheduled with the {\tt EventLoop}
to force a write attempt when {\tt EventLoop::run()} is called.
Writes to non-blocking sockets under Windows are always buffered,
therefore the process's primary thread will not block.
\item By ensuring that {\tt EventLoop::run()} gets called, the context
switch to the Winsock helper thread will take place, allowing the
downcall to {\tt TCPIP.SYS} to happen.
\item The XORP process then receives the event notification from Winsock,
and the write operation has completed.
\end{itemize}
{\em Note:} Writes to non-socket handles {\em always}
block the entire process, unless buffered elsewhere in the system. Windows
pipes are particularly affected by this limitation.
Windows console handles {\em may not} be affected by this as much,
partly due to internal buffering,
and also the scheduler priority ``boost'' which interactive processes receive in
the Windows environment.
This may however starve other parts of the system of time slice quantums.
To experiment with this, simply run a
recursive directory listing of a reasonably large Windows file system from
a {\tt CMD.EXE} shell.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Other changes relevant to {\tt libxorp/asyncio.cc}}
The majority of the Windows-specific changes to both
{\tt AsyncFileReader} and {\tt AsyncFileReader}
in this file are to use the {\tt WSASend()} and {\tt WSARecv()}
APIs for socket I/O.
An alternative definition of the {\tt struct iovec} type
is supplied in {\tt libxorp/xorp\_osdep\_mid.h}
to allow the buffer coalescing features
to be implemented for Windows.
Other types of file handle, such as pipes and consoles, use
their own specializations from {\tt libxorp/win\_io.c} which
have already been described.
I/O on disk files always uses the blocking
{\tt ReadFile()} and {\tt WriteFile()} APIs.