ATS Plugin 编译指南
ATS的plugin编译方法有两种:
inside 与ATS源代码树整合,在plugins目录中编译
outside 使用tsxs工具编译
Inside方式
在plugins目录创建一个目录my-plugin,然后在该目录内创建一个Makefile.am,内容如下:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# “License”); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include $(top_srcdir)/build/plugins.mk
pkglib_LTLIBRARIES = my_plugin.la
my_plugin_la_SOURCES = my_plugin.c my_plugin.h utils.c utils.h
my_plugin_la_LDFLAGS = $(TS_PLUGIN_LDFLAGS)
pkglib_LTLIBRARIES 指定了输出的*.so的文件名,所以我们的插件名称不一定要与插件的目录名称相同。
通过插件名称_la_SOURCES/LDFLAGS/DATA 来指定插件的对应源文件、编译连接时的参数等。但是当插件名称中出现中横线(-)时,这里我们需要用下划线(_)来代替。
需要注意,在install-data-hook部分,每一行必须以<TAB>开头。
然后打开plugin/Makefile.am, 将my-plugin目录添加到SUBDIRS里。
然后打开configure.ac,找到plugin/Makefile这一行,在下面新增:
plugins/ats-auth/Makefile
plugins/my-plugin/Makefile #add
然后重新运行autoreconf -fi 就会看到my-plugin目录下生成了Makefile
Outside方式
在任意位置创建目录my-plugin,然后在该目录内创建如下脚本,注意指定已经安装的ATS的目录以及ATS源代码所在的目录。
#!/bin/sh
[!”$ATSSRCDIR”] && export_ATSSRCDIR=/root/trafficserver
[!”$ATSINSTDIR”] && export_ATSINSTDIR=/usr/local/trafficserver
TSXS=${ATSINSTDIR}/bin/tsxs
FLAGS=”-I${ATSSRCDIR}/lib -I${ATSSRCDIR}/lib/ts -I${ATSSRCDIR}/proxy/api/ts -I${ATSSRCDIR}/proxy/api”
LIBS=”-lssl -ltsconfig”
INFILE=”my-plugin.c utils.c”
OUTFILE=”my-plugin.so”
# compile
${TSXS} ${FLAGS} ${LIBS} -o ${OUTFILE} -c ${INFILE}
# install
${TSXS} -o ${OUTFILE} -i
# copy config file
cp my-plugin.config.default ${ATSINSTDIR}/etc/trafficserver/my-plugin.config
然后,在该目录内编译源代码,调用上诉脚本即可实现对源代码的编译,同时安装插件到ATS的插件库目录。
如果需要连接第三方的扩展库,如openssl库等,可以使用:
my_plugin_la_LDFLAGS = $(TS_PLUGIN_LDFLAGS) -lssl
如果需要连接ATS的API库,如tsconfig,可以使用:
my_plugin_la_LDFLAGS = $(TS_PLUGIN_LDFLAGS) -L$(top_builddir)/lib/tsconfig/.libs -ltsconfig
注意:源项目需要更改一些文件
cp /test/trafficserver-6.0.0/lib/ts/apidefs.h.in /test/trafficserver-6.0.0/lib/ts/apidefs.h
cp /test/trafficserver-6.0.0/lib/ts/ink_config.h.in /test/trafficserver-6.0.0/lib/ts/ink_config.h
cp /test/trafficserver-6.0.0/lib/ink_autoconf.h.in /test/trafficserver-6.0.0/lib/ink_autoconf.h
ATS API的头文件
ATS API的头文件保存在${ATSINSTDIR}/include/ts目录。
这些头文件都来自ATS的源代码的 ${ATSSRCDIR}/libs/ts 或者 ${ATSSRCDIR}/proxy/api/ts 目录。
但是有一个特殊的 mgmtapi.h 来自 ${ATSSRCDIR}/mgmt/api/include 目录。
在Plugin中引用的头文件
编写Plugin时通常我们只需要使用ATS API 的头文件,此时在Outside方式下,FLAGS不需要设置,也不需要制定ATS源代码的路径。
通过TSXS工具编译源代码时,TSXS在调用gcc时,会自动添加参数-I{ATSINSTDIR}/include来实现ATS API库的头文件目录的指向。这个参数会被放在第一优先级。
因此当我们在编写plugin需要include源代码是时,用ts/xxx.h和atscppapi/xxxx.h来表示引用ATS API库的代码。
当我们编写复杂的Plugin时,往往需要使用ATS源代码中的内部API及头文件,此时为了不与ATS API 的头文件引用冲突(特别是同名但功能不同的头文件),在Outside方式可以通过指定FLAGS参数实现对内部API及头文件的调用:
-l${ATSSRCDIR}/lib
使用tsconfig/xxxx.h来表示引用ATS源码lib/tsconfig目录的头文件。
-l${ATSSRCDUR}/lib/ts 和 -l${ATSSRCDUR}/proxu/api/ts
使用xxxx.h来表示引用ATS源码lib/ts目录和proxy/api/ts目录的头文件。
对于Inside方式,则不会使用安装后的ATS API库,因此在build/plugins.mk中指定了:
-l$(top_srcdir)/lib
使用tsconfig/xxxx.h来表示引用ATS源码lib/tsconfig目录的头文件。
使用ts/xxxx.h来表示引用ATS源码lib/ts目录的头文件。
-l$(top_srcdir)/proxy/api
使用xxxx.h来表示引用ATS源码lib/ts目录和proxy/api/ts目录的头文件。
同样可以实现对ATS内部函数以及ATS API库的调用。
按照上诉规则引用头文件,可以保证一个plugin即可以使用inside模式编译,也可以使用outside模式编译。
makefile 文件
TARGET=remove_query
MAIN=-c common.c -c remove_query.c
SRC=$(MAIN)
all: $(TARGET)
install: $(TARGET)_install
clean:
rm *.lo *~ $(TARGET).so
$(TARGET):
tsxs -o $@.so $(SRC)
$(TARGET)_install:
tsxs -o $(TARGET).so -i
tsxs一般在编译打包后的trafficserver/bin 目录下
学习样例:参考
ATS插件开发入门介绍