📄 xorpdev_101.tex
字号:
%% $XORP: xorp/docs/xorpdev_101/xorpdev_101.tex,v 1.24 2007/03/15 00:43:16 pavlin Exp $%\documentclass[11pt]{article}%\usepackage[dvips]{changebar}\usepackage{subfigure}\usepackage{fullpage}\usepackage{setspace}\usepackage{times}\usepackage{latexsym}\usepackage{epsfig}\usepackage{graphicx}\usepackage{xspace}\usepackage{color}\usepackage{amsmath}\usepackage{rotating}\usepackage{moreverb}\usepackage{listings}\usepackage{alltt}\usepackage{stmaryrd}%\usepackage[dvipdf]{graphics}%\usepackage[dvips]{graphicx}%\usepackage{xorp}\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{An Introduction to Writing a XORP Process \\\vspace{1ex}Version 1.4}\author{ XORP Project \\% International Computer Science Institute \\% Berkeley, CA 94704, USA \\ {\it http://www.xorp.org/} \\ {\it feedback@xorp.org}}\date{March 20, 2007}\maketitle%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Local definitions%\newcommand{\stt}{\tt\small}\newcommand{\SR}{{\tt\small static\_routes}\xspace}\newcommand{\SRI}{{\it static\_routes}\xspace}% Configure listings\lstloadlanguages{C++,C}\lstset{ tabsize=8 , basicstyle=\ttfamily\small , commentstyle=\color{gray} , flexiblecolumns=false , frame=TBLR , language=C++ , stringstyle=\ttfamily , showstringspaces=false}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\tableofcontents\newpage%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{Introduction}This document is intented for a developer who wishes to write a XORPprocess, but doesn't know where to start. We'll walk through a simpleXORP process, discussing how to define and use XRL interfaces, and howthe bits fit together. This is a first pass at such a document. We're bound to have missedthings that are not obvious when you're starting out. Please provideus feedback as to how much help this document is; what really helped,what's missing, and what isn't explained properly.We'll assume that you have copies of four other XORP design documents:\begin{itemize} \item XORP Design Overview\cite{xorp:design_arch} \item XORP Libxorp Library Overview\cite{xorp:libxorp} \item XORP Inter-Process Communication Library Overview\cite{xorp:xrl} \item XRL Interfaces: Specifications and Tools\cite{xorp:xrl_interfaces}\end{itemize}These are available from the XORP web server. You should probablyhave read these through quickly so you're aware what additionalinformation is available before reading this document further. It'srecommended to read them in the order above. We will assume you are familiar with what an XRL request is, theoverall structure of the processes on a XORP router, and with C++.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{Overview}In this document we'll work through by example the structure of asimple XORP process. We've chosen the {\it static\_routes} process asan example. At the time of writing, this document is in sync with thesource code for static\_routes, but this is not guaranteed to always bethe case.{\it static\_routes} is a very simple XORP process. To a firstapproximation, it receives XRL configuration requests from the{\it xorp\_rtrmgr} to set up static routing entries, stores the entries, andcommunicates them to the RIB using XRLs. This makes it a good example, because it exports an XRL interface toother processes (typically the xorp\_rtrmgr) and calls XRLs on the XRLinterface of another XORP process (the RIB). But it doesn't do allthat much else, so there are few files and the code is quite readable.The source code for the \SRI process is found in the {\sttxorp/static\_routes} subdirectory of the XORP source tree.We'll walk through the main pieces of static\_routes in the followingorder:\begin{itemize} \item The XRL interface of static\_routes. \item Implementing the XRL interface of static\_routes. \item The main loop of static routes. \item Calling XRLs on the RIB.\end{itemize}\newpage%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{The XRL Interface of {\it static\_routes}}XRL interfaces are defined by a {\stt.xif} file (pronounced {\it dot-ziff}).{\stt xif} stands for XRL InterFace. All {\stt.xif} files reside in{\stt xorp/xrl/interfaces}.The relevant file for us is {\sttxorp/xrl/interfaces/static\_routes.xif}. The first part of this file isshown in Listing \ref{lst:xif}.\begin{lstlisting}[caption={ The start of {\stt xorp/xrl/interfaces/static\_routes.xif} % \label{lst:xif} } ]{}/* * Static Routes XRL interface. */interface static_routes/0.1 { /** * Enable/disable/start/stop StaticRoutes. * * @param enable if true, then enable StaticRoutes, otherwise * disable it. */ enable_static_routes ? enable:bool start_static_routes stop_static_routes /** * Add/replace/delete a static route. * * @param unicast if true, then the route would be used for unicast * routing. * @param multicast if true, then the route would be used in the * MRIB (Multicast Routing Information Base) for multicast purpose * (e.g., computing the Reverse-Path Forwarding information). * @param network the network address prefix this route applies to. * @param nexthop the address of the next-hop router for this route. * @param metric the metric distance for this route. */ add_route4 ? unicast:bool & multicast:bool & network:ipv4net \ & nexthop:ipv4 & metric:u32 add_route6 ? unicast:bool & multicast:bool & network:ipv6net \ & nexthop:ipv6 & metric:u32...}\end{lstlisting}The file {\stt static\_routes.xif} defines all the XRLs that are part of the{\stt static\_routes} XRL interface. These are XRLs that other processes cancall on the {\it static\_routes} process.The format of the file is basically the keyword {\stt interface}followed by the name and version of this particular interface,followed by a list of XRLs. In this case the name of the interface is {\stt static\_routes}, butthis does not have to be the same as the name of the process. Theversion number is {\stt 0.1}. Version numbers are generally increasedwhen a change is made that is not backwards compatible, but theprecise value has no important meaning.The list of XRLs is demarked by braces {\stt \{ ... \}}, and one XRL isgiven per line. Blank lines and comments are allowed, and a backslashbefore the newline can be used to split a long XRL over multiple linesto aid readability.\vspace{0.1in}\noindentThus the first XRL in this file is:\\ {\tt\small static\_routes/0.1/enable\_static\_routes?enable:bool}\vspace{0.1in}\noindentWhen this XRL is actually called, it would look like:\\{\tt\small finder://static\_routes/static\_routes/0.1/enable\_static\_routes?enable:bool=true}\vspace{0.1in} The {\stt finder} part indicates that the XRL is anabstract one - we don't yet know what the transport parameters are.The first \SR indicates the name of the target process, and the second\SR is the name of the interface, taken from the XIFfile. A process can support more than one interface, and an interfacedefinition can be used by more than one process, hence the duplicationin a process as simple as \SRI.\newpage%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{Using the {\tt static\_routes} XRL Interface}Now we have seen how the XRLs comprising the static\_routes interfaceare defined, we shall examine how processes actually use them. Forany particular interface, there are two types of user:\begin{itemize} \item The process that calls the XRLs and gets back responses. This is called the XRL {\it caller}. \item The process on which the XRL is called, and which generates responses. This is called the XRL {\it target}.\end{itemize}XORP provides scripts which can generate C++ code to make life mucheasier for both these parties.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\subsection{Generating stub code for the caller}If we examine the file {\stt Makefile.am} (the automake Makefile) in{\stt xorp/xrl/interfaces}, we find the fragment in Listing\ref{lst:iftemp}.\begin{lstlisting}[caption={ Fragment from {\stt xorp/xrl/interfaces/Makefile.am} % \label{lst:iftemp} } ]{}################################################################################ Client Interface related################################################################################ BGP MIB trapsnoinst_LTLIBRARIES = libbgpmibtrapsxif.lalibbgpmibtrapsxif_la_SOURCES = bgp_mib_traps_xif.hh bgp_mib_traps_xif.cc...# StaticRoutes Interfacenoinst_LTLIBRARIES += libstaticroutesxif.lalibstaticroutesxif_la_SOURCES = static_routes_xif.hh static_routes_xif.cc...################################################################################ Static Pattern Rules###############################################################################SCRIPT_DIR=$(top_srcdir)/xrl/scriptsCLNTGEN_PY=$(SCRIPT_DIR)/clnt-gen@PYTHON_BUILD@%_xif.cc %_xif.hh $(srcdir)/%_xif.hh $(srcdir)/%_xif.cc: \ $(srcdir)/%.xif $(CLNTGEN_PY)@PYTHON_BUILD@ $(PYTHON) $(CLNTGEN_PY) $<\end{lstlisting}%$This adds {\stt libstaticroutesxif.la} to the list of libraries thatshould be built, and indicates that the source files for this libraryare {\stt static\_routes\_xif.hh} and {\stt static\_routes\_xif.cc}The last part is pretty cryptic, but basically is a generic rule thatsays that files ending with {\stt \_xif.cc} and {\stt \_xif.hh} will begenerated from files ending with {\stt .xif} using the python scriptcalled {\stt clnt-gen}.So what actually happens here is that the file {\stt static\_routes.xif}is processed by {\stt clnt-gen} to produce {\stt static\_routes\_xif.hh}and {\stt static\_routes\_xif.cc}, which are then compiled and linkedinto the library {\stt libstaticroutesxif.la}. Any process that wantsto call the \SRI interface can link with this library.So what functionality does this library provide? Listing\ref{lst:sr.xif.hh} shows a fragment from the machine-generated file{\stt static\_routes\_xif.hh}. Between them, {\stt static\_routes\_xif.hh} and {\sttstatic\_routes\_xif.cc} define the machine-generated class {\sttXrlStaticRoutesV0p1Client} and its complete implementation.\begin{lstlisting}[caption={ Fragment from {\stt xorp/xrl/interfaces/static\_routes\_xif.hh} % \label{lst:sr.xif.hh} } ]{}class XrlStaticRoutesV0p1Client {public: XrlStaticRoutesV0p1Client(XrlSender* s) : _sender(s) {} virtual ~XrlStaticRoutesV0p1Client() {}... typedef XorpCallback1<void, const XrlError&>::RefPtr AddRoute4CB; /** * Send Xrl intended to: * * Add/replace/delete a static route. * * @param dst_xrl_target_name the Xrl target name of the destination. * * @param unicast if true, then the route would be used for unicast * routing. * * @param multicast if true, then the route would be used in the MRIB * (Multicast Routing Information Base) for multicast purpose (e.g., * computing the Reverse-Path Forwarding information). * * @param network the network address prefix this route applies to. * * @param nexthop the address of the next-hop router for this route. * * @param metric the metric distance for this route. */ bool send_add_route4( const char* dst_xrl_target_name, const bool& unicast, const bool& multicast, const IPv4Net& network, const IPv4& nexthop, const uint32_t& metric, const AddRoute4CB& cb );...}\end{lstlisting}\newpageThe constructor for {\stt XrlStaticRoutesV0p1Client} takes a pointer toan {\stt XrlSender} as its parameter. Typically this is actually an{\stt XrlRouter} - we'll come to this in more detail later.Then for every XRL defined in {\stt static\_routes.xif} there is amethod to be called on an instance of {\stt XrlStaticRoutesV0p1Client}.The example we'll look at here is {\stt send\_add\_route4()}, althoughthere are many more methods defined in {\stt static\_routes.xif}.If you compare the method {\stt send\_add\_route4()} in Listing\ref{lst:sr.xif.hh} with the XRL {\stt add\_route4} in Listing\ref{lst:xif}, it should be pretty clear where this comes from.Basically, when you call \\{\stt XrlStaticRoutesV0p1Client::send\_add\_route4} with all theparameters ({\stt unicast}, {\stt nexthop}, etc). set appropriately,the XRL {\stt add\_route4} will be called. You don't need to concernyourself with how the parameters are marshalled into the right syntaxfor the XRL, or how the XRL is actually transmitted, or even how thetarget process is discovered. But you do need to set the {\stttarget\_name} parameter to the same thing that the \SR process sets itto, otherwise the XRL {\it finder} won't be able to route your XRL toits destination. Often the target name will be the same as the nameof the process - in this case \SRI - but if there are multipleinstances of the interface then you'll need to figure out which targetname to use.You'll also notice that some of the parameters for XRL functions arenot native C++ types. In this case, {\stt network} is of type {\sttIPv4Net} and {\stt nexthop} is of type {\stt IPv4}. Classesinstantiating the these additional types are found in {\stt libxorp}and are used throughout XORP. The final parameter is {\stt const AddRoute4CB\& cb}.\\Earlier in the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -